macOS Automation (with JavaScript)

While pondering some options for automating stuff on macOS, I decided to take a look at the Objective-C JavaScript bridge. These are cursory notes on various odds and ends, without any real structure.

Retrieving the Current Wallpaper

Run the below with osascript -l Javascript -i:

// Import the bridge
ObjC.import('Cocoa');

// Get a shared workspace
sw = $.NSWorkspace.sharedWorkspace;

// Get the first screen (yes, this is an NSArray)
screen = $.NSScreen.screens.firstObject;

// Log the pathname (NSURL) out
$.NSLog(sw.desktopImageURLForScreen(screen).absoluteString)

If the above is a folder, you have to go into the Dock’s desktoppicture.db database1 and take a look at what’s there in the data table.

Since the JavaScript bridge seems to lack SQLite bindings, I haven’t yet had the time to pursue this in depth, other than the data table doesn’t seem to keep track of what wallpaper is on what display if you have multiple monitors (although I suspect that the last entry is always for the primary display).


  1. ~/Library/Application\ Support/Dock/desktoppicture.db ↩︎