【问题标题】:AppleScript: Use Dialog Return to Rename FileAppleScript:使用对话框返回重命名文件
【发布时间】:2016-05-13 16:10:55
【问题描述】:

我正在尝试在 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”)变量,所以如果这也可以包含,那就太好了。

【问题讨论】:

  • 您只更改了临时变量currentFile 的名称。您可能需要像 set name of (selection as alias) to currentFile 这样的东西作为最后一步。
  • @PaulR 谢谢 -- 我试过 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 set name of (selection as alias) to currentFile end tell 但它似乎没有做任何事情。

标签: applescript file-rename automator


【解决方案1】:

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

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

我添加了“没有多选”

您不需要将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 中做了什么可能会有所帮助。

【讨论】:

  • 谢谢!看起来仍然需要解决一些错误,但你肯定让它更接近了。第一个问题是有时它不起作用?但有时确实如此。所以我必须深入研究一下,看看是否有模式。但其次,当它起作用时——它似乎陷入了一个循环?我将选择“Option1”并且(当它工作时)它将文件名完美地更改为 Option1,但随后对话框再次弹出打开?而且似乎我可以不断更改所有选项之间的名称,直到我连续选择一个两次。
  • 我已对答案进行了编辑。并再次编辑 b/c 我在 else 部分的末尾留下了其中一个“名称”。
  • @Ryan 实际上,不确定您要对 else 部分做什么。你能解释一下为什么会这样吗?它不起作用,但我们需要知道您的意图是什么。
  • 另外请记住,如果已经有一个具有该名称的文件,它将失败。这作为基本代码工作(除了 else 部分),但几乎没有错误纠正。
  • 谢谢——仍然有轻微的错误,但我认为模式现在已经确定。重命名工作,虽然我认为循环的原因是因为当我们更改文件名时,来自 Automator 的文件夹操作被重新激活。有什么办法可以关闭它吗?否则我想我可以在第一次之后点击“取消”。
【解决方案2】:

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

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

【讨论】:

  • 谢谢!这干净多了。不过我正在对此进行测试,当我输入 AppleScript 时,至少语法是好的,但是当我把它放到 Automator 中时,它说 Can’t set selected to {choose from list theChoice}. Access not allowed. 当我放回我的旧脚本时,它说 Can’t set selected to choose from list theChoice without multiple selections allowed. Access not allowed. 我所做的一切Automator 将它用于文件夹操作——剩下的就是这段代码。有任何想法吗?以前没见过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
相关资源
最近更新 更多