有人知道为什么我的按钮不能用于 applscript 中的文件夹操作吗? 其设计目的是从 Automator 启动并保持下载文件夹干净,并提供重命名重复项的选项,然后移动重命名的文件或将重复项移至 bin。
但是,单击任何按钮都会导致脚本停止,而不移动文件。
关于如何纠正这个问题有什么建议吗?
on run {input, parameters}
tell application "Finder"
set fileToMove to item 1 of input
set fileName to name of fileToMove
set chosenFolder to POSIX path of (choose folder with prompt "Select a folder to move " & fileName & " ")
try
move fileToMove to folder chosenFolder
-- Make sound play only on success
on error
-- Display notification with title "File Not Moved" subtitle "File with that name already exists at that location"
set buttonReturned to display alert "Duplicate File Found" message "Would you like to move " & fileName & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep & Rename" cancel button "Cancel"
if buttonReturned is "Move Duplicate to Bin" then
delete fileToMove
else if buttonReturned is "Keep & Rename" then
set newName to the text returned of (display dialog "Set New File name for: " & fileName & "" default answer "")
set the name of fileToMove to newName
move fileToMove to folder chosenFolder
end if
end try
end tell
do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif'"
open currentWindow
end run
当我单击相应的按钮时,我希望发生该操作
即当按钮移动到垃圾箱时,会将原件移动到垃圾箱 当单击“保留并重命名”按钮时,它应该要求输入新名称,然后将文件重命名为输入名称,然后移动重命名的文件
这个结构是不正确的:
set buttonReturned to display alert "Duplicate File Found" message "Would you like to move " & fileName & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep & Rename" cancel button "Cancel"
if buttonReturned is "Move Duplicate to Bin" then
if
永远不会为真,因为display alert
的输出不是文本;这是一本字典。像这样重写:
display alert "Duplicate File Found" message "Would you like to move " & "what" & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep & Rename" cancel button "Cancel"
set buttonReturned to button returned of result
现在您的
if
测试将起作用。这不会解决您的脚本的所有问题,但它会解决您询问的问题。