Displaying mail account information with AppleScript

Apple's AppleScript scripting language provides a means to obtain information from applications on a OS X system. Applications have a "dictionary" that can be consulted to determine what properties can be queried or altered. E.g., to see the dictionary for the Mail application on an OS X system, I can open the AppleScript Editor program found in Applications/Utilities, then click on File, select Open Dictionary and scroll down until I see the Mail application listed. I can then click on it to highlight it and then click on the Choose button to open that dictionary.

Open Dictionary

After opening it, I can see items I can access in the Mail app's dictionary.

Mail dictionary

E.g., for an email account I can access the following properties:

delivery account (smtp server or missing value): The delivery account used when sending mail from this account.
name (text): The name of an account
password (text): Password for this account. Can be set, but not read via scripting
authentication (password/apop/kerberos 5/ntlm/md5/external/Apple token/none): Preferred authentication scheme for account
account type (pop/smtp/imap/iCloud,r/o): The type of an account
email addresses (list of text): The list of email addresses configured for an account
full name (text): The users full name configured for an account
empty junk messages frequency (integer): Number of days before junk messages are deleted (0 = delete on quit, -1 = never delete)
empty sent messages frequency (integer): Number of days before archived sent messages are deleted (0 = delete on quit, -1 = never delete)
empty trash frequency (integer): Number of days before messages in the trash are permanently deleted (0 = delete on quit, -1 = never delete)
empty junk messages on quit (boolean): Indicates whether the messages in the junk messages mailboxes will be deleted on quit
empty sent messages on quit (boolean): Indicates whether the messages in the the sent messages mailboxes will be deleted on quit
empty trash on quit (boolean): Indicates whether the messages in deleted messages mailboxes will be permanently deleted on quit
enabled (boolean): Indicates whether the account is enabled or not
user name (text): The user name used to connect to an account
account directory (rile, r/o): The directory where the account stores things on disk
port (integer): The port used to connect to an account
server name (text): The host name used to connect to an account
include when getting new mail (boolean): Indicates whether the account will be included when getting new mail
move deleted messages to trash (boolean): Indicates whether messages that are deleted will be moved to the trash mailbox
uses ssl (boolean): Indicates whether SSL is enabled for this receiving account

An AppleScript script to access some of these properties is shown below:

AppleScript - accountList

To run the script, I can click on the Run button. The script will open a window where the following information is displayed:

AppleScript mail account list

The AppleScript code is included below. It is also available in binary script form as AccountList.scpt and AccountList.applescript. You can choose Script or Text when saving or exporting code to get a .scpt or .applescript file.

tell application "Mail"
	set accountList to ""
	repeat with i in every account
		set accountList to accountList & "Name: " & name of i & "
"
		set accountList to accountList & "User name: " & user name of i & "
"
		set accountList to accountList & "Type: " & account type of i & "
"
		set accountList to accountList & "Email addresses: " & email addresses of i & "
"
		set accountList to accountList & "Server name: " & server name of i & "
"
		set accountList to accountList & "Port: " & port of i & "
"
		set accountList to accountList & "Directory: " & account directory of i & "
		
"
	end repeat
	display alert accountList
end tell

The tell application "Mail" mail line indicates that the application the script needs to communicate with is the Mail app. There has to be a corresponding end tell line at the end of the script.

In the set accountList to "" line, I create a variable named accountList (I could call it whatever I liked) and set it's initial value to be be empty with "". The repeat with i in every account line and its corresponding end repeat indicate that the commands that appear between the "repeat" and "end repeat" lines should be repeated for every account. The with i portion of the line creates a loop variable, i, for the repeat - I could use another name besides "i", if I wished, but it represents "index" for me.

For the commands between the "repeat" and "end repeat" lines, I add information for various account properties onto the initially empty accountList. The ampersand, &, character indicates that what follows should be appended to the current contents of the variable accountList. At the end of each line there is an ampersand and a double quote. A second double quote appears on the line below. That indicates I want a new line created in the output. I could also put "\n", instead of typing a double quote, hitting Return, and then typing another double quote. A \n represents a newline and the AppleScript Editor will replace the "\n" with a beginning double quote followed by an ending double quote on the line below when I click on the Run button.

I can run the script from the AppleScript Editor or I can run it from a command line, i.e., a Terminal window with osascript AccountList.scpt or osascript AccountList.applescript . If run from a terminal window, you will see the icon for the Mail app bounce at the bottom of the screen and, if you click on it, you will see the results.

The output can be written to a text file rather than placed in a window using display alert. To write the information to a text file, I can add another variable acctListFile set to be the location and file name for the file in which I wish to store the information. E.g., I could use a command like the one below:

set acctListFile to (open for access "/Users/jmsmith1/Documents/allaccts.txt" with write permission)

Near the end of the script I can then replace the display alert command with the following two commands:

write accountList to acctListFile
close access acctListFile

The first command writes the contents of the variable accountList to the output file and the second command closes the file, since I don't need to write anything else to the file. So, the code for the second version, i.e., AccountList2.applescript, would be as follows:

tell application "Mail"
	set acctListFile to (open for access "/Users/jmsmith1/Documents/allaccts.txt" with write permission)
	set accountList to ""
	repeat with i in every account
		set accountList to accountList & "Name: " & name of i & "
"
		set accountList to accountList & "User name: " & user name of i & "
"
		set accountList to accountList & "Type: " & account type of i & "
"
		set accountList to accountList & "Email addresses: " & email addresses of i & "
"
		set accountList to accountList & "Server name: " & server name of i & "
"
		set accountList to accountList & "Port: " & port of i & "
"
		set accountList to accountList & "Directory: " & account directory of i & "
		
"
	end repeat
	write accountList to acctListFile
	close access acctListFile
end tell

The output would be in allaccts.txt

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px

Valid HTML 4.01 Transitional

Created: Tuesday December 22, 2015