on run {input, parameters}
repeat with |file| in input
set |note name| to ""
tell application "Finder"
set |note name| to name of |file|
set AppleScript's text item delimiters to "."
set |note name| to first text item of |note name|
set AppleScript's text item delimiters to ""
end tell
tell application "Notes"
set |note body| to "<body><h1>" & |note name| & "</h1></body>"
set |new note| to make new note with properties {body:|note body|}
make new attachment of |new note| with data file |file|
end tell
end repeat
return input
end run
This add the first page of the PDF file only and show a huge empty space before the attachment.
Do I have to define the type of file I’m attaching to the note?
Your question is asked quite often, and in my opinion it has not yet been answered anywhere. I’ll show you the solution that came to mind after reading the Notes.app dictionary.
For the Automator, you will remake my snippet yourself (actually, there is another section for questions related to Automator).
set thePDF to (choose file of type "com.adobe.pdf") as text
tell application "Finder" to set |note name| to name of file thePDF
set |note body| to "<body><h1>" & |note name| & "</h1></body>"
tell application "Notes"
set |new note| to make new note with properties {body:|note body|}
make new attachment at end of attachments of note named |note name| with data (file thePDF)
end tell
Other Example:
set thePDF to (choose file of type "com.adobe.pdf") as text
set |note name| to "Test Note 1"
set |red paragraph| to "Blah, blah, blah"
set |note body| to "<body><h1 style=\"color:green;\">" & |note name| & "</h1>
<p style=\"color:red;\" >" & |red paragraph| & "</p>
</body>"
tell application "Notes"
set |new note| to make new note with properties {body:|note body|}
make new attachment at end of attachments of note named |note name| with data (file thePDF)
end tell
It would be nice if you showed the script you were testing this time. Because both of my scripts work correctly on my MAC. In general, have you tried my scripts as is?
I used your code. My MacBook Air is an M1 with the latest version of the system, maybe it has a bug in Notes or something has changed in the new version.
I’m getting the same effect in Mojave that Luiz is seeing. Also the new note’s name has to be set explicitly and, since several notes can have the same name, it’s a good idea to specify attachments of |new note| rather than attachments of note named |note name|.
set thePDF to (choose file of type "com.adobe.pdf") as text
set |note name| to "Test Note 1"
set |red paragraph| to "Blah, blah, blah"
set |note body| to "<body><h1 style=\"color:green;\">" & |note name| & "</h1>
<p style=\"color:red;\" >" & |red paragraph| & "</p>
</body>"
tell application "Notes"
activate
set |new note| to make new note with properties {name:|note name|, body:|note body|}
make new attachment at end of attachments of |new note| with data (file thePDF)
end tell
The PDF image can be moved up to the required location by deleting the page-sized empty line above it, but admittedly this isn’t ideal. Is there a way to get round this in the HTML? Another thing I’m finding is that if the PDF has more than one page, only the first appears in the note.
Nigel Garvey make sure you really managed to remove the space, every time I come back to see the note the space Reappears.
The current behavior of Notes.app when attaching a PDF is to show only the first part of the page, however when you click with the course the entire document appears in a new window.
I don’t particularly like this behavior, there could be some number indicating other pages or even the rendering of one page over the other, I think Apple should improve this soon.
Nigel, on the Catalina, your script throws error, because making new note doesn’t create a proper (object) reference to it. This behaviour was reason I used the syntax “of note |note name|”. The best choice is determining the ID of new note, then using syntax “of note id |note id|”
Here is, what I have after executing my 2nd script. It is file icon, and not embedded PDF. I think, this is correct behaviour, and embedding PDF is some bug:
Today I tested it and I’m having the same behavior described above, including that which refers to the weird space. “ If I delete the weird space it does not return”.
I wonder if it is possible to remove this weird space after attaching the file.
Not sure I can help you, because on Catalina the behaviour is different from your system. Following script:
set thePDF to (choose file of type "com.adobe.pdf") as text
set |note name| to "Test Note 1"
set |red paragraph| to "Blah, blah, blah"
set |note body| to "<body><h1 style=\"color:green;\">" & |note name| & "</h1>
<p style=\"color:red;\" >" & |red paragraph| & "</p>
</body>"
tell application "Notes"
make new note with properties {body:|note body|}
make new attachment at end of attachments of note |note name| with data (file thePDF)
return body of note |note name|
end tell
This works for me in Mojave — today at least! The ‘make’ and ‘show’ lines won’t work in Catalina, to judge from KniazidisR’s report above. I’ve no idea how the GUI Scripting section will fare on non-Mojave systems.
tell application "Notes"
activate
set thePDF to (choose file of type "com.adobe.pdf") as text
set |note name| to "Test Note 1"
set |red paragraph| to "Blah, blah, blah"
set |note body| to "<body><h1 style=\"color:green;\">" & |note name| & "</h1>
<p style=\"color:red;\" >" & |red paragraph| & "</p>
</body>"
set |new note| to make new note with properties {name:|note name|, body:|note body|}
make new attachment at end of attachments of |new note| with data (file thePDF)
show |new note|
end tell
tell application "System Events"
tell application process "Notes"
set frontmost to true
-- Make sure the cursor's in the note content column.
set focused of text area 1 of scroll area 1 of group 2 of splitter group 1 of splitter group 1 of window 1 to true
delay 0.3
-- Move the cursor to the bottom of the column.
key code 125 using {command down}
delay 0.3
-- Cursor up one line, left to end of previous line, delete.
key code {126, 123, 51}
end tell
end tell
I found a way to reliably attach a file or image to arbitrary places inside a note in Notes.app. See the code at Need help making GUI scripting AppleScript robust where I’ve asked for help to make the solution more robust.
Resurrecting this because, with macOS Sonoma (and iOS 17), we learn that this “weird empty space” is, in fact, a duplicate of the attachment.
AND, much worse, opening notes created this way in those operating systems causes Notes to crash unceremoniously, making these kinds of Notes (and the path to creating them) now useless instead of simply “annoying”.
I’m still on Ventura and can’t speak to the oddities in Sonoma, but this works well for me here. Customize the PDF source as you like.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Safari"
tell its document 1
set pageName to name
set pageURL to URL
end tell
end tell
set SaveFolderPath to "~/Desktop/"
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "Export as PDF…" of menu "File" of menu bar 1
repeat until exists sheet 1 of window 1
delay 0.2
end repeat
keystroke "tempPDF"
delay 0.2
keystroke "d" using command down
delay 0.2
keystroke return
end tell
end tell
tell application "Finder"
delay 1
set attPath to ((path to desktop folder from user domain as text) & "tempPDF.pdf") as alias
end tell
tell application "Notes"
activate
set theNote to make new note in folder "Testing" with properties {name:pageName}
tell theNote
delete attachments
set b to pageName & ":" & pageURL
set body to b
make new attachment at end of attachments with data attPath
delete attachment 1
end tell
end tell
tell application "Finder"
delete attPath
end tell