AppleScript在Finder中设置目录路径

时间:2013-04-20 19:21:43

标签: macos applescript finder

我正在尝试通过AppleScript删除计算机上的文件。当我应用下面的代码时,它似乎从桌面删除该文件。我想删除“/ Users / andrew / Documents”中的文件。这是下面的代码,从桌面删除文件:

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

这是尝试过的,但是不起作用:

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

如果有人可以提出任何建议,我们将不胜感激!!

1 个答案:

答案 0 :(得分:1)

在获取文件对象的路径前添加POSIX file

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

system attribute "HOME"已替换为/Users/username

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

或使用pre-OS X路径格式:

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