move a file in finder with applescript

元气小坏坏 提交于 2019-11-28 08:47:34

问题


I just want to move an image from one folder to the other, replacing the one that's already in there:

tell application "Finder"
      copy file "/Users/xx/Documents/img.jpg" to folder "/Users/xx/Documents/State"
   end tell

When I run it, I get an error message saying

Finder got an error: Can’t set folder [path] to file [path]"."number -10006 from folder [path]

Please help me!


回答1:


Try:

tell application "Finder"
    duplicate POSIX file "/Users/xx/Documents/img.jpg" to POSIX file "/Users/xx/Documents/State" with replacing
end tell

Or

tell application "Finder"
    move POSIX file "/Users/xx/Documents/img.jpg" to POSIX file "/Users/xx/Documents/State" with replacing
end tell



回答2:


As @adayzdone notes, the error appears because you're using a Posix-style path without declaring it.

Another approach is to use colon-separated HFS paths, like so:

move file "Macintosh HD:Users:xx:Documents:img.jpg" ¬
to "Macintosh HD:Users:xx:Documents:State:" with replacing

With colon-separated paths you need to include the whole thing, including the volume name (I'm assuming Macintosh HD here), otherwise it'll throw our good friend error 10,006.



来源:https://stackoverflow.com/questions/14058061/move-a-file-in-finder-with-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!