How can I programmatically change the background in Mac OS X?

后端 未结 10 1895
轮回少年
轮回少年 2020-11-28 03:30

How would I go about programmatically changing the desktop background in Mac OS X? I\'d like to use python, but I\'m interested in any way possible. Could I hook up to Ter

相关标签:
10条回答
  • 2020-11-28 03:40

    Another way to programmatically change the desktop wallpaper is to simply point the wallpaper setting at a file. Use your program to overwrite the file with the new design, then restart the dock: killall Dock.

    The following depends on Xcode, lynx and wget, but here's how I automatically download and install a monthly wallpaper on Mountain Lion (shamelessly stolen and adapted from http://ubuntuforums.org/showthread.php?t=1409827) :

    #!/bin/bash
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/local/bin
    size=1440
    dest="/usr/local/share/backgrounds/wallpaperEAA.jpg"
    read -r baseurl < <(lynx -nonumbers -listonly -dump 'http://www.eaa.org/en/eaa/aviation-education-and-resources/airplane-desktop-wallpaper' | grep $size) &&
    wget -q "$baseurl" -O "$dest"
    killall Dock
    

    Dump it into /etc/periodic/monthly/ and baby, you got a stew goin!

    0 讨论(0)
  • 2020-11-28 03:45

    To add to Matt Miller's response: you can use subprocess.call() to execute a shell command as so:

    import subprocess
    subprocess.call(["defaults", "write", "com.apple.Desktop", "background", ...])
    
    0 讨论(0)
  • 2020-11-28 03:47

    You could also use py-appscript instead of Popening osascript or use ScriptingBridge with pyobjc which is included in 10.5 but a bit more cumbersome to use.

    0 讨论(0)
  • 2020-11-28 03:48

    From python, if you have appscript installed (sudo easy_install appscript), you can simply do

    from appscript import app, mactypes
    app('Finder').desktop_picture.set(mactypes.File('/your/filename.jpg'))
    

    Otherwise, this applescript will change the desktop background

    tell application "Finder"
        set desktop picture to POSIX file "/your/filename.jpg"
    end tell
    

    You can run it from the command line using osascript, or from Python using something like

    import subprocess
    
    SCRIPT = """/usr/bin/osascript<<END
    tell application "Finder"
    set desktop picture to POSIX file "%s"
    end tell
    END"""
    
    def set_desktop_background(filename):
        subprocess.Popen(SCRIPT%filename, shell=True)
    
    0 讨论(0)
  • 2020-11-28 03:55

    Building on dF.'s answer, you could do it with Apple Script without Finder and you can do it for multiple desktops.

    To set the wallpaper for desktop i (desktop numbers start at 1):

    tell application "System Events"
        set currDesktop to item i of desktop
        set currDesktop's picture to "image_path"
    end tell
    

    This is what I ended up doing (in Python):

    SET_DESKTOP_IMAGE_WRAPPER = """/usr/bin/osascript<<END
    tell application "System Events"
    {}
    end tell
    END"""
    
    SET_DESKTOP_IMAGE = """
    set currDesktop to item {idx} of desktops
    set currDesktop's picture to "{image_path}"
    """
    
    def set_wallpapers(images):
        """ images is an array of file paths of desktops """
    
        script_contents = ""
        for i, img in enumerate(images):
            idx = i+1
            script_contents += SET_DESKTOP_IMAGE.format(idx=idx, image_path=img)
    
        script = SET_DESKTOP_IMAGE_WRAPPER.format(script_contents)
        subprocess.check_call(script, shell=True)
    

    Sometimes, the desktop images don't appear immediately. I don't know why this happens, but restarting the dock fixes it. To do that from python:

    subprocess.check_call("killall Dock", shell=True)
    

    By the way, you can get the number of desktops on the system using this AppleScript code:

    tell application "System Events"
        get the number of desktops
    end tell
    

    you can use that with subprocess.check_output to get the output

    0 讨论(0)
  • 2020-11-28 03:58

    The one-line solution for Mavericks is:

    osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Earth Horizon.jpg"'
    
    0 讨论(0)
提交回复
热议问题