Creating an AppleScript to Switch Between Multiple iTunes Accounts
Introduction
If you own an Apple device, you'll have an Apple ID which will also be your account for shopping in iTunes. Some Apple owners may have multiple Apple IDs: perhaps work, personal, for shared content with family members or even Apple IDs for iTunes stores in different countries.
If you have multiple Apple IDs that you use with iTunes, you'll already be familiar with the hassle of changing accounts frequently and having to type the credentials each time.
In this tutorial, I'll be showing you how create an AppleScript that enables easy switching, between all of your Apple IDs, with a single click inside iTunes.
Before We Begin
Before we begin, it is important to understand that this is an advanced tutorial using AppleScript. If you are unfamiliar with AppleScript, you are first advised to check out some beginner tutorial published on Mactuts+. I've listed them below.
- The Ultimate Beginner’s Guide To AppleScript
- If and If Else: AppleScript Conditional Statements
- Advanced AppleScript Techniques
- Save Time and Effort with AppleScript Repeat Loops
1. The Aim
The aim of this tutorial is to code an AppleScript that enables you to:
- save all your Apple ID accounts in a plist file
- log in into iTunes using a registered account
- delete all registered accounts
The script saves all of your Apple ID accounts in a plist file with following structure: each file row has as key a label that identify the account, and it has as value an array that contains the Apple ID email address and password.
Tip: Note that the password fields, in the plist file, are not encrypted. In order to protect this, it is recommended that you set up a password on your OS X user account. Consider setting up separate user accounts in OS X if you share your Mac with anyone else.
The image below shows an example of the plist file updated with two Apple IDs registered.



If you are unfamiliar with plist files, I recommend you read the documentation about it on the Apple Developer site.
2. Launch the AppleScript Editor
Launch the AppleScript Editor app. You will find the application inside Applications > Utility. Alternatively, type the app name into Spotlight (or Alfred app) to launch it.



The AppleScript Editor is your development environment where you can write, edit, compile and test your AppleScript code. Once you have opened the app you can start to write the script.
3. Begin Coding the Script
The script has some functions that simplify the main code so it will be more readable and clear. Let's code the functions.
Step 1
The first function allows the creation of a plist file which contains all of your Apple ID accounts. The code below defines the function.
1 |
|
2 |
-- Function to create a plist file |
3 |
on createAccountsFile(filePath) |
4 |
tell application "System Events" |
5 |
-- Create the Root Dictionary |
6 |
set rootDict to make new property list item with properties {kind:record} |
7 |
-- Add the Root Dictionary to a new plist file saved in filePath |
8 |
set plistFile to make new property list file with properties {contents:rootDict, name:filePath} |
9 |
end tell |
10 |
end createAccountsFile |
The createAccountsFile(filePath) function has one parameter filePath which is the POSIX path of your plist file. Line 5 creates the Root dictionary and line 7 saves it to the new plist file.
Step 2
A second function allows you to add a new Apple account to plist file. The addAccount(filePath, label, email, pass) function has four parameters:
- filePath is the POSIX path of plist file
- label is a label to identify the Apple account
- email is the email of Apple ID account
- pass identifies the account password
The code below implements the function defined.
1 |
|
2 |
-- Function to register a new Apple account |
3 |
on addAccount(filePath, label, email, pass) |
4 |
tell application "System Events" |
5 |
-- Open the plist file |
6 |
tell property list file filePath |
7 |
-- Get the plist file content |
8 |
tell contents |
9 |
-- Append a new row with all account info to the Root Dictionary of plist file |
10 |
make new property list item at end with properties {kind:list, name:label, value:{email, pass}} |
11 |
end tell |
12 |
end tell |
13 |
end tell |
14 |
end addAccount |
Step 3
The function coded below returns a list of all labels of each account. The getLabels(filePath) has one parameter only: the filePath that is the plist file POSIX path.
1 |
|
2 |
-- Function to get all accounts labels |
3 |
on getLabels(filePath) |
4 |
-- Define an empty list that will contains all labels |
5 |
set labels to {} |
6 |
tell application "System Events" |
7 |
-- Get the plist file content and save it in fileContent variable |
8 |
set fileContent to property list items of contents of property list file filePath |
9 |
-- Scan each file row and add each label to labels list |
10 |
repeat with account in fileContent |
11 |
set label to name of account |
12 |
set end of labels to label |
13 |
end repeat |
14 |
end tell |
15 |
-- Return the list with all labels |
16 |
return labels |
17 |
end getLabels |
Step 4
Another important function is getAccountInfo(filePath, selectedAccount) that returns information (email and password) about a registered Apple account. The function below has two parameters:
- filePath is the POSIX path of plist file
- selectedAccount is the chosen account when you run the final script
The code below implements the function.
1 |
|
2 |
-- Function to get email and password of a selected account |
3 |
on getAccountInfo(filePath, selectedAccount) |
4 |
tell application "System Events" |
5 |
-- Open the plist file and get its content |
6 |
set fileContent to property list file (filePath) |
7 |
-- Get information based on selectedAccount |
8 |
set info to value of property list item selectedAccount of fileContent |
9 |
end tell |
10 |
-- Return the info array |
11 |
return info |
12 |
end getAccountInfo |
Step 5
The getPath(fileName) function coded below, implements a utility function that returns the absolute POSIX path of a file specified by the parameter fileName. In our case, the file name will represent the plist file with all Apple accounts.
1 |
|
2 |
-- Function to get the absolute path of a file |
3 |
on getPath(fileName) |
4 |
tell application "Finder" |
5 |
-- Get the absolute path of parent folder of your AppleScript file. |
6 |
set _path to parent of (path to me) as string |
7 |
-- Transform the path in a POSIX path |
8 |
set _path to POSIX path of _path |
9 |
-- Concatenate the folder path with the file name |
10 |
set _path to _path & fileName |
11 |
-- Return the POSIX path of plist file with name 'filename' that is inside the folder of AppleScript file |
12 |
return _path |
13 |
end tell |
14 |
end getPath |
Step 6
Finally, let's code the last function: the core function. It allows us to sign into iTunes, automatically, with any account registered in the plist file. The iTunesLogin(info) function has one parameter only: info represents an array which contains email and password of an Apple account.
To login to iTunes automatically, AppleScript language simulates the manual operations you would do to achieve the following:
- Launch iTunes
- Click on Store > Sign In…
- Type your email
- Type your password
- Click on Sign In button
The function below does all steps above for you.
1 |
|
2 |
-- Function for iTunes sign in |
3 |
on iTunesLogin(info) |
4 |
-- Launch iTunes |
5 |
tell application "iTunes" to activate |
6 |
tell application "System Events" |
7 |
tell process "iTunes" |
8 |
set frontmost to true |
9 |
-- Logout before executing a new login |
10 |
try |
11 |
click menu item loginBtn of menu "Store" of menu bar 1 |
12 |
on error |
13 |
click menu item logoutBtn of menu "Store" of menu bar 1 |
14 |
delay 2 |
15 |
click menu item loginBtn of menu "Store" of menu bar 1 |
16 |
end try |
17 |
delay 1 |
18 |
-- Type account email |
19 |
keystroke item 1 of info |
20 |
keystroke tab |
21 |
delay 1 |
22 |
-- Type account password |
23 |
keystroke item 2 of info |
24 |
-- Press the return key |
25 |
keystroke return |
26 |
delay 3 |
27 |
keystroke return |
28 |
end tell |
29 |
end tell |
30 |
end iTunesLogin |
In this function, there are two global variables loginBtn and logoutBtn. They represent the buttons for signing into and signing out of iTunes. The button names are base on the system language; you have to set them correctly for the AppleScript to work.
4. AppleScript Main Code
Once you have finished coding all previous functions, you can write the main code of your AppleScript. Inside the AppleScript, place you cursor at the top of file.
Step 1
The first step to complete our tutorial is set some variables and do some checks.
1 |
|
2 |
-- Set the name of plist file which will contains all your Apple accounts |
3 |
set fileName to "accounts.plist" |
4 |
|
5 |
-- Get the absolute path of plist file using the 'getPath(fileName)' function |
6 |
set filePath to getPath(fileName) |
7 |
|
8 |
-- Define the iTunes login and logout buttons name based on your system language |
9 |
property loginBtn : "Sign In…" |
10 |
property logoutBtn : "Sign Out" |
11 |
|
12 |
tell application "Finder" |
13 |
-- Check if the plist file already exists. If false let's create it with 'createAccountsFile(filePath)' function |
14 |
if not (exists filePath as POSIX file) then |
15 |
my createAccountsFile(filePath) |
16 |
end if |
17 |
end tell |
Step 2
The next step is to display a dialog box which asks the user what they want to do? Our AppleScript has following features:
- Choose Account (for iTunes sign in)
- Add a new Apple Account (to the plist file)
- Reset Accounts (empty the plist file)



The code below implements the dialog.
1 |
|
2 |
-- Display the actions dialog box |
3 |
set dialogResult to display dialog "Switch Apple Account" buttons {"Choose Account", "Add Account", "Reset Accounts"} with hidden answer |
Step 3
Now it's time to parse the user choice.
1 |
|
2 |
-- Parse the result |
3 |
if dialogResult = {button returned:"Add Account"} then |
4 |
|
5 |
-- Click on 'Add Account' button |
6 |
|
7 |
else if dialogResult = {button returned:"Choose Account"} then |
8 |
|
9 |
-- Click on 'Choose Account' button |
10 |
|
11 |
else if dialogResult = {button returned:"Reset Accounts"} then |
12 |
|
13 |
-- Click on 'Reset Accounts' button |
14 |
|
15 |
end if |
If the user clicks on Add Account button the script ask to him to insert the credentials for a new Apple account. It will display three dialog boxes with input fields for account label, email and for the password. The code below implements this process.
1 |
|
2 |
-- Click on 'Add Account' button |
3 |
|
4 |
-- Ask to insert the account label |
5 |
display dialog "Insert a label for the new Apple account" default answer "" |
6 |
-- Save the field value in label variable |
7 |
set label to (text returned of result) |
8 |
|
9 |
-- Ask to insert the account email |
10 |
display dialog "Insert the email of Apple Account" default answer "" |
11 |
-- Save the field value in email variable |
12 |
set email to (text returned of result) |
13 |
|
14 |
-- Ask to insert the account password |
15 |
display dialog "Insert the password of Apple Account" default answer "" with hidden answer |
16 |
-- Save the field value in pass variable |
17 |
set pass to (text returned of result) |
18 |
|
19 |
-- Insert the new account inside the plist file using the 'addAccount(filePath, label, email, pass)' function |
20 |
addAccount(filePath, label, email, pass) |
21 |
|
22 |
-- Display a success messagge |
23 |
display dialog "The " & label & " account was registered successfully!" buttons {"Done"} |
If a user clicks on the Choose Account button, the script displays a dialog with all of the registered accounts. When the user chooses an account, the script runs iTunes login. Let's implement the action.
1 |
|
2 |
-- Click on 'Choose Account' button |
3 |
|
4 |
-- Get all accounts labels using the 'getLabels(filePath)' function |
5 |
set labels to getLabels(filePath) |
6 |
|
7 |
-- Check if the user has inserted at least one account |
8 |
if the (count of labels) is not 0 then |
9 |
|
10 |
-- Display all available accounts |
11 |
choose from list labels with title "Apple Accounts" with prompt "Login in iTunes with:" OK button name "Choose" cancel button name "Cancel" |
12 |
|
13 |
-- Save the choice |
14 |
copy the result as string to selectedAccount |
15 |
|
16 |
-- If user has not pressed the Cancel button go ahead |
17 |
if not selectedAccount is equal to "false" then |
18 |
-- Get the account info about chosen account using 'getAccountInfo(filePath, selectedAccount)' |
19 |
set info to getAccountInfo(filePath, selectedAccount) |
20 |
-- Run the login |
21 |
iTunesLogin(info) |
22 |
end if |
23 |
|
24 |
else |
25 |
display dialog "No accounts registered yet! Insert a new one!" buttons {"Done"} |
26 |
end if |
If the user clicks on the Reset Accounts button, the script overwrites the plist file with an empty one. The code below calls the createAccountsFile(filePath) function again to complete the process.
1 |
|
2 |
-- Click on 'Reset Accounts' button |
3 |
|
4 |
try |
5 |
-- Ask user to confirm the action |
6 |
display dialog "Do you want to delete all inserted accounts?" |
7 |
|
8 |
-- If user does not cancel the action overwrite the plist file |
9 |
createAccountsFile(filePath) |
10 |
|
11 |
-- Display a success message |
12 |
display dialog "All accounts are deleted successfully" buttons {"Done"} |
13 |
on error |
14 |
-- Do nothing |
15 |
end try |
5. Run the Script Inside iTunes
The last task required, in order to finish the tutorial, is installing your AppleScript into iTunes so you can easily access it anytime it is required.
Step 1
First, export the AppleScript. Inside the AppleScript Editor app click on File > Export… from the top menu.
Inside the export panel you have to:
- export as "Access To iTunes"
- choose as location your Desktop
- choose Script as file format
- and select the Run-only option
When you are ready click the Save button.



Step 2
Now move the exported script into iTunes. To do that, launch Finder app and choose Go > Go To Folder from the top menu.
Complete the panel input field with /Users/Your.System.User/Library/iTunes/ and click on the Go button.



Now move the the Access To iTunes file from the Desktop to the Script folder (if this folder does not already exist, you will need to create it first) to complete the process.



Step 3
Another important step to ensure your AppleScript works correctly is to enable access to assistive devices: open System Preferences app, choose the Accessibility icon and check Enable access to assistive devices. The image below show the panel with this option.



Step 4
Now you are able to trigger the AppleScript directly from iTunes.



Conclusion
Congratulations! You have completed the AppleScript. If you have followed all of the steps you will be able to use the AppleScript from within iTunes. In this tutorial you have learned how to code an AppleScript, to switch between your Apple accounts, without having to repeatedly type in different email addresses and passwords. You can easily adapt this AppleScript to work with Mac App Store, too. Simply modify the iTunesLogin function. I hope you enjoyed this tutorial, let me know how you get on with the scripts and any comments or questions that you have using the comments section, below.