AppleScript:使用对话框返回重命名文件

时间:2016-05-10 19:37:06

标签: applescript file-rename automator

我正在尝试在Mac上的Automator中创建一个文件夹操作,当一个新文件被放入我指定的文件夹时,会打开一个对话框询问我正在放入哪个文件 - 然后,根据该选择,自动将其重命名为对话框返回,理想情况下是今天日期的变量(同时保留相同的扩展名)。

AppleScripts相当新,但这是我到目前为止没有给我任何错误但也没有做任何事情:

on run
    set theChoice to {"Option1", "Option2", "Option3", "Option4", "Other"}
    set selected to {choose from list theChoice}
    if selected is "Option1" then
        tell application "Finder"
            set currentFile to name of (selection as alias)
            set currentName to name of currentFile
            set name of currentFile to "Option1" & "." & name extension of currentFile
        end tell
    else if selected is "Option2" then
        tell application "Finder"
            set currentFile to name of (selection as alias)
            set currentName to name of currentFile
            set name of currentFile to "Option2" & "." & name extension of currentFile
        end tell
    else if selected is "Option3" then
        tell application "Finder"
            set currentFile to name of (selection as alias)
            set currentName to name of currentFile
            set name of currentFile to "Option3" & "." & name extension of currentFile
        end tell
    else if selected is "Option4" then
        tell application "Finder"
            set currentFile to name of (selection as alias)
            set currentName to name of currentFile
            set name of currentFile to "Option4" & "." & name extension of currentFile
        end tell
    else
        tell application "Finder"
            set currentFile to name of (selection as alias)
            set currentName to name of currentFile
            set name of currentFile to alias & "." & name extension of currentFile
        end tell
    end if
end run

在确定了这一部分之后,我也希望在每个文件名的末尾添加一个今天的日期(例如“_20160510”)变量,所以如果可以包括那个也很棒。

2 个答案:

答案 0 :(得分:2)

我编辑了我的快速回答。可能还有其他问题。我稍后会看一下这个。但这似乎有效。在将过多的代码放在一起之前,你必须更好地隔离较小的代码段。确保你知道什么东西首先返回。

查看原始代码与此之间的差异。你会注意到

我添加了“没有多个选择”

你不需要将choose from list放在另一个列表中,所以我改变了它,并且摆脱了“第1项的第1项”,因为现在选择的是一个简单的列表

我拿出了额外的“名字”

我输入if / then来覆盖取消(返回false)

on run
set theChoice to {"Option1", "Option2", "Option3", "Option4", "Other"}
set selected to (choose from list theChoice without multiple selections allowed)

if selected is not false then
    if item 1 of selected is "Option1" then
        tell application "Finder"
            set currentFile to (selection as alias)
            set currentName to name of currentFile
            set name of currentFile to "Option1" & "." & name extension of currentFile
        end tell
    else if item 1 of selected is "Option2" then
        tell application "Finder"
            set currentFile to (selection as alias)
            set currentName to name of currentFile
            set name of currentFile to "Option2" & "." & name extension of currentFile
        end tell
    else if item 1 of selected is "Option3" then
        tell application "Finder"
            set currentFile to (selection as alias)
            set currentName to name of currentFile
            set name of currentFile to "Option3" & "." & name extension of currentFile
        end tell
    else if item 1 of selected is "Option4" then
        tell application "Finder"
            set currentFile to (selection as alias)
            set currentName to name of currentFile
            set name of currentFile to "Option4" & "." & name extension of currentFile
        end tell
    else
        tell application "Finder"
            set currentFile to (selection as alias)
            set currentName to name of currentFile
            --set name of currentFile to alias & "." & name extension of currentFile
            --what is it you want to do here??
        end tell
    end if
end if

结束

<强> [编辑]

有关向日期数字添加前导零的建议,请参阅http://www.foodoo.sunreal.com.au/info/date_info.html 它虽旧,但仍然合适。 另外,考虑(如果可能)使用do shell script命令,如:

do shell script "date +\"%m-%d-%y-%H-%M-%S\""

将返回类似

的内容
  

“05-12-16-11-46”

(见http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

至于在Automator中使用这些东西,可能有助于了解你在Automator中所做的事情。

答案 1 :(得分:0)

这是总结冗余代码的脚本的较短版本

set theChoice to {"Option1", "Option2", "Option3", "Option4", "Other"}
set selected to {choose from list theChoice}
if theChoice is false then return
set chosen to item 1 of theChoice

tell application "Finder" to set theSelection to selection
if theSelection is {} then return
set currentFile to item 1 of theSelection

set currentDate to do shell script "date +%Y%d%m"

if chosen is "Other" then
    tell application "Finder"
        -- do something with "Other"
    end tell
else
    tell application "Finder"
        set name of currentFile to chosen & "_" & currentDate & "." & name extension of currentFile
    end tell
end if