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
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
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