Skip to main content
Solved

Mavericks Wallpaper - the bash version

  • April 23, 2014
  • 30 replies
  • 137 views

Show first post

30 replies

Ninyo
Forum|alt.badge.img+6
  • Contributor
  • August 25, 2018

Hi,
Did anybody checked the latest revision of @seanhansell on High Sierra or Mojave?
Can't seem to get it to work.


Forum|alt.badge.img+12
  • Contributor
  • August 25, 2018

I found, can't remember where, and use this to set ours for different users by passing through the parameters from a bash script, works well on 10.12 and 10.13.
call it from bash like this python scriptname --path wallpaperpath

#!/usr/bin/python

'''Uses Cocoa classes via PyObjC to set a desktop picture on all screens.
Tested on Mountain Lion and Mavericks. Inspired by Greg Neagle's work: https://gist.github.com/gregneagle/6957826

See:
https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSWorkspace_Class/Reference/Reference.html

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html

https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSScreen_Class/Reference/Reference.html
'''

from AppKit import NSWorkspace, NSScreen
from Foundation import NSURL
import argparse
import sys

parser = argparse.ArgumentParser(description='Sets the desktop picture on all screens')
parser.add_argument('--path', help='The path of the image')
args = vars(parser.parse_args())

if args['path']:
    picture_path = args['path']
else:
    print >> sys.stderr, 'You must supply a path for the desktop picture'
    exit(-1)

# generate a fileURL for the desktop picture
file_url = NSURL.fileURLWithPath_(picture_path)

# make image options dictionary
# we just make an empty one because the defaults are fine
options = {}

# get shared workspace
ws = NSWorkspace.sharedWorkspace()

# iterate over all screens
for screen in NSScreen.screens():
    # tell the workspace to set the desktop picture
    (result, error) = ws.setDesktopImageURL_forScreen_options_error_(
                file_url, screen, options, None)
    if error:
        print error
        exit(-1)

Forum|alt.badge.img+17
  • Valued Contributor
  • August 26, 2018

@marklamont thats Graham Gilbert’s script, as mentioned earlier. https://github.com/grahamgilbert/macscripts/blob/master/set_desktops/set_desktops.py


seanhansell
Forum|alt.badge.img+16
  • Contributor
  • September 4, 2018

@Ninyo My script is still working for me. :)


Forum|alt.badge.img+5
  • New Contributor
  • January 22, 2020

Thanks for the Python script Graham Gilbert.

Not sure if it helps anyone else, but I made a few modifications.

I've tested on Catalina deploying from Jamf and it works great.

  • Removed the option to supply a path with --path.
  • Added a check to make sure that the file type is .png.
  • Update: Make sure to put the desktop photo in a place like /Library/Desktop Pictures and point the script to that location.
#!/usr/bin/python

"""A script to set the desktop wallpaper on macOS.
"""

###############################################################################
#
#   DESCRIPTION:
#
#
#       Uses Cocoa classes via PyObjC to set a desktop picture on all screens.
#       Tested on Mountain Lion and Mavericks. Inspired by Greg Neagle's work:
#
#           https://gist.github.com/gregneagle/6957826
#
#       Modified from @gramhamgilbert's script:
#
#       https://github.com/grahamgilbert/macscripts/blob/master/set_desktops/
#       set_desktops.py
#
#       Removed the ability to define a file path via argparse. Defines the
#       path to the image dynamically in the script. The image should live in
#       the same location where the script is being executed.
#
#       The onlything hardcoded in the script is the name of the image. In this
#       case the PICTURE_NAME is set to "stock_wallpaper.png"
#
#   SEE:
#
#       https://developer.apple.com/library/mac/documentation/cocoa/reference/
#       applicationkit/classes/NSWorkspace_Class/Reference/Reference.html
#
#       https://developer.apple.com/library/mac/documentation/Cocoa/Reference/
#       Foundation/Classes/NSURL_Class/Reference/Reference.html
#
#       https://developer.apple.com/library/mac/documentation/cocoa/reference/
#       applicationkit/classes/NSScreen_Class/Reference/Reference.html
#
###############################################################################


import sys
import os


from AppKit import NSWorkspace, NSScreen
from Foundation import NSURL

HERE = os.path.abspath(os.path.dirname(__file__))
PICTURE_NAME = "stock_wallpaper.png"

picture_path = os.path.join("/Library", "Desktop Pictures", PICTURE_NAME)


def verify_file_extension():
    """Verify that file extension is set to png"""
    if not PICTURE_NAME.endswith(".png"):
        print(
            "ERROR: Make sure that you are using a PNG file for your desktop "
            "image."
        )
        print("Picture Name: %s" % PICTURE_NAME)
        sys.exit(1)


def gen_file_url(path):
    """generate a fileURL for the desktop picture"""
    global file_url
    file_url = NSURL.fileURLWithPath_(picture_path)
    return file_url


def get_shared_workspace():
    """get shared workspace"""
    global ws
    ws = NSWorkspace.sharedWorkspace()
    return ws


def apply_desktop_wallpaper(ws, url):
    """Apply desktop wallpaper"""

    # make image options dictionary
    # we just make an empty one because the defaults are fine
    options = {}

    # iterate over all screens
    for screen in NSScreen.screens():
        # tell the workspace to set the desktop picture
        result = ws.setDesktopImageURL_forScreen_options_error_(
            file_url, screen, options, None
        )

        for item in result:

            if item is True:
                print("Wallpaper applied!")
                break

            elif item is False:
                print("Wallpaper NOT applied successfully ...")
                print(result)
                sys.exit(1)
            else:
                pass


def main():
    """The main event."""

    verify_file_extension()
    gen_file_url(path=picture_path)
    get_shared_workspace()
    apply_desktop_wallpaper(ws=ws, url=file_url)


if __name__ == "__main__":
    main()