Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Zoom

macrumors newbie
Original poster
Apr 12, 2004
27
2
I've been searching all over, and can't find code that actually works for this. My mom randomly rotates through ALL of her photos (from Photos app) on her desktop, and very often wants to know which pic she's looking at. I found some cool Applescripts that purport to do this, but it appears that what they actually do is get the first picture in the chosen list of images, not the currently displayed image.

Here's the script that gives all the relevant info (including - supposedly - the current image):

AppleScript:
tell application "System Events" to get properties of current desktop

And that gives some interesting info... but the "picture" value is wrong. It's in the right folder, but it's not the image currently being displayed. It's the first picture in the chosen image folder (alphabetically).

I'm a software engineer, but totally new to Applescript - and frankly, it mystifies me as a language. :)

Ideas?
 
This one liner works on High Sierra for the name of the static desktop picture selected in System Preferences.
But does NOT work for the changing desktop picture show for some reason.

AppleScript:
tell application "System Events" to set desktopPictureName to name of ((picture of current desktop as POSIX file) as alias)

I'm not sure the name of the image of changing desktop picture show can be accessed via AppleScript.
But I'll have a further play around and let you know of my findings.

Regards Mark
 
Last edited:
After a bit of experimenting, I've figured out a way of achieving the task, and contradicting myself at the same time.

It involves checking with a shell script the most recently modified and accessed file in the desktop pictures folder.
The shell script command is simply "ls -tu".
So incorporating the shell command into an AppleScript, you can get the name of the most recently accessed picture.

AppleScript:
tell application "System Events" to set desktopPicturesFolder to pictures folder of current desktop
set desktopPicturePath to desktopPicturesFolder & first paragraph of (do shell script "ls -tu " & quoted form of desktopPicturesFolder)
tell application "System Events" to set desktopPictureName to name of ((desktopPicturePath as POSIX file) as alias)
display dialog "The current Desktop picture file name is " & return & return & desktopPictureName buttons {"OK"}

OR broken down to one tell block.

AppleScript:
tell application "System Events"
    set desktopPicturesFolder to pictures folder of current desktop
    set desktopPicturePath to desktopPicturesFolder & first paragraph of (do shell script "ls -tu " & quoted form of desktopPicturesFolder)
    set desktopPictureName to name of ((desktopPicturePath as POSIX file) as alias)
    display dialog "The current Desktop picture file name is " & return & return & desktopPictureName buttons {"OK"}
end tell

you would of course have to repeatedly run the AppleScript on a timer.
I posted recently on how to use a "on Idle" handler in a stay open AppleScript application here.
https://forums.macrumors.com/threads/running-shell-script-every-30min-with-automator.2281446/

So you can simply place the above code into the "on idle" handler, and return the number of seconds to repeat.

Hope this is helpful

Regards Mark
 
Last edited:
Thanks, Mark! I don't need this to run repeatedly - on demand is fine.

I'm getting an error for this on Big Sur:

error "Can’t make file \"Macintosh HD:Users:carey:pictures:Desktop Pix03036_emeraldmorainelake_3840x2160.jpg\" into type alias." number -1700 from file "Macintosh HD:Users:carey:pictures:Desktop Pix03036_emeraldmorainelake_3840x2160.jpg" to alias

I'll play around with it later, see if I can come at that from another angle. But FYI, the script I'm copying has a nifty output mechanism that I'll be using. It pops up a dialog to show you the answer and lets you copy the image name or open it.

AppleScript:
tell application "Finder"
    set dp to get desktop picture of application "Finder"
    set dpn to name of dp
    set rep to display dialog "The current Desktop Picture is named " & return & "\"" & dpn & "\"" buttons {"Copy to Clipboard", "Open File", "OK"} default button 3
    if button returned of rep is "Copy to Clipboard" then set the clipboard to dpn
    if button returned of rep is "Open File" then open dp
end tell
 
I tested my script on both "High Sierra" and "Mojave" and it worked fine.
Buy I have no way of testing it on "Big Sur", so clearly Apple has made yet more AppleScript changes.

You probably figured out the the error is caused by this line on "Big Sur".

AppleScript:
set desktopPictureName to name of ((desktopPicturePath as POSIX file) as alias)

You could try changing it to this.

AppleScript:
set desktopPictureName to name of (file desktopPicturePath as alias)

But I'm only guessing at this point what might work on "Big Sur".
I would be happy to help further with your script, but the OS differences might make it hard.

Maybe someone else on the forum using "Big Sur" might be able to help testing this code.

Regards Mark
 
Screen Shot 2021-01-31 at 12.28.19 PM.png
 
Here is another "Big Sur" guess which works fine on "Mojave" here.

AppleScript:
set AppleScript's text item delimiters to "/"
tell application "System Events"
    set desktopPicturesFolder to pictures folder of current desktop
    set desktopPicturePath to desktopPicturesFolder & first paragraph of (do shell script "ls -tu " & quoted form of desktopPicturesFolder)
    set desktopPicturePathComponents to every text item of desktopPicturePath
    set AppleScript's text item delimiters to ":"
    set desktopPictureAlias to (desktopPicturePathComponents as text) as alias
    set desktopPictureName to name of desktopPictureAlias
    display dialog "The current Desktop picture file name is " & return & return & desktopPictureName buttons {"OK"}
end tell

This should work by using AppleScripts text item delimiters break up the file path "/" forward slashes.
And then rebuilding the file path with ":" HFS delimiters, which make an alias file path format.

Although I just can't believe that Apple have stopped the ability to convert posix file paths into an alias types.
Which is something we've been able to do forever in AppleScript.

Mark
 
Still fails. Sorry, Mark... don't kill yourself here. :) When I get done with this other thing I'm doing, I can focus more on this. For now, I'm just trying and pasting the results, so not being much help.

Code:
error "Can’t make \":Users:carey:Pictures:Desktop Pix03036_emeraldmorainelake_3840x2160.jpg\" into type alias." number -1700 from ":Users:carey:Pictures:Desktop Pix03036_emeraldmorainelake_3840x2160.jpg" to alias
 
Yeah! it seems everything that works on my "Mojave" just doesn't seem to work on "Big Sur".
So it looks like you'll have to find out how to convert posix style paths into an AppleScript alias type on "Big Sur".

Or there is just one more thing you can try, which doesn't require "System Events"
If this doesn't work on "Big Sur", then they really have screwed up AppleScript on "Big Sur"

AppleScript:
set AppleScript's text item delimiters to "/"
set desktopPicturesFolder to POSIX path of (path to desktop pictures folder)
set desktopPicturePath to desktopPicturesFolder & first paragraph of (do shell script "ls -tu " & quoted form of desktopPicturesFolder)
set desktopPicturePathComponents to every text item of desktopPicturePath
set desktopPictureName to last item of desktopPicturePathComponents

But this method won't help you further on when you will need to tell "Finder" to open the image file.
Because the "Finder" will need to be passed the file as an alias type.

Regards Mark
 
FYI... just tested the "ls -tu" thing, and it actually isn't giving me the current desktop, anyway. So we're back to square one, unfortunately. This works for you?
 
Well if my last posting didn't work for you, then the differences between "Big Sur" and my "Mojave" are so great.
As much as I would like to help you further, it would probably be a waste of time, such are the differences in the OS's.

But this is my working tested on Mojave script, which as it stands will not work for you, but might give you some ideas.
It includes the dialog box to open or paste the image to the clipboard as well, but I suspect probably won't work for you either.

AppleScript:
set AppleScript's text item delimiters to "/"
set desktopPicturesFolder to POSIX path of (path to desktop pictures folder) as text
set desktopPicturePath to desktopPicturesFolder & first paragraph of (do shell script "ls -tu " & quoted form of desktopPicturesFolder)
set desktopPicturePathComponents to every text item of desktopPicturePath
set desktopPictureName to last item of desktopPicturePathComponents

set dialogResponse to display dialog "The current Desktop picture file name is " & return & return & desktopPictureName buttons {"Open", "Clipboard", "OK"} default button "OK" with title "Desktop Picture Action Utility" with icon note giving up after 30

if button returned of dialogResponse is equal to "Open" then
    set desktopPictureAlias to POSIX file desktopPicturePath as alias -- This convert to alias line like the others might not work for you.
    tell application "Finder"
        open file desktopPictureAlias
    end tell
else if button returned of dialogResponse is equal to "Clipboard" then
    set the clipboard to (read desktopPicturePath as JPEG picture)
    set alertResponse to display alert "Copied the " & desktopPictureName & " image file picture data to the clipboard." buttons {"OK"}
    if button returned of alertResponse is equal to "OK" then
        return clipboard info
    end if
else if button returned of dialogResponse is equal to "OK" then
    return
end if

Hopefully one of the other forum members familiar with AppleScript and running "Big Sur", will jump in to help you further, with how to make your ideas work on the newer OS.

Good Luck with the project.

Regards Mark
 
Again - thank you so much for your efforts here, Mark!!
 
Did you ever figure out how to get the correct response on Big Sur? This is the most detailed discussion of the topic I've found, but still without a solution. I had an Applescript that could identify the image used on a given workspace under prior MacOS versions, but it no longer provides the correct file name under Big Sur. I don't need a script to change the wallpaper (I use the built-in function under System Preferences to randomly select an image in a specified folder to do that). I just need a script that can tell me the name of the current image on the current workspace.

I was hoping one of the members proficient with shell commands and/or Applescript would have chimed in with a fix by now. Very disappointing...
 
Last edited:
You can find some info in the desktoppicture.db file located in your Library --> Application Support --> Dock folder.
Screenshot 2021-12-09 at 20.44.32.png
 
You can find some info in the desktoppicture.db file located in your Library --> Application Support --> Dock folder.View attachment 1925746
Thanks for the response. Can you dumb it down a bit for me? I can handle simple bash scripts and AppleScripts, but I'm not familiar with reading MacOs databases. I can see you're using sqlite, but I have no experience with that software.
 
The only issue I found with the script in reply #3 is that the path from getting the pictures folder does not have a trailing delimiter - this result is the same in my Big Sur system, and also Sierra. Without adding a slash between the folder and the file name returned from the shell script, the final path doesn’t exist to get a name from (as indicated by the posted errors).
 
@Red Menace

I did not continue responding to this thread because of the operating system differences.
But it can also be done with AppleScriptObjC, which I know you are also familiar with.
Which is also more likely to be the more reliable way going forward with new Mac OS's.

So this code below will also open the desktop image file in the Finder's default application.
Which again works fine on "Mojave", but I don't know if it does on "Big Sur", so maybe you can test it for the new OP.

AppleScript:
use framework "Foundation"
use scripting additions

property myApp : a reference to current application

set workspace to myApp's NSWorkspace's sharedWorkspace()

set desktopImageURL to workspace's desktopImageURLForScreen:(myApp's NSScreen's mainScreen())

set openedURL to workspace's openURL:desktopImageURL

if openedURL then
    "Success opening file at URL " & (desktopImageURL's |path|())
else
    "Failure opening file at URL " & (desktopImageURL's |path|())
end if

There are other "NSWorkspace" functions that can also be used to open the URL in specific application's.

Regards Mark
 
Last edited:
@Mark FX

That ASObjC script also works in Big Sur, although like in your original script the returned folder path does not include a trailing slash (oddly enough, coercing the URL to text instead does keep the trailing delimiter). The missing delimiter just needs to be inserted when adding more items to the path.
 
@Red Menace

Perhaps if mac-mike needs help converting it to their needs, you could help them out as I don't like posting code I can't test on the correct operating system, it only leads to problems.
As this forum posting proves.

Regards Mark
 
@Red Menace

I did not continue responding to this thread because of the operating system differences.
But it can also be done with AppleScriptObjC, which I know you are also familiar with.
Which is also more likely to be the more reliable way going forward with new Mac OS's.

So this code below will also open the desktop image file in the Finder's default application.
Which again works fine on "Mojave", but I don't know if it does on "Big Sur", so maybe you can test it for the new OP.

AppleScript:
use framework "Foundation"
use scripting additions

property myApp : a reference to current application

set workspace to myApp's NSWorkspace's sharedWorkspace()

set desktopImageURL to workspace's desktopImageURLForScreen:(myApp's NSScreen's mainScreen())

set openedURL to workspace's openURL:desktopImageURL

if openedURL then
    "Success opening file at URL " & (desktopImageURL's |path|())
else
    "Failure opening file at URL " & (desktopImageURL's |path|())
end if

There are other "NSWorkspace" functions that can also be used to open the URL in specific application's.

Regards Mark
The script runs without errors on Big Sur, and Finder opens the correct folder, but the actual image is not opened. Nor identified as far as I can tell.

Also, when attempting to save this script, an error is generated:
Screen Shot 2021-12-10 at 4.03.48 PM.png
 
Last edited:
Recompile and try to save the script again. A quick test with Mark FX's script produces some funky output on my end when randomly choosing a picture. Looking at mac-mike's result that also seems to point to a folder URL.
Screenshot 2021-12-11 at 12.30.27.png


Screenshot 2021-12-11 at 12.30.54.png
 
Last edited:
Trying to get the current desktop picture is weird, no matter what you do. Using System Events results in the default picture, not necessarily what is being shown. Using NSWorkspace, the desktopImageURLForScreen is actually the selection in the preference, so if you happen to be using random items from a folder, you just get the specified folder. Using the file access date is also problematic, since you can get the default picture even if it isn’t being used. Even the desktop debug menu gets it wrong, and that is provided by Apple.

The best approach seems to be to use the SQL database, but that doesn’t always seem to get it right, either - it doesn’t help that what is in there is pretty cryptic. It would appear there is a reason why there aren’t any ready solutions to be found out there.
 
mac-mike said:
Also, when attempting to save this script, an error is generated:

The posted code is AppleScriptObjC code, and not plain vanilla AppleScript.
So you can't save it as a script with a ".scpt" file extension, you can only save it as an application or text.
When you go to the "Save" menu option, and the save dialog box appears, select the Application or Text option from the "File Format:" combo box.

Secondly as previously stated, this code may not work as expected on "Big Sur", as I can only test here on "Mojave".
So my posting was aimed at Red Menace who knows AppleScriptObjC and has "Big Sur" to test with, so that he could come up with a solution for you.

Regards Mark
 
@Red Menace

After OSX, Apple seems to have changed something in the way the desktop picture is referenced.
So I realised I was not going to be able to solve the problem from my OSX operating system version.
But good luck with finding a solution on "Big Sur".

Regards Mark
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.