导出为.app的AppleScript的相对路径

时间:2016-11-11 19:05:24

标签: applescript

我将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

脚本的路径是〜/ Desktop / script / script.app / Contents / Resources / Scripts / main.scpt

我想将图像放在Resources文件夹中并使路径相对,这样我就可以将文件夹放在任何地方,而不用更改我的脚本,所以我试过

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.

1 个答案:

答案 0 :(得分: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将抛出错误。