Mojave Current Desktop Image

Back in the day…I had an AppleScript that would tell you the current desktop image…sometimes when one of my 3000+ desktop images was up I either wondered “where is that” or “I need to get rid of that one”…and the script told me what it was.

Alas…it no longer works under Mojave…and googling revealed no great solution to get it. Anybody know how to do that?

If you shared the AppleScript code, maybe someone would have an idea of why it’s not working on Mojave.

Managing the Desktop Picture on macOS is mainly for Mac administrators but it includes a command line program, desktoppr, for reading (or setting) the desktop picture (if you have the command line tools installed, you can add #!/usr/bin/swift to the top of the source code and run it as a script instead of using the compiled version).

Setting The Desktop Image In Macos Mojave From The Command Line details how the contents of desktoppicture.db changed in Mojave due to the addition of dynamic desktop pictures. It links to a Bash script the author wrote for setting the image but it’s probably a less useful reference because it seems to write entirely new SQLite in the desktoppicture.db to make the change. My guess is your AppleScript wasn’t running shell commands to dump fields using SQLite.

This is the code in the current script…it quit working about 2-3 macOS versions back but periodically I try to use it and go looking for a new script that works.

Could it have quit working more than 2-3 macOS versions ago? Could it have worked on Mountain Lion 10.8 but not in Mavericks 10.9? According to the second link I provided, Mavericks is when the file path information started being stored in a SQLite database instead of within a Plist file.

Was there supposed to be code included in your message? I don’t see any. Either put the text of the code in a separate text file and share that with a link from some personal cloud storage (iCloud, Dropbox, Google Drive, etc.) or use Discourse’s web interface to include it in the body of your reply. Specifically, in Discourse’s WYSIWYG editor, have a blank line, then a line with four backticks, then another blank line, another line with four backticks, then paste the code on the line between the lines of backticks. The lines of backticks will tell Discourse that the text in-between is code and should be formatted as such; it won’t have all of Script Editor’s same colors for syntax but it will be more readable than it would be otherwise.

Yeah…certainly could have. I dug through my old applescripts folder and found a version of the script with. Mavericks in the name so that’s certainly a god possibility.

The code was in the post when I sent it…copied from Script Editor but maybe it got stripped out somehow by Discourse since it was “code". Here it is again after running it through a text file copy/paste cycle to get any script sense stripped out of it. Hopefully it will come through this time.

tell application "System Events"
    set my_desktop to value of (property list item "LastName" of property list item "default" of property list item "Background" of property list file ((path to preferences as Unicode text) & "com.apple.desktop.plist"))
    display dialog my_desktop
end tell

If Mavericks is where it went into the SQLlite database…then I guess there’s no getting it easily anymore. I did find another script that will add and remove the current desktop picture…but it just adds and deletes the following plist line…com.apple.dock desktop-picture-show-debug-text…but then it runs a killall Dock to get it to take effect and that rotates the picture which means the name only shows up on the next one in the random order selection.

Hey Neil,

Sending email to a Discourse forum is very hit or miss.

As much as I prefer working from an email client when managing forums/mailing-lists, I always compose my posts for Discourse in the forum editor – so I can see what the final product will look like. If you don’t you tend to get bit eventually.

Code in Discourse needs to be fenced. (I don’t know if you can send it in that way via an email.)

The code button </> in the editor does a 4-space indent, but fencing is better and allows for a syntax keyword.

Here’s what the submission should look like:

```applescript
tell application "System Events"
   tell current desktop
      set desktopPicturePath to picture
   end tell
end tell
```

Here’s how that renders on the forum:

tell application "System Events"
   tell current desktop
      set desktopPicturePath to picture
   end tell
end tell

Please try this code and see if it works for you in Mojave.

-Chris

Thanks for the advice on script formatting, Chris. I’ve edited Neil’s post to include it. (It’s quite neat that Discourse can pretty-print AppleScript syntax like that.)

1 Like

Installing the desktoppr application I referenced before is the key, it’s way easier than trying to write something that will read the SQLite file directly.

  1. Install the desktoppr package
  2. Open Automator and choose to create a Quick Action
  3. Set the Workflow to receive “no input” in "any application
  4. find and drag over Run Shell Script and enter /usr/local/bin/desktoppr in the field
  5. find and drag over Set Value of Variable and create a new variable, perhaps call it “Pictures”
  6. find and drag over Ask for Confirmation and within the Messages field, start typing the name of the variable; when it offers the variable name, press the Return key so it’s filled in. The variable name should have rounded corners and a downward pointing carat; if it’s just plain text, delete it and try again.
  7. Click File > Save and give the Quick Action a name, like “Desktop Pictures.”

It will save the result in ~/Library/Services/ as a .workflow file. To use it, go to any application menu > Services and click the name of the Quick Action, the full file path to the desktop picture (or pictures if you have more than one display) will appear in a dialog and you can click the Cancel or OK buttons to dismiss it (pressing the Esc or Return keys also works).

If you don’t want it in the Services menu, create it as an Automator application instead and run the application to get the same output. If you want to stick with AppleScript, here you go:

tell application "System Events"
	set my_desktop to (do shell script "/usr/local/bin/desktoppr")
	display dialog my_desktop
end tell

Hi Curtis…thanks for the assist…didn’t get back to looking at this until today. I installed desktoppr and tried both the suggested Automator action as well as the AppleScript. Both of them run properly…but instead of giving me the full path to the current desktop image they return the full path to the folder I’ve got selected in System Preferences for desktop images. Running desktoppr manually from a terminal window in /usr/bin gives the same result…the folder, not the current image path.

I do not have sufficient coding skills to know what needs changing…actually I have few to no coding skills beyond using snippets of code others have written and changing paths, variables, and such inside other folks code.

Sorry about that, since I have a single picture selected and don’t have “Change Picture” enabled, the current picture and what System Preferences shows is one in the same. It also seems that everything that works through AppleScript also reads that value.

However, desktoppicture.db file is updated every time the actual display picture changes. When “Change picture” is in effect, something deletes a table row with the current picture and adds a new row with the new picture. Therefore, the highest rowid in that table belongs to the the displayed picture.

The following AppleScript runs a shell script to query the database and get back the most recently added filename. If you have more than one screen you can change the number after “LIMIT” but if not all screens have “Change picture” enabled, the output might not always be accurate, especially if a screen has one of the defaults selected.

tell application "System Events"
	-- change number after LIMIT to match number of screens
	set query to "SELECT * FROM data ORDER BY rowid DESC LIMIT 1;"
	set support_folder to path to application support from user domain
	set db_file to POSIX path of support_folder & "/Dock/desktoppicture.db"
	set current_image to (do shell script "sqlite3 " & quoted form of db_file & " " & quoted form of query)
	display dialog current_image
end tell

Thanks Curtis…that script works perfectly and does what I’m looking to do.

1 Like