0

我正在尝试通过 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

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

4

1 回答 1

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
于 2013-04-20T19:49:19.620 回答