srobinson
srobinson

Reputation: 356

Python subprocess doesn't work when ran by launchd

I have a Python script that uses subprocess to run an AppleScript that changes my desktop background. A bash script is called by launchd periodically to run the Python script.

When I run the bash script from the command line it works fine, but the problem is that when launchd calls the bash script in the same exact way the AppleScript doesn't seem to work (I've debugged it up until this point, and everything prior to the subprocess call works fine). My guess is its a permissions issue, but launchd seems to be running the script as me. I've even tried having it run as root and it still doesn't work. Running it as a Daemon and User Admin (/Library/LaunchAgents), also doesn't work.

Here is my plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Disabled</key>
        <false/>
        <key>Label</key>
        <string>com.foo.background.change</string>
        <key>Nice</key>
        <integer>-15</integer>
        <key>ProgramArguments</key>
        <array>
                <string>/bin/bash</string>
                <string>-c</string>
                <string>/Users/scott/Projects/Background/env/bin/Background/change_background.sh</string>
        </array>
        <key>ServiceDescription</key>
        <string>Runs a script to change the desktop background image.</string>
        <key>ServiceIPC</key>
        <false/>
        <key>StartInterval</key>
        <integer>60</integer>
        <key>UserName</key>
        <string>scott</string>
</dict>
</plist>

Any and all help is appreciated!

EDIT: Thanks for the response! Here is the python script. set_desktop_background is called from another method that determines which image to use. The full image path is always used.

import subprocess

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

def set_desktop_background(filepath):
    subprocess.Popen(SCRIPT%filepath, shell=True)

Upvotes: 4

Views: 519

Answers (1)

srobinson
srobinson

Reputation: 356

Finally figured it out. Somewhere down the line while I was writing this script I enabled the 'Change Picture: Every Day' feature in OSX's System Preferences (I wanted to see if I could find the plist used for this process). Once I finally realized this and disabled it, launchd was able to change the background via my script.

I'm still not sure why this happened since I was able to change the background when I manually ran the script, but oh well.

Thanks for the help!

Upvotes: 1

Related Questions