Skip to content

Instantly share code, notes, and snippets.

@hafta
Created April 22, 2020 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hafta/8efc203d33fea574959152111343a554 to your computer and use it in GitHub Desktop.
Save hafta/8efc203d33fea574959152111343a554 to your computer and use it in GitHub Desktop.
Python script to print the path to the image used as the desktop background of the focussed screen
#!/usr/bin/python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/. */
from AppKit import NSScreen, NSWorkspace
def main():
focussedScreen = NSScreen.mainScreen()
if not focussedScreen:
raise RuntimeError("mainScreen error")
workspace = NSWorkspace.sharedWorkspace()
if not workspace:
raise RuntimeError("sharedWorkspace error")
imageURL = workspace.desktopImageURLForScreen_(focussedScreen)
if not imageURL:
raise RuntimeError("desktopImageURLForScreen returned invalid URL")
if not imageURL.isFileURL():
raise RuntimeError("desktop image URL is not a file URL")
print(imageURL.path())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment