Scripting Notes: The Attachment Class

In the Notes scripting dictionary, the attachment class is found in the Notes suite. It contains a few standard properties: name, id, and container; and one specialized property: content identifier; and contains no other elements.

attachment n : an attachment in a note

elements

contained by application, notes.

properties

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

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

container (note, r/o) : the note containing the attachment

content identifier (text) : the content-id URL in the note's HTML.

responds to

count, delete, exists, save

Example Scripts

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

The first collection of script samples, deal with the management of attachments:

Click to open example in the Script Editor application Save All Image Attachments
This script will save all of the image attachments to a chosen folder in the Finder.
tell application "Notes"
activate
set the accountCount to the count of accounts
if accountCount is 1 then
set the dialogMessagePartA to ¬
"This script will save the image attachments of all of the notes of the current account."
set the dialogMessagePartB to ¬
"Click the “Continue” button to choose a destination folder for the saved images."
else
set the dialogMessagePartA to ¬
"This script will save the image attachments of all of the notes of a chosen account."
set the dialogMessagePartB to ¬
"Click the “Continue” button to choose the source account," & space & ¬
"and a destination folder for the saved images."
end if
display dialog dialogMessagePartA & linefeed & linefeed & ¬
dialogMessagePartB buttons {"Cancel", "Continue"} default button 2 with icon 1

set thisAccountName to (my getNameOfTargetAccount("Choose an account:"))

set the targetFolderHFSPath to ¬
(choose folder with prompt ¬
"Choose the folder to contain the saved image attachments:") as string

set allImageAttachments to ¬
every attachment whose name ends with ".jpg" or name ends with ".png"
repeat with i from 1 to the count of allImageAttachments
set thisAttachment to (item i of allImageAttachments)
if accountCount is 1 then
-- no need to check to see if attachment is contained by note in a specified account
set thisAttachmentName to the name of thisAttachment
set the newFileHFSPath to ¬
(my deriveTargetItemHFSPath(thisAttachmentName, targetFolderHFSPath))
save thisAttachment in file newFileHFSPath
else
set thisItem to thisAttachment
-- walk up the container chain until the account container is reached
repeat
set thisContainer to the container of thisItem
if (the class of thisContainer) is account then
if the name of thisContainer is thisAccountName then
set thisAttachmentName to the name of thisAttachment
set the newFileHFSPath to ¬
(my deriveTargetItemHFSPath(thisAttachmentName, targetFolderHFSPath))
save thisAttachment in file newFileHFSPath
end if
exit repeat
else
set thisItem to thisContainer
end if
end repeat
end if
end repeat

display dialog "Saving process completed." buttons {"Show Saved Files", "Done"} ¬
default button 2 with icon 1
set the userChoice to the button returned of the result
end tell
if the userChoice is "Show Saved Files" then
tell application "Finder"
activate
open folder targetFolderHFSPath
end tell
end if

on deriveTargetItemHFSPath(thisItemName, targetFolderHFSPath)
set fileExtensionDelimiterOffset to the offset of "." in ¬
(the reverse of (every character of thisItemName)) as string
set thisItemBaseName to text 1 thru -(fileExtensionDelimiterOffset + 1) of thisItemName
set thisItemFileExtension to text -(fileExtensionDelimiterOffset - 1) thru -1 of thisItemName
set the counter to 1
set targetItemName to thisItemName
repeat
tell application "Finder"
if not (exists item targetItemName of folder targetFolderHFSPath) then
return (targetFolderHFSPath & targetItemName)
else
set targetItemName to thisItemBaseName & "-" & ¬
(counter as string) & "." & thisItemFileExtension
set counter to counter + 1
end if
end tell
end repeat
end deriveTargetItemHFSPath

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 Delete Every Attachment
This example demonstrates the use of the delete verb with the notes application element.
tell application "Notes"
set the attachmentCount to the count of attachments
display dialog "This script will attempt to delete " & attachmentCount & " attachments(s)." & ¬
linefeed & linefeed & "This action cannot be undone." buttons ¬
{"Cancel", "Continue"} default button 1 with icon 2
delete every attachment
display dialog "All attachments deleted." buttons {"OK"} default button 1 with icon 1
end tell

TOP | CONTINUE