Your original script above seems to be one by Shane Stanley, from here on Mac OS X Automation.
What you are requesting is not as easy as it seems. The problem is that in AppleScript the body
property of a note seems to accept only HTML text; RTF markup doesn’t get interpreted.
It’s not a problem to get the HTML clipboard flavor, but depending on the selection you have copied, the result will look different from what it looks on the source web page. (The HTML on the clipboard is the HTML “snippet” of the selection, and will not include other elements that affect the rendering on the web page. Also in most cases images will miss.)
I also experimented with getting the RTF clipboard and converting it to HTML, but the results are similar.
Nevertheless I post the script, because it can be useful for example when you select single paragraphs or a code block with syntax highlighting.
Script 1 (pure HTML clipboard)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set pb to current application's NSPasteboard's generalPasteboard()
set theData to pb's dataForType:(current application's NSPasteboardTypeHTML)
set noteHTMLText to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
set noteHTMLText to noteHTMLText as text
tell application "Notes"
activate
set thisAccountName to my getNameOfTargetAccount("Choose an account:")
display dialog "Enter the title for the new note:" default answer ¬
"New Note" with icon 1 with title "New Note with Clipboard"
set the noteTitle to the text returned of the result
tell account thisAccountName
make new note at folder "Notes" with properties {name:noteTitle, body:noteHTMLText}
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 modifications are all at the beginning of the script, the rest is like the original.
In most cases the second script will be more useful, since it gives you the same results as when pasting “natively” (⌘V).
It is basically a UI script: When you run it, the script creates a new note (unchanged from the original script). Once you click OK, it simply simulates the necessary keystrokes to paste the clipboard into the note. That’s all.
Script 2 (simulating ⌘V)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Notes"
activate
set thisAccountName to my getNameOfTargetAccount("Choose an account:")
display dialog "Enter the title for the new note:" default answer ¬
"New Note" with icon 1 with title "New Note with Clipboard"
set the noteTitle to the text returned of the result
tell account thisAccountName
make new note at folder "Notes" with properties {name:noteTitle}
show note 1
end tell
end tell
tell application "System Events"
key code 124 using command down
key code 125
key code 36
key code 9 using command down
key code 36
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
Experiment with both scripts. As said, in my tests the second script worked better in most cases, especially with images. But in some cases text formatting was better preserved with the pure HTML script (script 1).
Sorry that I cannot come up with something better at the moment. Maybe another member has a better idea…