Skip to content

Instantly share code, notes, and snippets.

@gregneagle
Last active July 24, 2024 09:28
Show Gist options
  • Select an option

  • Save gregneagle/6957826 to your computer and use it in GitHub Desktop.

Select an option

Save gregneagle/6957826 to your computer and use it in GitHub Desktop.

Revisions

  1. gregneagle revised this gist Oct 28, 2013. 1 changed file with 18 additions and 12 deletions.
    30 changes: 18 additions & 12 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,31 @@
    #!/usr/bin/python

    # 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
    '''Uses Cocoa classes via PyObjC to set a random desktop picture on all screens.
    Tested on Mountain Lion and Mavericks.
    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 glob
    import random

    from AppKit import NSWorkspace, NSScreen
    from AppKit import NSWorkspaceDesktopImageScalingKey
    from AppKit import NSImageScaleProportionallyUpOrDown
    from AppKit import NSWorkspaceDesktopImageAllowClippingKey
    from Foundation import NSURL

    picture_path = "/Library/Desktop Pictures/Beach.jpg"
    pictures_glob = glob.glob("/Library/Desktop Pictures/*.jpg")
    picture_path = random.choice(pictures_glob)

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

    # make image options dictionary
    options = {
    NSWorkspaceDesktopImageScalingKey: NSImageScaleProportionallyUpOrDown,
    NSWorkspaceDesktopImageAllowClippingKey: True
    }
    # we just make an empty one because the defaults are fine
    options = {}

    # get shared workspace
    ws = NSWorkspace.sharedWorkspace()
  2. gregneagle revised this gist Oct 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -27,5 +27,5 @@
    # iterate over all screens
    for screen in NSScreen.screens():
    # tell the workspace to set the desktop picture
    error = ws.setDesktopImageURL_forScreen_options_error_(
    (result, error) = ws.setDesktopImageURL_forScreen_options_error_(
    file_url, screen, options, None)
  3. gregneagle revised this gist Oct 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -27,5 +27,5 @@
    # iterate over all screens
    for screen in NSScreen.screens():
    # tell the workspace to set the desktop picture
    (result, error) = ws.setDesktopImageURL_forScreen_options_error_(
    error = ws.setDesktopImageURL_forScreen_options_error_(
    file_url, screen, options, None)
  4. gregneagle revised this gist Oct 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -27,5 +27,5 @@
    # iterate over all screens
    for screen in NSScreen.screens():
    # tell the workspace to set the desktop picture
    error = ws.setDesktopImageURL_forScreen_options_error_(
    (result, error) = ws.setDesktopImageURL_forScreen_options_error_(
    file_url, screen, options, None)
  5. gregneagle revised this gist Oct 14, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    #!/usr/bin/python

    # https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSWorkspace_Class/Reference/Reference.html#//apple_ref/doc/uid/20000391-BCIEBDGB
    # 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#//apple_ref/doc/c_ref/NSScreen
    # https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSScreen_Class/Reference/Reference.html

    from AppKit import NSWorkspace, NSScreen
    from AppKit import NSWorkspaceDesktopImageScalingKey
  6. gregneagle revised this gist Oct 13, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    #!/usr/bin/python

    # https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSWorkspace_Class/Reference/Reference.html#//apple_ref/doc/uid/20000391-BCIEBDGB
    # 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#//apple_ref/doc/c_ref/NSScreen

    from AppKit import NSWorkspace, NSScreen
    from AppKit import NSWorkspaceDesktopImageScalingKey
    from AppKit import NSImageScaleProportionallyUpOrDown
  7. gregneagle created this gist Oct 13, 2013.
    27 changes: 27 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/usr/bin/python

    from AppKit import NSWorkspace, NSScreen
    from AppKit import NSWorkspaceDesktopImageScalingKey
    from AppKit import NSImageScaleProportionallyUpOrDown
    from AppKit import NSWorkspaceDesktopImageAllowClippingKey
    from Foundation import NSURL

    picture_path = "/Library/Desktop Pictures/Beach.jpg"

    # generate a fileURL
    file_url = NSURL.fileURLWithPath_(picture_path)

    # make image options dictionary
    options = {
    NSWorkspaceDesktopImageScalingKey: NSImageScaleProportionallyUpOrDown,
    NSWorkspaceDesktopImageAllowClippingKey: True
    }

    # get shared workspace
    ws = NSWorkspace.sharedWorkspace()

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