System Events result to variable

Hi everyone-

I’m relatively new to programming, but learn quick. What I’d like to do is create a small app that will poll the ~/library/preferences/com.apple.desktop.plist file to see what the currently set desktop picture’s name is (ImageFilePath attribute in the default section)

I then want to display that file name in a display dialog.

I think this close to the code that will return the value that I’m looking for:

tell application "System Events" to return the value of property list item ¬
"ImageFilePath" of property list file ((path of preferences folder) & "com.apple.desktop.plist")

However I get a “System Events got an error: NSReceiverEvaluationScriptError: 4” when I run it and the word “value” is highlighted.

First, how to I resolve the error above and second, how to I pass this into a variable so that I can then display the result?

Thank you!!!

Jared

I don’t know if this’ll work, and I can’t test it, as I don’t have the dev tools on this computer, but try something like this…


Tell user defaults of application "Finder"
set var1 to (contents of default entry ImageFilePath) as string
end tell

However, I’m not too sure you can (easilly) access the user defaults of another application. This is just a shot in the dark.

-Parrot

desktop picture is an AppleScript constant and like any file you can get its properties.

tell application "Finder" to set DP to {name, container} of (desktop picture as alias)

Sweet!!! Is there a place to look for other kinds of constants? I imagine that they’re incredibly useful.

Thanks!

-jared

UPDATE

I’ve got a built and running application. However (there’s always a “however”) it doesn’t quite work right. My problem (and I likely should have mentioned this earlier) is that my desktop picture rotates through random pictures that are in a folder. The whole reason that I wanted this app was so that I could see what the name of the current picture was as they’re all of places in Germany and the filenames are as such.

Here’s my code:

on clicked theObject
	set DP to ""
	set MyText to ""
	
	tell application "Finder" to set DP to {name} of (desktop picture as alias)
	
	set MyText to (DP as string)
	
	set the contents of text field "textfield" of window "main" to MyText
	
end clicked


What I’ve noticed is that if you open up ~/library/preferences/com.apple.desktop.plist and then go into Root/Background/default the key that the above code is pulling from is ImageFilePath. This key is an apparent reference to the last picture that was explicitly selected as my desktop picture (before I turned on the rotating picture feature). The key that is accurate, and I can see change each time the desktop picture changes is Root/Background/default/LastName. Now, is there a way to grab this and drop it into my DP variable? I’m going to try to go back to the System Events app and use the plist functions there. Any ideas on that?

Thanks!

Jared

Hi Everyone-

I just love when I’m the biggest poster to my own thread :slight_smile:

I figured it out!

So I changed up my code a bit. Now it is:


on clicked theObject

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

set the contents of text field "textfield" of window "main" to DP

end clicked


(BTW, there are soft returns at the end of the lines in the tell block above - Option-Return)

It works great!

Jared