8 Replies Latest reply: Jun 3, 2012 1:11 PM by Roman1
Roman1 Level 1 (25 points)

I'd much appreciate such script or any suggestions as how to achieve that. I have hundreds of notes and decided to change my tagging system.


Thanks a loT!

  • twtwtw Level 5 (4,925 points)

    Are ou talking about notes in Mail?

  • Roman1 Level 1 (25 points)


  • twtwtw Level 5 (4,925 points)

    There's no way to modify notes through mail (you can access them, but the content is read only).  The only way to do what you want that I can see is to close Mail.app and delve down into the ~/Library/Mail folder.  In there you'll folders for all of your mail accounts and a folder called Mailboxes (In Lion they'll be inside a folder called V2).  Account based notes are in the account folder, generic notes are the Mailboxws folder, but either way they'll be in a folder called Notes.  you'll need to navigate through a series of folders till you get the the emlx files, and then modify the html directly.


    Sorry, but you haven't given enough detail for anything more than that sketchy outline of the process.

  • Roman1 Level 1 (25 points)

    Thank you for your input and apologies for not writing more about my issue.


    I'm a heavy user of iphone/ipad and use the built in notes application for basically all my notes, files, writing, whatever can do with text is put to text and synced across my mac/iphone/ipad. I tried other ways, but this method always wins as I don't have to use cloud for syncing and notes app is very polished, etc...


    I use tagging system to search in the many notes I have and need to change tagging from e.g. #essay to kbEssay for better searching. So I'd like to search all my notes for #essay, for instance, and exchange that one word for kbEssay.


    I could use jEdit and do it in the unix mail files, but thought a nice AppleScript would be more elegant.




  • twtwtw Level 5 (4,925 points)

    Unfortunately, this is different than what you originally said, and not something I can help you with.  I don't have an iPad, and have no idea how its Notes app works.  You might try reposting this question over in the iPad forum.

  • Roman1 Level 1 (25 points)

    It's not. iOS is irrelevant in this discussion, but you asked for background. Mail.app notes in Mac OS X is the only relevant part.

  • twtwtw Level 5 (4,925 points)

    Well, color me confused.  But if you're just concerned about Mail notes, here's a script that uses the example you gave above.


    if application "Mail" is running then

      -- mail should not be running when you use this script

              display alert "Whoops!" message "Please quit Mail.app before running this script" as critical


    end if


    tell application "Finder"

      -- locate the correct mail folder for the given account


                        set accountFolder to folder "IMAP-your.account@somewhere.net" of folder "V2" of folder "Mail" of folder "Library" of home

              on error

      -- folders changed from snow leopard to lion, check the folder hierarchy visually

                        display alert "Whoops!" message "something's wrong with the folder hierarchy." as critical

                        open folder "Mail" of folder "Library" of home


              end try

              tell accountFolder

                        with timeout of 600 seconds

      -- gather notes file references. set a long timeout because this can take a while if there are a large number of notes

                                  set theNotes to (every file of (entire contents of folder "Notes.mbox") whose name extension is "emlx") as alias list

                        end timeout

              end tell

    end tell


    -- set up keywords to be swapped out here.

    set swapPairs to {{"#essay", "kbEssay"}, {"#another key", "its replacement"}}


    repeat with thisNote in theNotes

      -- open and read the emlx file into a variable

              set fp to open for access thisNote with write permission

              set noteHTMLText to read fp

      -- loop through the swap pairs making replacements

              repeat with thisPair in swapPairs

                        set noteParts to tid(noteHTMLText, first item of thisPair)

                        set noteHTMLText to tid(noteParts, second item of thisPair)

              end repeat

      -- zero out the file and replace with the revised text

              set eof fp to 0

              write noteHTMLText to fp

              close access fp

    end repeat


    on tid(input, delim)

      -- generic text items handler

              set {oldTID, my text item delimiters} to {my text item delimiters, delim}

              if class of input is list then

                        set output to input as text


                        set output to text items of input

              end if

              set my text item delimiters to oldTID

              return output

    end tid


    I've tested this on my laptop and it works, but I STRONGLY suggest that you back up your mail folder before trying it yourself, and (if possible) that you set up a few notes in a little used or dummy account and test it before you run it on your actual notes. 


    I also make no guarantees about what will happen if these notes are synched to other devices.  It probably won't explode your computer, but...

  • Roman1 Level 1 (25 points)

    Wow! You're amazing! Thank you for all your time and knowledge. I will try to understand the script first and then run it.


    I like your phrase "color me confused". All I meant was, that since all devices are synced, I need to worry only about my mac to have the final version, which will then sync to the others automatically after I connect them to iTunes. I now realize I'm going to loose the dates of the notes, but I tend to write those at the end of each note anyway.


    Nice day!