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

AppleScript:使用对话框返回重命名文件,applescript,file-rename,automator,Applescript,File Rename,Automator,我试图在Mac上的Automator中创建一个文件夹操作,当一个新文件放入我指定的文件夹时,会打开一个对话框,询问我要放入哪个文件——然后,根据该选择,会自动将其重命名为对话框return,理想情况下是一个今天日期的变量(同时保留相同的扩展名) AppleScripts非常新,但这是我迄今为止所拥有的,它不会给我任何错误,但也不会做任何事情: on run set theChoice to {"Option1", "Option2", "Option3", "Option4", "Oth

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

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

在弄清楚这一部分之后,我还希望在每个文件名的末尾添加一个“今天的日期”(例如“\u 20160510”)变量,因此如果可以也包括这一部分,那就太好了。

我已经编辑了我的快速答案。可能还有其他问题。我得晚一点再看这个。但这似乎奏效了。在将太多代码放在一起之前,您必须更好地隔离较小的代码部分。确保你知道什么东西首先返回

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

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

您不需要将
从列表中选择
放在另一个列表中,因此我改变了这一点,并去掉了“的项目1中的项目1”,因为现在选择的是一个简单的列表

我拿出了额外的“名字”

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

终点

[编辑]

有关在日期数字中添加前导零的建议,请参阅 它很旧,但仍然合适。 此外,考虑(如果可能的话)使用<代码> DO shell脚本< /C>命令,如:

do shell script "date +\"%m-%d-%y-%H-%M-%S\""
这将返回类似于

“05-12-16-11-46”

(见附件)


至于在Automator中使用这些东西,它可能有助于了解您在Automator中所做的工作。

我已经编辑了我的快速答案。可能还有其他问题。我得晚一点再看这个。但这似乎奏效了。在将太多代码放在一起之前,您必须更好地隔离较小的代码部分。确保你知道什么东西首先返回

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

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

您不需要将
从列表中选择
放在另一个列表中,因此我改变了这一点,并去掉了“的项目1中的项目1”,因为现在选择的是一个简单的列表

我拿出了额外的“名字”

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

终点

[编辑]

有关在日期数字中添加前导零的建议,请参阅 它很旧,但仍然合适。 此外,考虑(如果可能的话)使用<代码> DO shell脚本< /C>命令,如:

do shell script "date +\"%m-%d-%y-%H-%M-%S\""
这将返回类似于

“05-12-16-11-46”

(见附件)


至于在Automator中使用这些东西,它可能有助于了解您在Automator中所做的工作。

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

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

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

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

您只更改了临时变量
currentFile
的名称。最后一步可能需要像
将(所选内容作为别名)的名称设置为currentFile
这样的内容。@PaulR谢谢--我尝试了
告诉应用程序“Finder”将currentFile设置为名称(所选内容作为别名)将currentFile设置为名称将currentFile的名称设置为“Option1”&“currentFile的名称扩展名(&name extension of currentFile)将名称(选择为别名)设置为currentFile end tell
,但它似乎没有任何作用。您只更改了临时变量的名称
currentFile
。最后一步可能需要像
将(所选内容作为别名)的名称设置为currentFile
这样的内容。@PaulR谢谢--我尝试了
告诉应用程序“Finder”将currentFile设置为名称(所选内容作为别名)将currentFile设置为名称将currentFile的名称设置为“Option1”&“currentFile的名称扩展名(&name)将的名称(选择为别名)设置为currentFile end tell
,但似乎没有任何作用。谢谢!仍然需要找出一些错误,看起来,但你肯定使它更接近。第一个问题是,有时它不起作用?但有时确实如此。所以我得再深入一点,看看是否有规律。但是第二,当它真的起作用的时候,它看起来像是在一个循环中?我将选择“Option1”(当它工作时),它会将文件名完全更改为Option1,但随后对话框会再次打开?似乎我可以在所有选项之间不断更改名称,直到我连续两次选择一个。我已经对答案进行了编辑。再次编辑b/c我在
else
部分的末尾留下了其中的一个“name of”。@Ryan实际上不知道你想用
else
部分做什么。你能解释一下为什么会这样吗?它不起作用,但我们需要知道你的意图是什么。还要记住,如果已经有一个文件同名,它将失败。除了else部分之外,它还可以作为基本代码工作,但是实际上没有错误更正。谢谢——仍然有一些轻微的错误,但是我认为模式现在已经确定了。重命名工作,虽然我认为循环的原因是因为当我们更改文件名时,Automator的文件夹操作被重新激活。有没有办法把它关掉?否则我想我可以在第一次之后点击“取消”。谢谢!仍然需要找出一些错误,看起来,但你肯定使它更接近。第一个问题是,有时它不起作用?但有时确实如此。所以我得再深入一点,看看是否有规律。但是第二,当它真的起作用的时候,它看起来像是在一个循环中?我将选择“Option1”(当它工作时),它会将文件名完全更改为Option1,但随后对话框会再次打开?似乎我可以在所有选项之间不断更改名称,直到选择为止