AppleScript set directory path in Finder

感情迁移 提交于 2019-12-02 18:01:32

问题


I'm trying to delete a file on my computer via AppleScript. When I apply the code below, it seems to delete the file from desktop. I'd like to delete the file in the "/Users/andrew/Documents". This is the code below which deletes the file from the desktop:

tell application "Finder"
    if exists file "new.mp3" then
        delete file "new.mp3"
    end if
end tell

This is what tried and it doesn't work:

tell application "Finder"
    if exists file "/Users/andrew/Documents/new.mp3" then
        delete file "/Users/andrew/Documents/new.mp3"
    end if
end tell

If anyone could any advice it would be much appreciated!!


回答1:


Add POSIX file before the path to get a file object:

tell application "Finder"
    set f to POSIX file "/Users/username/Documents/new.mp3"
    if exists f then delete f
end tell

system attribute "HOME" is replaced with /Users/username:

set f to POSIX file ((system attribute "HOME") & "/Documents/new.mp3")
tell application "Finder" to if exists f then delete f

Or use the pre-OS X path format:

tell application "Finder"
    set f to "Macintosh HD:Users:username:Documents:new.mp3"
    -- set f to (path to documents folder as text) & "new.mp3"
    if exists f then delete f
end tell


来源:https://stackoverflow.com/questions/16124232/applescript-set-directory-path-in-finder

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