2013-04-20 78 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