Scarvy / readwise-to-apple-notes

Export Readwise highlights to Apple Notes.
Apache License 2.0
14 stars 0 forks source link

`subprocess.CalledProcessError` exporting highlights to Apple Notes #5

Closed Scarvy closed 1 week ago

Scarvy commented 1 week ago

Some highlights cause errors when running

subprocess.run(
    ["osascript", "-e", apple_script],
    check=True,
    stdout=subprocess.DEVNULL,
)

I have a hunch because of single/double quotes in the title or highlighted text.

Scarvy commented 1 week ago

Yes, it was the title with quotes that caused the errors. Fixed here.

clean_title = _escape_for_applescript(book["title"])
clean_text = _escape_for_applescript(highlight["text"])
clean_note = _escape_for_applescript(highlight["note"])

if highlight["note"].startswith(".h"):
    apple_script = f"""
        tell application "Notes"
        -- Ensure the "Readwise" folder exists
        set folderName to "Readwise"
        set theFolder to missing value
        repeat with eachFolder in folders
            if name of eachFolder is folderName then
                set theFolder to eachFolder
            end if
        end repeat
        if theFolder is missing value then
            set theFolder to (make new folder with properties {{name:folderName}})
        end if

        -- Attempt to find the note by title in the "Readwise" folder
        set theNote to missing value
        repeat with eachNote in (notes of theFolder)
            if name of eachNote is "{clean_title}" then
                set theNote to eachNote
                exit repeat
            end if
        end repeat

        -- If found, append new content, otherwise create the note
        if theNote is not missing value then
            set the body of theNote to the body of theNote & ¬
            "<br>" & "<b>{clean_text}</b>"
        else
            make new note at theFolder with properties {{name:"{clean_title}", body:"{clean_text}"}}
        end if
    end tell
    """

else:
      highlight_tags = ", ".join(tag["name"] for tag in highlight["tags"])
      book_tags = ", ".join(tag["name"] for tag in book["book_tags"])

      apple_script = f"""
      tell application "Notes"
          -- Ensure the "Readwise" folder exists
          set folderName to "Readwise"
          set theFolder to missing value
          repeat with eachFolder in folders
              if name of eachFolder is folderName then
                  set theFolder to eachFolder
              end if
          end repeat
          if theFolder is missing value then
              set theFolder to (make new folder with properties {{name:folderName}})
          end if

          -- Attempt to find the note by title in the "Readwise" folder
          set theNote to missing value
          repeat with eachNote in (notes of theFolder)
              if name of eachNote is "{clean_title}" then
                  set theNote to eachNote
                  exit repeat
              end if
          end repeat

          -- If found, append new content, otherwise create the note
          if theNote is not missing value then
              set the body of theNote to the body of theNote & ¬
              "<br>" & "{clean_text}" & " (Location {highlight["location"]})" & ¬
              "<br><br>" & "<b>Note</b>: {clean_note}" & ¬
              "<br>" & "<b>Tags</b>: {highlight_tags}" & ¬
              "<br>"
          else
              make new note at theFolder with properties {{name:"{clean_title}", body:"<br>Title: {clean_title}<br>Category: {book["category"]}<br>Document Tags: {book_tags}"}}
          end if
      end tell
      """