2017-06-18 99 views
0

时,我有一个简单的脚本来取物品倒垃圾,并把它们放回原来的目录:AppleScript的处理对话把文件传​​回垃圾桶

repeat 10 times 
    tell application "Finder" to open trash 
    tell application "Finder" to activate 
    tell application "System Events" 
     tell process "Finder" 
      delay 0.2 
      key code 125 
      key down command 
      delay 0.2 
      key code 51   
      key up command  
     end tell 
    end tell 
    delay 0.2 
    tell application "Finder" to close every window 
end repeat 

但运行此脚本抛出了一个异常 - 取景器弹出对话框,说文件已经存在于原始位置,有3个选项:'同时保留','停止'和'替换'。我希望脚本自动选择'替换'。

任何想法?

回答

0

你要面对几个问题:

文件的最后一个位置存储垃圾.ds_store内。对于某些文件(如果原始文件夹被删除),路径将不会存在于该文件中,并且放回选项将不起作用(因此您的脚本不会) - 如果手动尝试,您会在上下文菜单中看到它变灰。

然后按下按钮的gui方法不会返回任何东西 - 所以你不能控制进一步的步骤,如覆盖文件。

最好的方法是读出原始位置,然后用覆盖选项创建移动命令。

在另一个线程上有人写了珍珠脚本来做到这一点:https://superuser.com/a/867988

运行脚本像

set ps to "posixpath/to/script.pl" 
set moveCommands to do shell script "perl " & quoted form of ps 

然后执行所有的移动命令与执行shell脚本像上面。最好的事情是你可以chsnge脚本添加选项,如不存在“mv -n”就不要覆盖。我没有测试通过它阅读的脚本。您可以通过管道传输命令,以便在更少的步骤中完成所有操作。

请注意,这将适用于存在原始文件夹的文件。如果你有不同的情况,你需要调整它,使文件夹创建

0

这适用于我运行最新版本的OSX Sierra。您可能需要调整一些延迟设置为您工作

tell application "Finder" 
    close every window 
    activate 
    open trash 
    select every item of trash 
    delay 1 
end tell 
tell application "System Events" 
    key code 51 using (command down) 
    delay 1 
    try 
     set theCheckbox to checkbox "Apply to All" of scroll area 1 of window "Copy" of application process "Finder" 
     set applyToAll to perform action "AXPress" of checkbox "Apply to All" of scroll area 1 of window "Copy" of application process "Finder" 
     set replaceButton to perform action "AXPress" of button "Replace" of scroll area 1 of window "Copy" of application process "Finder" 
     if theCheckbox exists then 
      delay 1 
      applyToAll 
      delay 1 
      replaceButton 
     else 
      key code 51 using (command down) 
     end if 
    end try 
end tell