AppleScript: Personal Preferences

The first feature of Mac OS 9 is that it can have different settings for each user. There are people who want this feature, but can’t upgrade to Mac OS 9. Guess what? Anyone with AppleScript can have multiple user settings.

I will be explaining how to switch preferences for a computer with three users. The users names are User 1, User 2, and User 3. You can use real names if you wish, when you create this script.

Set Up The Computer

Create a new folder in the System Folder. Name it User 1. Select the Preferences folder and duplicate it (Command-D). Rename the copy User 2. Inside the User 2 folder create a new folder. Name the new folder User 2. Select the Preferences folder and duplicate it (Command-D). Rename the copy User 2. Inside the User 3 folder create a new folder. Name the new folder User 3. Inside the Preferences folder, create a new folder User 1.

You have assigned folders to place the preferences in while the person is not using the computer. User 2 and User 3 have the duplicated preference files, because you are assuming that they don’t want to start back at the default settings. User 1 has a blank folder because User 1 is using the computer. You know this because there is a folder named User 1 in the Preferences folder. The user folders inside the user folders are there to tell the computer who the preferences belong too when they are not in their user folder.

Are you confused? You don’t have to be. Here is a script that will set up the computer for you. It will take some time for it to complete. The instructions on how the script is formed are between the (* and *).

tell application “Finder”

(*This script is for the Finder.*)

activate

(*Use activate to bring the application to the front.*)

select folder “Preferences” of folder “System Folder” of startup disk

(*Select + item + location selects the item.*)

duplicate selection

(*Use the duplicate command to copy the item.*)

select folder “Preferences” of folder “System Folder” of startup disk

duplicate selection

(*You must select what you want to duplicate and then duplicate the selection.*)

select folder “Preferences copy” of folder “System Folder” of startup disk

set name of selection to “User 2”

(*Set + name + what + to + “to what” is used to change an items name.*)

select folder “Preferences copy 1” of folder “System Folder” of startup disk

set name of selection to “User 3”

(*To change the name of an item you must select it and change the name of the selection.*)

make new folder at folder “System Folder” of startup disk

(*Make + what + at location will create a new item.*)

select folder “untitled folder” of folder “System Folder” of startup disk

set name of selection to “User 1”

make new folder at folder “User 2” of folder “System Folder” of startup disk

select folder “untitled folder” of folder “User 2” of folder “System Folder” of startup disk

set name of selection to “User 2”

make new folder at folder “User 3” of folder “System Folder” of startup disk

(*To make a new folder, use the the command make new folder.*)

select folder “untitled folder” of folder “User 2” of folder “System Folder” of startup disk

set name of selection to “User 3”

make new folder at folder “Preferences” of folder “System Folder” of startup disk

select folder “untitled folder” of folder “User 2” of folder “System Folder” of startup disk

set name of selection to “User 1”

end tell

Create The Scripts

Each user has their own script. It tells the computer to put away the previous users preferences and activate their own preferences. I will be showing you how to create the script for User 1.

Who was using the computer last? You need to put away the preferences of the previous user. If the last user was you, you need to tell the computer not to put away the preferences. The text between the (* and *) are for instructional use only. You don’t have to include them in your script.

tell application “Finder”

(*this script is for the Finder*)

if exists folder “User 1” of folder “Preferences” of folder “System Folder” of startup disk then

(*To find out if something exists or not, you use the exists command with the item and its location.*)

return

(*To cancel a script, use the return command.*)

else

(*If the requirements of the if command aren’t met, the else command will tell the script to do something else.*)

If User 1 wasn’t using the computer last, the computer will see if User 2 was.

if exists folder “User 2” of folder “Preferences” of folder “System Folder” of startup disk then

(*The location of the item is very important!*)

move items of folder “Preferences” of folder “System Folder” of startup disk to folder “User 2” of folder “System Folder” of startup disk

(*Instead of file, AppleScript uses the term item.*)

else

(*No. Okay, don’t move the preferences.*)

If User 2 & 1 weren’t using the computer last, the computer realizes that User 3 must have been. It will tell the computer to move all the items in the User 3 folder. It must also tell the computer where to find the folder and where to move the items to.

move items of folder “Preferences” of folder “System Folder” of startup disk to folder “User 3” of folder “System Folder” of startup disk

(*The move command is formed like so: move + what + where + to + where to.*)

end if

The preference folder is now empty. Since you know this is the User 1 script, User 1 must want his own preferences. You don’t have to find out which preferences to move. You can just tell the computer to move the User 1 preferences.

move items of folder “User 1” of folder “System Folder” of startup disk to folder “Preferences” of folder “System Folder” of startup disk

(*You can replace the term startup disk with the volume name (inside quotes), if you wish.*)

The preferences will now work in any application that hasn’t been opened yet. The Finder has already been opened, so we must quit the Finder and have it relaunch. Quitting takes only a few seconds, opposed to restarting.

tell application “Finder”

quit

end tell

(*So you don’t confuse the script, use the tell command to make sure the correct application quits.*)

All the commands have been given. The script must now realize that it has ended. Just add the end lines to your script and you will be done.

end if

end tell

The finished script should look like this:

tell application “Finder”

if exists folder “User 1” of folder “Preferences” of folder “System Folder” of startup disk then

return

else

if exists folder “User 2” of folder “Preferences” of folder “System Folder” of startup disk then

move items of folder “Preferences” of folder “System Folder” of startup disk to folder “User 2” of folder “System Folder” of startup disk

else

move items of folder “Preferences” of folder “System Folder” of startup disk to folder “User 3” of folder “System Folder” of startup disk

end if

move items of folder “User 1” of folder “System Folder” of startup disk to folder “Preferences” of folder “System Folder” of startup disk

tell application “Finder”

quit

end tell

end if

end tell

Save the script as User 1 in the Startup Items folder. In the save dialog window, check ‘Never Show Startup Screen.’

To modify the script for use for User 2, replace the text User 1 with User 2 anywhere in the script. Also, replace the text User 2 with User 1 anywhere in the script. Save the script as User 2 in the Startup Items folder. To change the User 2 script to the User 3 script, replace the text User 2 with User 3 anywhere in the script. Also, replace the text User 3 with User 2 anywhere in the script. Save the script as User 3 in the Startup Items folder. Quit out of Script Editor.

Select The User

Install the Location Manager from a Mac OS CD. In the Location Manager control panel create 3 locations. Name the locations User 1, User 2, and User 3. Use the Auto-Open Items setting to select the script that will be opened on startup for the user. You don’t need to use any of the other settings in the Location Manager, because your preference switching scripts do the same thing Location Manager offers. Open the location manager preferences. Turn on the option of having the Location Manager ask you at startup. When the user starts up the computer, they can tell the computer who is using the computer.

The Alternative To The Location Manager

You can use AppleScript if you don’t want to use the location manager. Create a little dialog box where the user can select who they are. You should remove the scripts from the Startup Items to a safer place. Place this script in the Startup Items folder. When you first run this script, it will ask you where the applications (scripts) are.

display dialog “Who Are You?” buttons {“User 1”, “User 2”, “User 3”}

(*The dialog window.*)

if button returned of result = “User 1” then

(*If the person is User 1…*)

tell application “User 1”

activate

end tell

(*…it will run the User 1 script.*)

else

(*If the person isn’t…*)

if button returned of result = “User 2” then

(*…the computer will ask if the person is User 2.*)

tell application “User 2”

activate

end tell

(*The computer will ask where the application is, the first time the script is run.*)

else

(*If it the person isn’t User 1 or 2, they must be three.*)

tell application “User 3”

activate

end tell

(*The preferences will be set for User 3.*)

end if

end if

What if there are only 2 users?

You don’t need the User 3 folder, the User 3 script, the User 3 location, and the parts of the other scripts that were for User 3.

Delete the last else command and the part of the script that was for User 3. Stop when you reach end if. You still need this because you deleted only part of the if command, not the whole thing. The User 1 script for two users should look like this:

tell application “Finder”

if exists folder “User 1” of folder “Preferences” of folder “System Folder” of startup disk then

return

else

if exists folder “User 2” of folder “Preferences” of folder “System Folder” of startup disk then

move items of folder “Preferences” of folder “System Folder” of startup disk to folder “User 2” of folder “System Folder” of startup disk

end if

move items of folder “User 1” of folder “System Folder” of startup disk to folder “Preferences” of folder “System Folder” of startup disk

tell application “Finder”

quit

end tell

end if

end tell

If you decided to go with the script instead of using Location Manager, you must modify that, also. Delete the third button and the if statement that is for the third user. Be sure that your ifs, thens, elses, and end ifs align up together. Here is what it should look like:

display dialog “Who Are You?” buttons {“User 1”, “User 2”}

if button returned of result = “User 1” then

tell application “User 1”

activate

end tell

else

tell application “User 2”

(*Notice how I deleted the second if command.*)

activate

end tell

end if

(*There should only be one end if now.*)

What if there are more that 3 users?

For each user add a User folder to the System folder. In those folders there should be their preferences and an empty User folder. Each user needs a User script. All the scripts must be changed to add another user.

The user scripts use if commands to figure out who the previous user was. Notice in the 3 user script how user 3 doesn’t have an if user 3 command before the computer moves his file. It is the last option in the if statement, so it doesn’t need another if. When you add another user, he won’t be the last option. You need to add an if command before the computer decided to move the User 3 preferences.

else

if exists folder “User 3” of folder “Preferences” of folder “System Folder” of startup disk then

move items of folder “Preferences” of folder “System Folder” of startup disk to folder “User 3” of folder “System Folder” of startup disk

Now you can add another user. Add an else command to tell the computer to continue if the requirements have not been met. If there are only four users, you don’t need the extra if command. If there are more than four users, you must have the if command before you move the User 4 preferences, as we did with User 3 above. Here is the User 1 script for a computer with four users:

tell application “Finder”

if exists folder “User 1” of folder “Preferences” of folder “System Folder” of startup disk then

return

else

if exists folder “User 2” of folder “Preferences” of folder “System Folder” of startup disk then

move items of folder “Preferences” of folder “System Folder” of startup disk to folder “User 2” of folder “System Folder” of startup disk

else

if exists folder “User 3” of folder “Preferences” of folder “System Folder” of startup disk then

move items of folder “Preferences” of folder “System Folder” of startup disk to folder “User 3” of folder “System Folder” of startup disk

else

move items of folder “Preferences” of folder “System Folder” of startup disk to folder “User 4” of folder “System Folder” of startup disk

end if

move items of folder “User 1” of folder “System Folder” of startup disk to folder “Preferences” of folder “System Folder” of startup disk

tell application “Finder”

quit

end tell

end if

end if

end tell

If you have decided to use the dialog box idea, then you will have to add the option for the fourth user. Unfortunately, the dialogs in AppleScript only support 3 buttons. If you want more buttons, you must download the scripting addition, dialog director (http://osaxen.com/home.taf?id=dialog_director). Instead of having them press a button, you could have them type in their User number. Here is what the script would look like for four users:

display dialog “Who Are You?” default answer {“User “}

(*Use default answer for people to type in something*)

(*If you don’t put anything for the default text, the space will be blank.*)

if text returned of result = “User 1” then

(*Use the command ‘text returned’ instead of button returned.*)

tell application “User 1”

activate

end tell

(*Same as before*)

else

if text returned of result = “User 2” then

(*The user must type exactly what the computer can recognize!*)

tell application “User 2”

activate

end tell

else

if the text returned of result = “User 3” then

tell application “User 3”

activate

end tell

else

tell application “User 4”

(*You don’t need another if command before this because it is the last option.*)

activate

end tell

end if

end if

end if

If you set up your computer and the scripts correctly, everything should work. If you do encounter a problem, feel free to send me an email about it. Restart and never change a browser setting back, again!