Scripting Notes: The Accounts Class

In the Notes scripting dictionary, the account class is found in the Notes suite. It contains a few standard properties: name and id, and contains only one element, folders.

account n : A Notes account.

elements

contains: folders; contained by application.

properties

name (text, r/o) : The name of the account.

id (text, r/o) : The unique identifier of the account.

responds to

count, exists

Local or Online Accounts

By default, Notes provides access to either online accounts or local accounts. If the current user does not have an active online account (such as iCloud) in the Mail, Contacts & Calendars system preference pane, or Notes is not a selected service in the active account, then Notes will display only the local On My Mac account, and all notes will be stored locally on the computer. However, if iCloud or another online account is active, then Notes will display all the online accounts that have their Notes service checked, and will no longer display the On My Mac account.

accounts-match

Notes will display every online account whose Notes service is active.

The value of the account name property can be used to determine if there are active online accounts:

tell application "Notes"
if the name of every account contains "On My Mac" then
-- no online accounts are active
end if
end tell

In order for scripts to accommodate with the option of multiple active accounts, place the following sub-routine in your scripts. If there is only one active account, the sub-routine automatically returns the name of the current account. But if there exists more than one active account, the sub-routine will present a dialog to the user from which they can choose the account to target with the script.

Click to open example in the Script Editor application Get the Name of the Targeted Account
This example demonstrates how to get the name of the targeted account.
tell application "Notes"
set thisAccountName to my getNameOfTargetAccount("Choose an account:")
tell account thisAccountName
-- actions go here
end tell
end tell

on getNameOfTargetAccount(thisPrompt)
tell application "Notes"
if the (count of accounts) is greater than 1 then
set theseAccountNames to the name of every account
set thisAccountName to ¬
(choose from list theseAccountNames with prompt thisPrompt)
if thisAccountName is false then error number -128
set thisAccountName to thisAccountName as string
else
set thisAccountName to the name of account 1
end if
return thisAccountName
end tell
end getNameOfTargetAccount

Targeting Notes in the Account Class

Note that the account class does not contain the notes class as an element. This means that global queries or actions targeting notes cannot be done by addressing the account object.

For example, a script asking an account for the number of notes it contains will fail:

get-notes-count

However, the same script will work if instead of requesting the number of notes in the account, the request asks for the count of notes in all of its folders (which are elements of an account):

get-notes-count-02

Example Scripts

The following script examples demonstrate how to access and manipulate the account class element. To open an example script in the AppleScript Editor application on your computer, click the script icon at the top left of an example script. (Requires OS X Mountain Lion)

Click to open example in the Script Editor application Make Folder for This Weekday
This example demonstrates how to create a new folder.
set todaysName to (the weekday of the (current date)) as string
tell application "Notes"
activate
set thisAccountName to my getNameOfTargetAccount("Choose an account:")
tell account thisAccountName
if not (exists folder todaysName) then
make new folder with properties {name:todaysName}
end if
end tell
end tell

on getNameOfTargetAccount(thisPrompt)
tell application "Notes"
if the (count of accounts) is greater than 1 then
set theseAccountNames to the name of every account
set thisAccountName to ¬
(choose from list theseAccountNames with prompt thisPrompt)
if thisAccountName is false then error number -128
set thisAccountName to thisAccountName as string
else
set thisAccountName to the name of account 1
end if
return thisAccountName
end tell
end getNameOfTargetAccount
Click to open example in the Script Editor application New Note from Clipboard
This example demonstrates how to create a new note in a specific account. Note that the body of a note is actually stored and displayed as HTML data. So to preserve line breaks and formatting, the text from the clipboard is encased in HTML pre-formatting tags.
set noteHTMLText to "<pre style=\"font-family:Helvetica,sans-serif;\">" & ¬
(the clipboard as Unicode text) & "</pre>"
tell application "Notes"
activate
set thisAccountName to my getNameOfTargetAccount("Choose an account:")
display dialog "Enter the title for the new note:" default answer ¬
"New Note" with icon 1 with title "New Note with Clipboard"
set the noteTitle to the text returned of the result
tell account thisAccountName
make new note at folder "Notes" with properties {name:noteTitle, body:noteHTMLText}
end tell
end tell

on getNameOfTargetAccount(thisPrompt)
tell application "Notes"
if the (count of accounts) is greater than 1 then
set theseAccountNames to the name of every account
set thisAccountName to ¬
(choose from list theseAccountNames with prompt thisPrompt)
if thisAccountName is false then error number -128
set thisAccountName to thisAccountName as string
else
set thisAccountName to the name of account 1
end if
return thisAccountName
end tell
end getNameOfTargetAccount

TOP | CONTINUE