2016-11-11 40 views
0

我有一個AppleScript導出爲.app文件,因爲我需要它在日誌運行到AppleScript的相對路徑導出爲的.app

這是我的代碼:

repeat 
    tell application "System Events" 
     repeat with desktopNumber from 1 to count of desktops 
      tell desktop desktopNumber 
       set picture to "~/Desktop/script/img.jpg" 
      end tell 
     end repeat 
    end tell 
end repeat 

的路徑腳本是〜/桌面/腳本/ script.app /內容/資源/腳本/ main.scpt

我想將圖像放在資源文件夾以及相關的路徑,所以我可以把文件夾在任何地方,而不會改變我的腳本,所以我試過

set desktopPicture to ((container of container of (path to me)) as text) & "/img.jpg" 
repeat 
    tell application "System Events" 
     repeat with desktopNumber from 1 to count of desktops 
      tell desktop desktopNumber 
       set picture to desktopPicture 
      end tell 
     end repeat 
    end tell 
end repeat 

但是,這給我的錯誤Can’t make container of container of alias \"Macintosh HD:Users:Me:Desktop:script:script.app:Contents:Resources:Scripts:main.scpt\" into type text.

回答

0

System Events不能擴大波浪~

如果你想使用相對路徑來引用~/Desktop/script/img.jpg你可以使用

tell application "System Events" to set desktopPicture to file "img.jpg" of folder "script" of desktop folder 

set desktopPicture to alias ((path to desktop folder as text) & "script:img.jpg") 

考慮,在這兩種情況下,如果文件不存在的AppleScript將拋出一個錯誤。