1

hope I can find some help. I'm a total newbie at AppleScript.

I have put together a script that will copy the text I have highlighted on Safari, and paste it into a specific Note with just a keyboard shortcut.

However, I realised when the text gets pasted into the Note, the size is almost like the default font size, which is tiny. I have tried changing the default font size under Note > Preferences, but the pasted text causes the sizes of the other text to get smaller and smaller every time I run the script, until all reaches the smallest size of 8.

Is there any script I can include to fix it at the system default when the text is pasted in?

Here is the script I have written:

-- Copy selected text to clipboard:
tell application "System Events" to keystroke "c" using {command down}
delay 1


set noteName to "My Note"

set entry to return & (the clipboard)

tell application "Notes"         
    tell account "iCloud"
        set body of note noteName to (get body of note noteName & entry)
    end tell    
end tell

1 Answer 1

1

This may not be the perfect solution but this following AppleScript will copy the selected text and append that to your Note. It will then change the font size of all of that Note's content to the size you want.

All you need to do is insert the name of your Note and the desired font size of all content in that Note... In the first line of the following code.

Solution 1:

addToNotesAndEditFontSize("My Note", 12) -- Insert Note Name & Size For All Fonts

on addToNotesAndEditFontSize(noteName, fontSize)
    tell application "System Events" to keystroke "c" using {command down}
    delay 0.1
    tell application "Notes" to set body of note noteName to ¬
        body of note noteName & linefeed & (the clipboard of me)
    tell application "Notes" to set noteBody to body of note noteName
    set editedNoteBody to (do shell script "echo " & quoted form of ¬
        noteBody & " |sed -E 's@font-size: .px|font-size: ..px@font-size: " & ¬
        fontSize & "px@g'")
    tell application "Notes" to set body of note noteName to editedNoteBody
end addToNotesAndEditFontSize

Obsessively taking this project one step further... This following AppleScript displays a dialog allowing you to choose a Note to append the clipboard to, from a list of all your NON password protected Notes. Then will copy the selected text and append that to your Note. Then displays a dialog allowing you to insert your Note's text size. It will then change the font size of all of that Note's content to the size you want.

Solution 2:

Now you can target any note you want without having to edit any of the code.

tell application "System Events" to keystroke "c" using {command down}
delay 0.1

tell application "Notes" to set allNoteNames to name of every note ¬
    whose password protected is false

activate
set noteName to (choose from list allNoteNames ¬
    with title "Choose A Note" with prompt ¬
    "Choose A note to be edited" OK button name ¬
    "OK" cancel button name "Cancel") as text
if noteName is "false" then return

activate
set fontSize to text returned of (display dialog ¬
    "Insert desired text size (Numbers Only)" default answer ¬
    "12" buttons {"Cancel", "OK"} default button 2 ¬
    cancel button 1 with title "Choose Font Size") as number

addToNotesAndEditFontSize(noteName, fontSize)

on addToNotesAndEditFontSize(noteName, fontSize)
    tell application "Notes" to set body of note noteName to ¬
        body of note noteName & linefeed & linefeed & (the clipboard of me)
    tell application "Notes" to set noteBody to body of note noteName
    set editedNoteBody to (do shell script "echo " & quoted form of ¬
        noteBody & " |sed -E 's@font-size: .px|font-size: ..px@font-size: " & ¬
        fontSize & "px@g'")
    tell application "Notes" to set body of note noteName to editedNoteBody
end addToNotesAndEditFontSize

enter image description here enter image description here


Solution 3:

As per per request from a comment...

Would it be possible to choose a certain note, and make the system remember my selection for that particular note name for the entire day without having me to select it every time (since I will be pasting into the same note over a period of time?

The first time running this following version of the code, you'll be asked to choose the note to edit and the font size... Just like in the code in the above Solution 2. Then your choices will be written to a temporary file named "notesTMP.txt". This is the path to the file... "Macintosh HD:private:tmp:notesTMP.txt".

As long as the "notesTMP.txt" file exists, now every time you run this code, you will no longer have to choose the Note or it's font size. The code reads that "notesTMP.txt" file for the Note name and font size and continues on.

Don't quote me on this but I'm pretty sure that files in your "Macintosh HD:private:tmp" folder are considered temporary files by the system and if a file in that folder has not been edited in the last 24 hours, it automatically gets deleted after a reboot. If you need to reset the code so it asks you again to choose the Note and font size... Just delete the "Macintosh HD:private:tmp:notesTMP.txt" file yourself.

property tempFile : "Macintosh HD:private:tmp:notesTMP.txt"

tell application "System Events" to keystroke "c" using {command down}
delay 0.1

try
    alias tempFile
on error errMsg number errNum
    tell application "Notes" to set allNoteNames to name of every note ¬
        whose password protected is false
    activate
    set noteName to (choose from list allNoteNames ¬
        with title "Choose A Note" with prompt ¬
        "Choose A note to be edited" OK button name ¬
        "OK" cancel button name "Cancel") as text
    if noteName is "false" then return
    activate
    set fontSize to text returned of (display dialog ¬
        "Insert desired text size (Numbers Only)" default answer ¬
        "12" buttons {"Cancel", "OK"} default button 2 ¬
        cancel button 1 with title "Choose Font Size") as number
    addToNotesAndEditFontSize(noteName, fontSize)
    do shell script "echo " & quoted form of noteName & ¬
        " > " & quoted form of POSIX path of tempFile
    do shell script "echo " & fontSize & " >> " & ¬
        quoted form of POSIX path of tempFile
    return
end try

set noteName to paragraph 1 of (read alias tempFile)
set fontSize to paragraph 2 of (read alias tempFile) as number

try
    addToNotesAndEditFontSize(noteName, fontSize)
on error errMsg number errNum
    display alert "Choose Different Note" message ¬
        "Your original Note no longer exists. Please choose a different Note." as ¬
        critical buttons {"Cancel", "OK"} default button 2 cancel button 1
    do shell script "rm " & quoted form of POSIX path of tempFile
    run me
end try

on addToNotesAndEditFontSize(noteName, fontSize)
    tell application "Notes" to set body of note noteName to ¬
        body of note noteName & linefeed & linefeed & (the clipboard of me)
    tell application "Notes" to set noteBody to body of note noteName
    set editedNoteBody to (do shell script "echo " & quoted form of ¬
        noteBody & " |sed -E 's@font-size: .px|font-size: ..px@font-size: " & ¬
        fontSize & "px@g'")
    tell application "Notes" to set body of note noteName to editedNoteBody
    do shell script "afplay '/System/Library/Sounds/Glass.aiff'"
end addToNotesAndEditFontSize
11
  • Wow thank you @wch1zpink !! That works like a charm! I was wondering why your code is so much more complicated than mine 😅 If you have a bit of time to spare, could you explain very briefly what happens from tell application "Notes" to set body of note noteName to ¬ onwards? I got lost there and can't make out what the code is trying to do :( Thank you for your time.
    – gnohz
    Sep 1, 2022 at 4:53
  • Thank you for the explanation!! Now I got a better understanding :) Yes I have already upvoted and marked your solution as the accepted answer! Thank you again. This will save me a lot of time in the long run! :))
    – gnohz
    Sep 2, 2022 at 1:59
  • Waoow! I just saw the updated post! This is amazing! :DD I have a tiny request. Would it be possible to choose a certain note, and make the system remember my selection for that particular note name for the entire day without having me to select it every time (since I will be pasting into the same note over a period of time? Sorry if I'm asking too much ^^; If it's too troublesome, then it's fine! :)
    – gnohz
    Sep 2, 2022 at 2:23
  • Thank you so much! No hurry at all. I appreciate it :))
    – gnohz
    Sep 2, 2022 at 2:39
  • 1
    So sorry for another comment! I think I managed to solve my own problem by adding as «class utf8» so that piece of code looks like this: set noteName to paragraph 1 of (read alias tempFile as «class utf8») 😀 Thank you so much again!! Everything works like a charm :D
    – gnohz
    Sep 3, 2022 at 6:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .