change background picture.

I am trying to make an application that changes the background every time the application is run. However, I run my current app and nothing happens. What I am I doing wrong? Here is what I have:

tell application "Finder"
	set desktop picture to file "Users/Administrator/Desktop/3g-apple-iphone5.jpg"
end tell

P.S. I googled to see what I could find, and nothing I tried that was what I was looking for worked. So i need help! SO PLEASE HELP.

Thank you for helping!

Best Regards,

Ben

no one knows???

Hi,

AppleScript doesn’t work with POSIX paths


tell application "Finder"
	set desktop picture to file "Users:Administrator:Desktop:3g-apple-iphone5.jpg" of startup disk
end tell

That used to be the case, but it’s not so clear-cut now. All the standard additions commands that list “file” as a parameter will, as of 10.6, accept a POSIX path (or even an HFS path). The same applies for commands in apps using Cocoa scripting. The Finder is not one of these, though.

I know, Shane, but as AppleScript is a “black hole” of exceptions and special cases,
it’s clearer for the casual scripters to consider the simple rule

AppleScript → HFS paths
UNIX (shell) → POSIX paths

I agree. I guess I was musing on how long that will be the case…

Not even that seems to work. And I copied and pasted it right into the code.

THank you for helping.

Benjamin

Try


tell application "Finder"
	set desktop picture to file "Users:Administrator:Desktop:3g-apple-iphone5.jpg"  of startup disk
	update desktop
end tell

still not working :(.

Do you think I should just start over? Maybe do more research? It doesn’t seem that hard, though.

The script appears to be correct, but it doesn’t work for me either. I had more success with this:

set p2dpf to (path to desktop as text) & "3g-apple-iphone5.jpg"

tell application "System Events"
	tell current desktop
		set picture rotation to 0 -- 0 = never.
		set picture to file p2dpf
	end tell
end tell

It worked!! :D:D:D:D:D:D:D

Thank you so much Nigel Garvey.

Best Regards,

Benjamin

i have my pic in my pictures folder now, what do I have to change to set the pic to be the pic in my pics folder? If my pics title has spaces, do I need to replace it with underscores? Please help.

Best Regards,

Benjamin

Nevermind. I got it working! But how do you make picture “Fit to Screen” rather than “Fill Screen” in the code? Thanks!

This seems to work in 10.6.6. There’s probably a more economical way to cover the possibilities than with four “sed” commands, but I don’t know it:

set p2dpf to (path to desktop as text) & "3g-apple-iphone5.jpg"

tell application "System Events"
	tell current desktop
		set picture rotation to 0 -- 0 = never.
		set picture to file p2dpf
	end tell
end tell

-- This shell script reads the Desktop picture preferences file ("com.apple.desktop.plist"),
-- replaces any of the other possible "Placement" settings with "SizeToFit",
-- writes the edited preferences back to the file,
-- and kills the Dock process so that it restarts with the new settings.
do shell script "a=$(defaults read com.apple.desktop | sed   s/\"Placement = Crop;\"/\"Placement = SizeToFit;\"/g | sed s/\"Placement = FillScreen;\"/\"Placement = SizeToFit;\"/g | sed s/\"Placement = Centered;\"/\"Placement = SizeToFit;\"/g | sed s/\"Placement = Tiled;\"/\"Placement = SizeToFit;\"/g) ;  defaults write com.apple.desktop \"$a\" ; killall Dock"

Its doing the “fill screen” rather than the “fit to screen” (which is what I need it to do).

Thanks,

Benjamin

It does “Fit to Screen” (the option name displayed in System Preferences on English systems) on my machine, whatever the placement was before. Of course, if System Preferences is open while the script’s run, it won’t show any change until it’s quit and reopened.

Other than that, I’m not sure what to suggest, except perhaps trying a short delay between the System Events code and the shell script.

Thank you. I will see what I can do.

Best Regards,

Benjamin