Applescript将桌面图片设置为当前目录中的文件

时间:2015-03-18 04:18:56

标签: macos applescript

我尝试将当前桌面图片设置为脚本当前目录中的图片。例如,如果我的文件夹结构是:

~/Documents/Scripts/DesktopImageScript/image.jpg

将脚本放在同一目录中,我希望能够将桌面图像设置为image.jpg,而无需直接引用文件夹结构。我目前用于获取当前目录的代码是:

tell application "System Events" to set app_directory to POSIX path of (container of (path to me))

问题不在于该代码,因为我可以使用预期的正确结果运行以下命令:

do shell script "echo " & app_directory

我认为问题出在我用来设置桌面图片的代码中:

tell application "Finder" set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg")) end tell

我尝试运行脚本时收到的错误是:

error "Finder got an error: AppleEvent handler failed." number -10000

不确定导致错误的原因或解决方法。任何帮助表示赞赏。完整的脚本如下:

tell application "System Events" to set app_directory to POSIX path of (container of (path to me)) tell application "Finder" set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg")) end tell

1 个答案:

答案 0 :(得分:1)

使用StackOverflow上另一个答案的代码解决。

tell application "System Events" set theDesktops to a reference to every desktop repeat with x from 1 to (count theDesktops) set picture of item x of the theDesktops to app_directory & "/image.jpg" end repeat end tell