I’m sure this has been implemented many times but I had a hard time tracking down a simple script for it, so I’m sharing my working solution here. See the links below as well as the sample script.
Basically wanted to have a “one button scan to Apple Notes” option (scan snap official software has a “scan to Evernote” function but nothing similar for Apple Notes). Implemented it via folder actions so that any file dropped into a folder is imported to a new note as an attachment and I just point the scanning software to “save scan to folder” automatically.
(*
NAME:
Notes Inbox
SUMMARY:
Auto-import scans, images, etc. as attachments of a new note in Apple Notes by way of folder actions.
Any file dropped into the selected folder in Finder will be imported into the target folder in Apple Notes.
INSTALL:
1. Copy this script to "~/Library/Scripts/Folder Action Scripts" (or "/Library/Scripts/Folder Action Scripts")
2. In Finder, create a new folder named something like "Notes Inbox" (inside your home folder, for example)
3. Right click on new folder and choose "Services > Folder Actions Setup" (or open the setup utility directly from "/System/Library/CoreServices/Applications/Folder Actions Setup.app")
4. Allow service to run if prompted
5. Make sure "Enable Folder Actions" is checked
6. Add the new "Notes Inbox" in the left side folder list (if not already there and selected)
7. Add the "Notes Inbox" script to the right side script selection (and make sure checked)
CONFIG:
Change the "notePrefix" and "notesFolder" properties as desired.
notePrefix could be something like "Scan: " if you wish to prefix that to anything processed by this script.
notesFolder is "Inbox" by default and is the name of the folder in Apple Notes where incoming notes will be created.
CF:
https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_folder_actions.html
AUTHOR:
Ethan Schoonover
es@ethanschoonover.com
github: altercation
*)
property notePrefix : ""
property notesFolder : "Inbox"
on processFile(fileToProcess)
set theFile to fileToProcess as text
tell application "Finder" to set noteName to name of file theFile
set timeStamp to short date string of (current date) as string
set noteBody to "<body><h1>" & notePrefix & noteName & "</h1><p>Imported on: " & timeStamp & "</p></body>"
tell application "Notes"
if not (exists folder notesFolder) then
make new folder with properties {name:notesFolder}
end if
set newNote to make note at folder notesFolder with properties {body:noteBody}
make new attachment at end of attachments of newNote with data (file theFile)
(*
Note: the following delete is a workaround because creating the attachment
apparently creates TWO attachements, the first being a sort of "ghost" attachment
of the second, real attachment. The ghost attachment shows up as a large empty
whitespace placeholder the same size as a PDF page in the document and makes the
result look empty
*)
delete first attachment of newNote
show newNote
end tell
tell application "Finder" to delete file theFile
end processFile
on adding folder items to this_folder after receiving added_items
try
repeat with added_item in added_items
processFile(added_item)
end repeat
on error errText
display dialog "Error: " & errText
end try
end adding folder items to
(*
the following runs only when executing this script manually via script editor for testing purposes, simulating
incoming files by allowing the user to select a file in lieu of an actual triggered folder action
*)
processFile(choose file)
First you should make example so you know how Folder Action Script is working.
This example will display every item on the screen that you add to Folder
ex.
(**
* Reference:
* https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_folder_actions.html
*)
(**
* [First example of Folder Action to display every item]
*)
on adding folder items to this_folder after receiving these_items
-- make list of items to become string of every item include return
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space & return & return}
set theString to (text items of these_items) as text
set AppleScript's text item delimiters to ASTID
display dialog theString
end adding folder items to
Now you know how its able to get the items.
So how could we do it in Notes. You could use set body property of object note. This involve to
make the image data to be base64 encoded. In other words embedded into note object. This could be done in AppleScriptObjC.
So is there an better way, yes its… you could use an mix of scriptable application of Notes
and use GUI scripting with System Events. To use the below script you need to copy image object first or set the clipboard to <path_to_file>
Make new note object.
Show the note object.
Set focus of text area to true
Paste the object
ex.
its makeNewNoteWithName("My Title")
its pasteObject()
on makeNewNoteWithName(noteName)
tell application "Notes"
set defaultAccount to default account
set noteObject to make new note in defaultAccount
tell noteObject
set body to noteName
end tell
show note noteName
end tell
end makeNewNoteWithName
on pasteObject()
tell application "System Events" to tell application process "Notes"
set frontmost to true
set focused of (text area 1 of scroll area 1 of group 1 of ¬
splitter group 1 of splitter group 1 of window 1) to true
key code 9 using {command down}
end tell
end pasteObject
When you understand my approach you could include everything in Folder Action Script.
You maybe need some delay to Notes.app could start before it make new note.
So the result could be like this…
I have test this script by copy image from Finder. I had Notes.app open already.
When I paste the image inside the Folder Action it will make new Notes with the image.
(**
* Reference:
* https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_folder_actions.html
*)
(**
* [First example of Folder Action to display every item]
*)
on adding folder items to this_folder after receiving these_items
repeat with anItem in these_items
its makeNewNoteWithName("My Image")
set the clipboard to anItem as «class furl»
delay 1
pasteObject()
end repeat
end adding folder items to
on makeNewNoteWithName(noteName)
tell application "Notes"
set defaultAccount to default account
set noteObject to make new note in defaultAccount
tell noteObject
set body to noteName
end tell
show note noteName
end tell
end makeNewNoteWithName
on pasteObject()
tell application "System Events" to tell application process "Notes"
set frontmost to true
set focused of (text area 1 of scroll area 1 of group 1 of ¬
splitter group 1 of splitter group 1 of window 1) to true
key code 9 using {command down}
end tell
end pasteObject
I am hoping to use this to sync with various PDF editors.
Quick question,
-Is there a way to check if there is same named file already in Apple Note and update it rather than just create new?
-And instead of deleting it, could it keep the file in the folder?
Following two changes will allow various apps to be integrated with Apple Note. In my instance, it would be my E-ink tablet that auto uploads PDFs.
I think GoodNotes have auto pdf upload option as well.