简体   繁体   English

已启动,可防止脚本更改桌面图片

[英]Launchd prevents script from changing desktop picture

I have a little script I'm working on to retrieve a new picture from http://reddit.com/r/wallpapers daily, and set that as my wallpaper. 我有一个正在处理的小脚本,每天从http://reddit.com/r/wallpapers检索一张新图片,并将其设置为我的墙纸。 When I call the script from the command line, it works beautifully. 当我从命令行调用脚本时,它可以很好地工作。 But as soon as I try and setup a launch daemon it will save the picture, but the desktop does not change. 但是,一旦我尝试设置启动守护程序,它将保存图片,但桌面不会更改。 I expect it has something to do with the scope, but I have no idea where to start. 我希望它与范围有关,但是我不知道从哪里开始。

Code: 码:

SCRIPT = """/usr/bin/osascript<<END
tell application "Finder"
set desktop picture to POSIX file "%s"
end tell
END"""

file_path = /some/file/the/script/downloaded.jpg

subprocess.Popen(SCRIPT%file_path, shell=True)

You could be right about the reason, however I've seen strange things with "POSIX file" commands inside an application tell block of code. 您可能是对的,但是我在应用程序内部的“ POSIX文件”命令中看到了奇怪的东西,告诉代码块。 "POSIX file" is an applescript command. “ POSIX文件”是一个applescript命令。 You will find it in the "standard additions" applescript dictionary not the Finder's applescript dictionary. 您将在“标准增补” Applescript词典中找到它,而不是在Finder的Applescript词典中找到它。 As such it could be a problem when you tell the Finder to perform that command because the Finder doesn't know that command. 因此,当您告诉Finder执行该命令时可能会出现问题,因为Finder不知道该命令。

A general applescript rule is that you shouldn't tell an application to do something that's not in its applescript dictionary... strange things can happen if you do. applescript的一般规则是,您不应告诉应用程序执行其applescript词典中未包含的内容……如果您这样做,可能会发生奇怪的事情。 And you have a strange thing happening so step 1 is to make sure your code is as clean as possible. 而且您发生了一件奇怪的事情,因此第一步是确保您的代码尽可能干净。

So here's something to try. 因此,这里有一些尝试。 As mentioned I'm just guessing because I've seen problems with this before so it's worth a shot. 如前所述,我只是在猜测,因为我之前已经看到过与此相关的问题,因此值得一试。 Try this as your applescript code. 尝试将此作为您的applescript代码。

set p to POSIX file "%s"
tell application "Finder" to set desktop picture to p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM