2013-04-20 81 views
0

我试图通过AppleScript在我的计算机上删除文件。当我应用下面的代码时,它似乎从桌面上删除文件。我想删除“/ Users/andrew/Documents”中的文件。这是低于该代码从桌面删除文件:Finder中的AppleScript设置目录路径

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

路径前添加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 

或者使用预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 
+0

非常感谢Lauri解决方案 - 非常棒! – andrew 2013-04-20 20:48:17