你最大的問題是您使用的是POSIX風格的路徑(使用斜線)時,你應該使用的是Mac風格的路徑(用冒號)。 Applescript使用冒號分隔的路徑,我們可以轉換兩者之間使用「posix路徑」從冒號走到斜槓和「posix文件」從斜槓到冒號。
而且您不能僅將路徑作爲字符串傳遞。在applescript中,我們使用文件說明符......這就是爲什麼我把文件放在字符串路徑前面的原因。所以一旦我們解決這個問題,這個工作。
set posixPath to "/Users/abc/Downloads/02331_veiledinclouds_2560x1600.jpg"
set macPath to (POSIX file posixPath) as text
tell application "URL Access Scripting"
download "http://interfacelift.com/wallpaper_beta/grab/02331_veiledinclouds_2560x1600.jpg" to file macPath
end tell
但是,必須有一個文件名長度問題,因爲當我運行它時,下載文件的文件名被縮短爲31個字符。
編輯: 下面是一個腳本,用於在必要時截斷,下載和重命名文件。
set posixPath to "/Users/abc/Downloads/02331_veiledinclouds_2560x1600.jpg"
set baseName to do shell script "/usr/bin/basename " & quoted form of posixPath
set needsRenaming to false
if (count of baseName) is greater than 31 then
set downloadName to text -31 thru -1 of baseName
set basePath to do shell script "/usr/bin/dirname " & quoted form of posixPath
set posixPath to basePath & "/" & downloadName
set needsRenaming to true
end if
set macPath to (POSIX file posixPath) as text
tell application "URL Access Scripting"
download "http://interfacelift.com/wallpaper_beta/grab/02331_veiledinclouds_2560x1600.jpg" to file macPath
end tell
if needsRenaming then
tell application "Finder"
set name of file macPath to baseName
end tell
end if
很好玩。我甚至沒有想過這樣做。 – 2010-08-10 13:15:18