Scripting Notes: The Folder Class

In the Notes scripting dictionary, the folder class is found in the Notes suite. It contains a few standard properties: name, id, and container, and contains two elements, folders, notes.

folder n : A folder containing notes

elements

contains: folders, notes; contained by application, accounts, folders.

properties

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

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

container (account or folder, r/o) : the container of the folder

responds to

count, delete, exists, make, move

Example Scripts

The following script examples demonstrate how to access and manipulate the folder 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)

Folders are elements of the application, accounts, and folder classes. They respond to standard verbs like make, move, exist, and delete.

IMPORTANT: Never delete the “Notes” folder from an account. Doing so can put the Notes application in a non-functioning state.

Click to open example in the Script Editor application Delete All Folders of Specified Account
This example demonstrates how to delete all folders in a specific account.
tell application "Notes"
display dialog "This script will delete all folders from a specified account." & ¬
linefeed & linefeed & "This action cannot be undone." buttons {"Cancel", "Continue"} ¬
default button 1 with icon 2
set thisAccountName to ¬
(my getNameOfTargetAccount("Choose the account to have its folders deleted:"))
delete (every folder of account thisAccountName whose name is not "Notes")
display dialog "All folders have been deleted from account “" & ¬
thisAccountName & ".”" buttons {"OK"} default button 1 with icon 1
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

The following example demonstrates how to create a series of folder at the top-level of an account:

Click to open example in the Script Editor application Make Monthly Folders
This example demonstrates how to create a series of folders in a specific account.
set thisDate to current date
set day of thisDate to 1
set time of thisDate to 0
set theseFolderNames to {}
repeat with i from 1 to 12
set month of thisDate to i
set the thisMonthName to (month of thisDate) as string
if i is greater than 9 then
set thisPrefix to (i as string)
else
set thisPrefix to "0" & (i as string)
end if
set the end of theseFolderNames to thisPrefix & "-" & thisMonthName
end repeat
tell application "Notes"
set thisAccountName to ¬
(my getNameOfTargetAccount("Choose the account in which to make the folders"))
tell account thisAccountName
repeat with i from 1 to the count of theseFolderNames
set thisFolderName to item i of theseFolderNames
if not (exists folder thisFolderName) then
make new folder with properties {name:thisFolderName}
end if
end repeat
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

The following example demonstrates how to create a series of folders within a folder in an account:

Click to open example in the Script Editor application Make Folders in Folders
This example demonstrates how to create a series of folders in another folder.
set the folderName to "This Month"
tell application "Notes"
set thisAccountName to ¬
(my getNameOfTargetAccount("Choose the account in which to create folders:"))
tell account thisAccountName
if not (exists folder folderName) then
make new folder with properties {name:folderName}
end if
repeat with i from 1 to 4
set thisFolderName to "Week-" & (i as string)
tell folder folderName
if not (exists folder thisFolderName) then
make new folder with properties {name:thisFolderName}
end if
end tell
end repeat
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

The following example demonstrates how to replicate the folder structure of a chosen Finder folder. (see illustration at right)

Click to open example in the Script Editor application Replicate Finder Folder Hierarchy
This script will create a folder hierarchy in Notes, matching the folder hierarchy of a chosen Finder folder, up to a maximum of four levels deep.
tell application "Notes"
activate
display dialog "This script will create a folder hierarchy in Notes, matching the folder hierarchy of a chosen Finder folder, up to a maximum of four levels deep." with icon 1
set the sourceFolder to ¬
(choose folder with prompt "Pick the folder whose folder structure is to be copied:")
tell application "Finder"
set sourceFolderName to the name of sourceFolder
end tell
tell account 1
if exists folder sourceFolderName then
display alert "FOLDER EXISTS" message "A folder named “" & sourceFolderName & ¬
"” already exists in Notes." buttons {"Cancel"} ¬
default button 1 cancel button "Cancel"
end if
end tell
end tell
set sourceFolderHFSPath to sourceFolder as string
--> "Macintosh HD:Users:sal:Documents:Smith Project:"
set AppleScript's text item delimiters to ":"
set theseSegemnts to every text item of sourceFolderHFSPath
-- {"Macintosh HD", "Users", "sal", "Documents", "Smith Project", ""}
set AppleScript's text item delimiters to ""
set the sourceSegmentCount to (the count of theseSegemnts) - 1
-- find all folders and convert their paths to segment lists
tell application "Finder"
set theseFolders to (every folder of the entire contents of the sourceFolder)
set the folderPathsAsSegmentsList to {}
repeat with i from 1 to the count of theseFolders
set thisHFSPath to (item i of theseFolders) as string
set AppleScript's text item delimiters to ":"
set thisFolderPathAsSegments to items (sourceSegmentCount + 1) thru -2 of ¬
(every text item of thisHFSPath)
set AppleScript's text item delimiters to ""
set the end of the folderPathsAsSegmentsList to thisFolderPathAsSegments
end repeat
end tell
-- make the source folder in Notes
tell application "Notes"
tell account 1
set the topLevelFolder to make new folder with properties {name:sourceFolderName}
end tell
end tell
-- build the other folders within the source folder
repeat with i from 1 to count of folderPathsAsSegmentsList
set thisSegmentList to item i of folderPathsAsSegmentsList
set segmentCount to the length of thisSegmentList
tell application "Notes"
tell account 1
tell topLevelFolder
if the segmentCount is greater than or equal to 1 then
if not (exists folder (item 1 of thisSegmentList)) then ¬
make new folder with properties {name:(item 1 of thisSegmentList)}
if the segmentCount is greater than or equal to 2 then
tell folder (item 1 of thisSegmentList)
if not (exists folder (item 2 of thisSegmentList)) then ¬
make new folder with properties ¬
{name:(item 2 of thisSegmentList)}
if the segmentCount is greater than or equal to 3 then
tell folder (item 2 of thisSegmentList)
if not (exists folder (item 3 of thisSegmentList)) then ¬
make new folder with properties ¬
{name:(item 3 of thisSegmentList)}
if the segmentCount is greater than or equal to 4 then
tell folder (item 3 of thisSegmentList)
if not (exists folder (item 4 of thisSegmentList)) then ¬
make new folder with properties ¬
{name:(item 4 of thisSegmentList)}
end tell
end if
end tell
end if
end tell
end if
end if
end tell
end tell
end tell
end repeat
tell application "Notes"
display dialog "Process completed." buttons {"OK"} default button 1 with icon 1
end tell

TOP | CONTINUE