Script to change your login screen background

I came across a command line here http://www.macosxhints.com/article.php?story=20070610164328933 that allows you to change the background of your login window. I wanted to create a script that would make it easier to choose the picture you wanted and not have to mess with the terminal. Here is what I came up with:

I have a bad memory, thus I over commented it so I could go back and reference it.

--This is just a dialog for the user
display dialog "This program will change your login screen background to a picture that you select. You must use a jpeg image for this to work. Do you wish to continue?"

--This allows the user to choose a picture of his liking
tell application "Finder"
	set image_path to choose file with prompt "Choose a jpeg image that you want to set for the background"
end tell

--This changes the chosen image path above to one that the terminal shell can understand. The quoted form takes care of any spaces in the path
set my_image to quoted form of POSIX path of image_path as string

--This creates a variable so you don't have to type the path to the Library desktop pic folder
set desktop_images to "'/Library/Desktop Pictures'"

--This creates a variable that combines the above path and the new file name
set login_image to desktop_images & "/login_image.jpg"

--This copies the image from wherever it is into the Library desktop pic folder and renames it
do shell script "cp " & my_image & " " & desktop_images & "/login_image.jpg"

--This sets the default login pic to the new image
do shell script "defaults write /Library/Preferences/com.apple.loginwindow DesktopPicture " & login_image with administrator privileges

--This displays a dialog as the active window
tell application "Finder"
	activate
	display dialog "Your login screen background has been changed. Log out to see your new background!"
end tell

I’m new at this so if anyone sees a better way I could have done this I would appreciate the comments.
thanks
dale

Welcome, and thanks for sharing! :slight_smile:

My only recommendations would be to remove the Finder tell blocks and to use full paths in do shell script.

choose file with prompt "Choose a JPEG image for the login window background:" of type {"public.jpeg"} without invisibles
set newImagePath to quoted form of POSIX path of result

set imagePath to quoted form of POSIX path of ((path to desktop pictures folder as Unicode text) & "Login.jpg")

do shell script "/bin/cp " & newImagePath & " " & newImagePath & "; /usr/bin/defaults write /Library/Preferences/com.apple.loginwindow DesktopPicture " & imagePath with administrator privileges

display dialog "Your login screen background has been changed. Log out to see your new background!"

There is also a randomising version on macosxhints

Thanks Bruce, that cleans it up pretty nicely.

This is wonderful I have spent so many hours trying to do this and have not had success. Very cool you had posted this already. For some reason Bruce Phillips updated worked the first time but after that when I ran it again under Snow Leopard it stopped working.

You really need to rename the file and overwrite the desktop image file in the CoreServices folder.

That is exactly what I was trying to do but I couldn’t figure out how to even get it to replace because it won’t let me replace anything in that folder even if I first have the script delete the file in the System folder. I tried the following with a file I manual renamed and stuck on the desktop and I can’t get it to use administrator privileges. It seems like this should be fairly simple but I can’t get it to work.

tell application "Finder"
	activate
	duplicate file "DefaultDesktop.jpg" of folder "Desktop" of folder "user account name" of folder "Users" of startup disk to folder "CoreServices" of folder "Library" of folder "System" of startup disk with replacing
end tell

Hello

You can use a do shell script with administrative privileges to gain rights to perform the operation.

A good example is given here: http://macscripter.net/viewtopic.php?id=32019
For the full documentation see: http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html

Best regards

McUsr

What about a button to set it back to what it was before? (Just an idea)