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

后端 未结 10 1896
轮回少年
轮回少年 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:58

    There are a default collection of quality images in:

    /Library/Desktop Pictures/
    

    To automate the selection of one of these across all desktops, without a 3rd party dependency:

    osascript -e 'tell application "System Events" to set picture of every desktop to "/Library/Desktop Pictures/Solid Colors/Stone.png"'
    
    0 讨论(0)
  • 2020-11-28 03:59

    If you are doing this for the current user, you can run, from a shell:

    defaults write com.apple.desktop Background '{default = {ImageFilePath = "/Library/Desktop Pictures/Black & White/Lightning.jpg"; };}'
    

    Or, as root, for another user:

    /usr/bin/defaults write /Users/joeuser/Library/Preferences/com.apple.desktop Background '{default = {ImageFilePath = "/Library/Desktop Pictures/Black & White/Lightning.jpg"; };}'
    chown joeuser /Users/joeuser/Library/Preferences/com.apple.desktop.plist
    

    You will of course want to replace the image filename and user name.

    The new setting will take effect when the Dock starts up -- either at login, or, when you

    killall Dock
    

    [Based on a posting elsewhere, and based on information from Matt Miller's answer.]

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

    I had this same question, except that I wanted to change the wallpaper on all attached monitors. Here's a Python script using appscript (mentioned above; sudo easy_install appscript) which does just that.

    #!/usr/bin/python
    
    from appscript import *
    import argparse
    
    def __main__():
      parser = argparse.ArgumentParser(description='Set desktop wallpaper.')
      parser.add_argument('file', type=file, help='File to use as wallpaper.')
      args = parser.parse_args()
      f = args.file
      se = app('System Events')
      desktops = se.desktops.display_name.get()
      for d in desktops:
        desk = se.desktops[its.display_name == d]
        desk.picture.set(mactypes.File(f.name))
    
    
    __main__()
    
    0 讨论(0)
  • 2020-11-28 04:03

    You can call "defaults write com.apple.Desktop Background ..." as described in this article: http://thingsthatwork.net/index.php/2008/02/07/fun-with-os-x-defaults-and-launchd/

    The article also goes into scripting this to run automatically, but the first little bit should get you started.

    You might also be interested in the defaults man pages: http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/defaults.1.html

    0 讨论(0)
提交回复
热议问题