Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Get the name of the current desktop image Desktop
I was replying to a question in the forums when I realized that this would make a great tip for the main site. Just like the original poster, I have a lot of desktop images, and sometimes I can't remember the name of the one that's currently displayed when I want to edit it (or trash it). So here's the AppleScript that I came up with to solve that problem...
tell application "System Events"
  set my_desktop to value of (property list item "LastName" of 
  property list item "1983938400" 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
This script looks in the com.apple.desktop.plist file in your user's Library/Preferences folder for the name of the current desktop image. On my Mac running 10.4.5, there are actually several plist items under Background, each of which contain entries for desktop images that are not being displayed, so it took some poking around to find the correct path.

Apparently, there are some variations in the way that this .plist file looks. For this to work on your own system, you should open your own com.apple.desktop.plist file with the Property List Editor.app, and see where the value for your current desktop image is kept. Then change the value in the script (1983938400 in the above code) to match the value of the appropriate LastName entry for your current desktop image.

I have my Mac set to cycle through the images kept in a single "Screens," folder and I have double-checked that it will snag the name of the file again after the image has cycled a few times. This also works with two-monitor setups. I'll leave the modification for two monitors to your own ingenuity.
    •    
  • Currently 3.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[13,530 views]  

Get the name of the current desktop image | 17 comments | Create New Account
Click here to return to the 'Get the name of the current desktop image' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Get the name of the current desktop image
Authored by: Volt on May 10, '06 09:16:18AM

This is a great idea. I have hundreds of desktop images on cycle, and if someone sees my desktop image and asks for it, I'd have to otherwise dig through all my images to find that specific one.

I actually modified the script to change the "1983938400" to "default", and it seems to be working on 10.4.6.



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: JimMueller on May 10, '06 01:33:50PM

Is this hint for Tiger only?

I tried this code in Script Editor on my 10.3.9 system and got a systax error: "Expected expression, ")", etc. but found "property"" and it's been many, many years since I dabbled in AppleScript.

Do you know if this will display the names of all images on multiple monitors?



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: 6502 on May 10, '06 06:18:29PM

> Is this hint for Tiger only?

I think it will work in earlier OSes, but I haven't tried it in 10.3x.


> I tried this code in Script Editor on my 10.3.9 system and got a
> systax error: "Expected expression, ")", etc. but found
> "property"" and it's been many, many years since I dabbled in
> AppleScript.

System Events should be scriptable in 10.3. You can double check that the property list commands are accessible in your Script Editor application by using the Open Dictionary command -- select System Events.app in the resulting list and you should find an entry for the Property List Suite. If it's there then there's no good reason for the script not to work.

Double-check that your line-breaks are in the right places. The whole thing from "set my_desktop" to the double-parentheses is one line unless you use the Option-Return character () at each line break.


> Do you know if this will display the names of all images on multiple monitors?

It will, but you have to figure out which property list item represents your second desktop picture. In the example, the property list item "1983938400" is the one that holds the location of the desktop picture for the main monitor. You should be able to find the appropriate property list item if you look through the com.apple.desktop.plist file in the Property List Editor (which you may be able to do just by double-clicking the .plist file).

Once you know where to find the entry for the second monitor, it's just a matter of substitution. So let's suppose the plist item containing the name of the second monitor's desktop picture was "552268546" -- you'd add this code to the script...

set my_desktop2 to value of (property list item "LastName" of
property list item "552268546" of property list item "Background"
of property list file ((path to preferences as Unicode text) &
"com.apple.desktop.plist"))

...and then you'd change the last line to...

display dialog (my_desktop & ", " & my_desktop2)



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: JimMueller on May 11, '06 10:43:45AM

> select System Events.app in the resulting list and you should find
> an entry for the Property List Suite. If it's there then there's no
> good reason for the script not to work.

Standard Suite
Text Suite
Accounts Suite
Login Items Suite
Disk-Folder-File Suite
Folder Actions Suite
Power Suite
Process Suite
System Events Suite

Nope - no Property List Suite in 10.3.9

Yet another reason to kick this booger up to Tiger...



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: fkfkkfkfkfk on May 10, '06 03:31:32PM

defaults read com.apple.desktop !



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: 6502 on May 11, '06 01:02:01AM
I always enjoy a little experimentation, so as was suggested, I've been fooling with defaults read and I can't get the output that I'd like -- I can't simply pull up the name of the current picture under Tiger.
defaults read com.apple.desktop
...produces the same results as...
defaults read com.apple.desktop Background 1983938400 LastName
...which is a dump of the whole file. Either I'm doing it wrong or else there's a bug that prevents it from reading nested items. This is an alternative...
defaults read com.apple.desktop | grep LastName
...but as I indicated earlier, in my own .plist file, there's a lot of junk, including entries for desktop images that are NOT the one being displayed on my monitor. So when I grep for LastName, I get 11 results rather than one. Any ideas?

[ Reply to This | # ]
Get the name of the current desktop image
Authored by: beauh on May 11, '06 01:17:21AM

[code]defaults read com.apple.desktop | grep -A 10 default | grep ImageFilePath | awk -F= '{print $2}' | sed -e 's/";/"/'[/code]



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: 6502 on May 11, '06 04:46:42AM
Thanks! That was helpful, but when I ran it, the result was the path to an image that was NOT being displayed as my desktop image. However, both of these worked to produce the same results as the AppleScript...
defaults read com.apple.desktop | grep -A 10 1983938400 | grep LastName | awk -F= '{print $2}' | sed -e 's/";/"/'

defaults read com.apple.desktop | grep -A 10 default | grep LastName | awk -F= '{print $2}' | sed -e 's/";/"/'
...and this worked to find the name of the image on my second monitor...
defaults read com.apple.desktop | grep -A 10 552268546 | grep LastName | awk -F= '{print $2}' | sed -e 's/";/"/'
Once again, I think it's important to stress that the way to find the correct property list item is to browse through the .plist file and to use the LastName property. I think that if you've only ever displayed one image as your desktop image since your last clean install, the ImageFilePath field in the default property list entry is going to match the file referenced in the LastName field, but if you are randomizing your screens, they are going to be different. Also, if you are anything like me where I haven't done a clean install on this particular Mac since 10.2, the .plist file is going to have a lot of obsolete and redundant entries and the ImageFilePath won't have any relationship at all to the present location of your image files. So, anyway... this is the AppleScript that I derived from your code...
set my_desktop to (do shell script ("defaults read com.apple.desktop | grep -A 10 1983938400 | grep LastName | awk -F= '{print $2}' | sed -e 's/\";/\"/'")) as text

set my_desktop2 to (do shell script ("defaults read com.apple.desktop | grep -A 10 552268546 | grep LastName | awk -F= '{print $2}' | sed -e 's/\";/\"/'")) as text

display dialog my_desktop & " • " & my_desktop2
...where "1983938400" and "552268546" are the .plist items that store the names of my current desktop images.

[ Reply to This | # ]
Get the name of the current desktop image
Authored by: robJ on May 11, '06 06:49:51AM
I have only one monitor so I don't know how this works on a multi-monitor setup but this works fine on my PowerBook running 10.4.6.
tell application "Finder" to 
	set dp to displayed name of (get desktop picture)
display dialog "Desktop Picture Name: " & dp buttons 
	{"OK"} default button 1 giving up after 10


[ Reply to This | # ]
Get the name of the current desktop image
Authored by: 6502 on May 12, '06 02:26:16PM
> I have only one monitor so I don't know how this works on a
> multi-monitor setup but this works fine on my PowerBook running 10.4.6.

On my multi-monitor setup under 10.4.5, it produces an error, "Can't get displayed name of missing value..."

When I tried this,
tell application "Finder" to get desktop picture
...the result was the string, "missing value" so it seems that it's literally trying to get the name of a string "missing value."

I guess having two monitors makes a big difference. Or else Apple changed something in 10.4.6.

[ Reply to This | # ]
Get the name of the current desktop image
Authored by: kostia on May 11, '06 06:14:55PM

The simpler version (RobJ's) of the script works fine for me, but the original hint doesn't. I do have two monitors, so I want to use the slightly more complex version.

The script compiles but on run I get the error NSReceiverEvaluationScriptError: 4, with the first instance of the word "value" highlighted.

Any ideas?



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: 6502 on May 11, '06 07:58:38PM

> Any ideas?

Try this version...
http://www.macosxhints.com/comment.php?mode=display&sid=20060507041822864&type=article&order=&pid=74635



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: kostia on May 12, '06 01:19:51PM

I read that, but I don't understand how it relates to scripting. How exactly do I get the Applescript to perform the grep and all the stuff it's piped into, then do something with that result?



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: kostia on May 12, '06 01:21:18PM

Never mind, I clearly misread that post or mistook it for another one. I didn't realize the second chunk of code was Applescript and not command-line entry.

Ignore my other comment. Thanks!



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: virius on May 13, '06 01:02:51PM
Full service solution:

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
It displays the name, offers to copy it to clipboard, or to open it.
AppleScript is great for stuff like this...

[ Reply to This | # ]
Get the name of the current desktop image
Authored by: JimMueller on May 14, '06 07:15:22PM

Very elegant implementation, and at least it runs in Panther (though it does not copy the filename to the clipboard), but it is not giving the filename of the current rotating desktop.

When I run it in Tiger it only displays the filename of the last desktop I had directly selected.



[ Reply to This | # ]
Get the name of the current desktop image
Authored by: 8008135 on Sep 16, '06 02:52:29PM
i'm a complete noob to code but i managed to update this script a little... i found that 'open' wasn't working because it didn't include address for the file. i think that with further modifications this could be made into a useful image sorting utility. just link the shell script 'cp' call to a hotkey, and set the changetime variable in the desktop plist file to a low value. meaning that when gathering images together you could just slideshow your way through a folder tapping a hotkey everytime you see a picture that you need.

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"))
	
	set folder_path to value of (property list item "ChooseFolderPath" of 
		property list item "default" of property list item 
		"Background" of property list file 
		((path to preferences as Unicode text) & 
			"com.apple.desktop.plist"))
	
	set rep to display dialog "The current Desktop Picture is named " & return & """ & my_desktop & """ buttons {"Open File", "Copy to Clipboard", "Copy to Desktop"} default button 3
	set finder_path to folder_path & "/" & my_desktop
	if button returned of rep is "Copy to Clipboard" then set the clipboard to my_desktop
	if button returned of rep is "Open File" then open finder_path
	if button returned of rep is "Copy to Desktop" then do shell script "cp " & finder_path & " ~/desktop"
end tell


[ Reply to This | # ]