applescript / Change your desktop background with AppleScript

A simple AppleScript to change your Desktop Background

With this tutorial you will learn how to write your own Apple Script to change your desktop background. There are even two options for quick switching; using the 'Script Menu' in the menu bar or using a hot key with Quicksilver. I will explain, first, how to do this with the Script Menu in the menu bar. We will go through the steps of getting the script menu up there. Then setting up for the script. Finally, writing and testing the script. That will take care of the basics. To go into an advanced configuration, we will add the script to a hotkey with the application Quicksilver. For a quick definition of Quicksilver read this link in Wikipedia.

To get started, figure out what you are using this for. I have a set of desktop backgrounds for when someone, other than myself, can see my computer. They are mostly colorful variations of my company logo. When I am working alone I need something darker and more "space-y". Now, I know that my choices for the desktop backgrounds are going to be "Work" and "Presentation". Please, spend some thought on that now, you will need it later.

Enough planning lets get to work.

Preparing

We need to set up your mac to be able to do this. Everything you need is already on there. Still, we have to get it ready to do it.

Enable GUI Scripting and Show the script icon on your menu bar.

Enable GUI Scripting with AppleScript Utility We will now enable GUI Scripting and set that Script menu in the menu bar.

To do this Open Applications/AppleScript/AppleScript Utility.app and Set the check mark in GUI Scripting: Enable GUI Scripting. It is self-explainatory; it enables GUI scripting.

Now, you can also Set the checkmark for "Show Script menu in menu bar".

Once those two options are set you should see a small dark colored script-looking icon in your menu bar. If you set that check mark and still do not see the script menu in the menu bar then 'relaunch' Finder. You will need that Script menu to access your script with just one click.


Make a location for the backgrounds

Okay, that is pretty easy. I like to keep things that are in use in a seperate location just incase I move an image or enclose them in another folder; I place all of my desktop background images in a folder in my user folder named "desktopBackgrounds".

now we are ready.

Execution

Like I said before, the following script is going to react to your input. When activated, it ask you "Choose your Background". The choices, in this example, "Cancel", "Work", and "Presentation", you can rename them later. For example, "Standard" or "Hot" just make it serve your purpose. What will then happen is the AppleScript will change the Desktop Background Preference for the location of your image. Depending on which button you click in the window, your computer will look for matching image in your "desktopBackgrounds" folder, that you just created. So, for example, if you leave the choices "Work", and "Presentation" it will look for work.jpg and presentation.jpg; get it?

Pick yourself two desktops backgrounds. Give them the same names you plan to give your buttons and move or copy them to you "desktopBackgrounds" folder.

Now open your Script Editor, you will find Script Editor.app in Applications/AppleScript. When it opens it will start a new "Untitled" Script for you.

Write the AppleScript

For those of you that just want the script it is posted at the bottom of this page. Remember, you will still have to adjust the paths and names to your computer. For the rest of you let's take this step by step. AppleScript is pretty straight forward. I will write this statement by statement.

To get the script to offer us a choice we will have to get it to display something we can select. The following line will do that.



display dialog "Choose your location." buttons {"Cancel", "Home", "Work"} default button 3

That is pretty easy, "display dialog" you are asking the computer to display a dialog. That dialog will give you three buttons, cancel, home, and work. The next line will define your variable that we will need to tell the computer which image to display. the last part, "default button 3", puts the focus on the third button. That way, you can switch, even quicker, by activating the script then hitting enter. then your desktop background image will change to that third choice.

Next, we will need a quick place to "store" our choice. We will create, or declare a variable to hold that value.



set situation to the button returned of the result

The above line is also pretty self explanatory; "set" the value of the variable "situation" to the name of the button you clicked. (put simply)

Last but not least, the heart of the script. We will need to now tell the computer to change the desktop backgroud image to the one we chose.



tell application "Finder"
set desktop picture to {"Your HD:Users:YourUserName:desktops:" & situation & ".jpg"} as alias
end tell

You should be able to understand these lines if you have experience with "if, then, else" type programming. I will assume that there are many beginners reading this so... The application in "control" of your desktop background image is "Finder". Basicly you have to tell finder what to do. You also have to let finder know when you are done telling it what to do. The way you do that is displayed above in the first and third lines. The command begins with "tell application..." and ends with "end tell". The command that we gave it is in the second line. That line is where our variable "situation" comes into play. To break that line down we have "set" the "desktop picture", finder's name for your desktop background image, "to" the image's location, we chose, as a symbolic link, "alias". Please, make sure you adjust this line to your computer. As I have said before; most of you will still have your hard drive's name as "Macintosh HD". If your user name is "John" then that path will look like this:



set desktop picture to {"Macintosh HD:Users:John:desktops:" & situation & ".jpg"} as alias

Continuing in the path; "desktops" is the folder we created for the images. At that point we stop the path from being a string and making it dynamic by adding a variable. We also want the value of the variable to be included in our string and not the word "situation". We tell the script, here the string stops, with the quotation mark. The ampersand, &, tells the script to attach the value of the variable to our string. We are not done. Our image will be ending in .jpg, .png, or whatever. To tell the script to pick the string back up we use another ampersand, then start the quotation mark again, add your extention, then close your string and the path. if you selected "Work" the computer will see the path as: Macintosh HD:Users:John:desktops:work.jpg, I hope you understood that. If not email me using the contact form or post in my thread at Virtualshenanigans.com.

In short, you want to tell finder where that image is. You do not want finder to move your image to a different location. To solve this dilemma, we ask finder to make a sort of "short cut" to the image called an "alias". After finder sets up that alias it can use it to display the image, of your choosing, as the desktop background image.

copy & paste

This script into the empty Script Editor window. Adjust it for your computer. You will see, I did not call my hard drive "Macintosh HD". I changed it to help you learn where you have to customize things for your computer. You will also have to add your user name. That is all you have to edit.



display dialog "Choose your location." buttons {"Cancel", "Home", "Work"} default button 3
set situation to the button returned of the result
tell application "Finder"
set desktop picture to {"Your HD:Users:yourUserName:desktops:" & situation & ".jpg"} as alias
end tell

Finally Save the script as "changeDesk" and put it in the folder " USER/Library/Scripts". Now when you click on the little black scroll, in the menu bar, you will see. "changeDesk" in the list. Click it and it will ask you to choose your location...

that was it.

Disclaimer: Still-Scripts claims no responsibility for any damage that may occur from the use of any information found here or found on links followed from this page. All items, logos and/or companies mentioned on this and other pages are trademarks, registered trademarks, logos, copyrights, or service marks of the respective companies.