Saturday, October 25, 2014

#1 2012-07-26 07:51:52 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Import files into the Notes.app in Mountain Lion

The new Notes.app in Mountain Lion is scriptable which is great. I wanted to import some files into Notes.app so I wrote this script.

Notice that this script is setup for an "iCloud" account and the notes are added to a folder called "Import Folder". You'll also notice that this converts the file's contents into html code. It seems notes can take plain text but in my testing it doesn't recognize line breaks and paragraphs in plain text. As such I convert all file types into html code and then send that to Notes because Notes natively understands html anyway.

This script will work on any file type that textutil can understand which is a lot (txt, rtf, rtfd, html, word files etc.).

One bad thing... although we give the new note a name you will notice that the name will change to the first line of text once you start editing it. This can't be helped because for some reason this is how Notes.app gets the names of a note. So just realize that once you edit a new note the name may change if the first line of the note is not the same as the name you assign to it.

One other caveat... if your file has images embedded in it this will not work with those images correctly. I suggest you drag/drop your images into the note later by hand. If anyone wants to update this script to handle images then by all means make that improvement.

Good luck!

Applescript:

-- this imports files into Notes.app in MacOSX 10.8 Mountain Lion
-- it converts files into a format compatible with Notes.app and creates a new note from that converted text
-- find lots of Notes scripts here: http://www.macosxautomation.com/applescript/notes/index.html

-- get a file to import to a note
set noteFile to choose file with prompt "Import which file?"

-- get the note name from the file name
set {noteName, ext} to getName_andExtension(noteFile)

-- get the name of the account that you want to add the note to
set accountName to getNameOfTargetAccount()

-- get the name of the folder to add the note to
set folderName to getNameOfTargetFolderOfAccount(accountName)

-- get the file's text as html
if ext is "html" then
   set noteText to read noteFile
else
   -- convert the file's contents into html code
   set noteText to do shell script "/usr/bin/textutil -stdout -convert html " & quoted form of POSIX path of noteFile & " | /usr/bin/tidy -ib -utf8"
end if

tell application "Notes"
   tell account accountName
       -- make sure the folder exists
       if not (exists folder folderName) then make new folder with properties {name:folderName}
       
       -- add the note to the folder
       tell folder folderName
           make new note with properties {name:noteName, body:noteText}
       end tell
   end tell
end tell


(****************** SUBROUTINES *****************)
-- this lets you choose which acount you want to target
-- if there's only 1 account then that account name is returned
on getNameOfTargetAccount()
   tell application "Notes"
       if (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 "Add note to which account?"
           if thisAccountName is false then error number -128
           set thisAccountName to thisAccountName as text
       else
           set thisAccountName to the name of first account
       end if
       return thisAccountName
   end tell
end getNameOfTargetAccount

-- this lets you choose which folder you want to target from an account
-- if there's only 1 folder then that folder name is returned
on getNameOfTargetFolderOfAccount(accountName)
   set folderName to missing value
   tell application "Notes"
       tell account accountName
           if (count of folders) is greater than 1 then
               set theseFolderNames to the name of every folder
               set folderName to choose from list theseFolderNames with prompt "Add note to which folder?"
               if folderName is false then error number -128
               set folderName to folderName as text
           else
               set folderName to the name of first folder
           end if
       end tell
   end tell
   return folderName
end getNameOfTargetFolderOfAccount

on getName_andExtension(f)
   set f to f as text
   set {name:nm, name extension:ex} to info for file f without size
   if ex is missing value then
       set ex to ""
   else
       set nm to text 1 thru ((count nm) - (count ex) - 1) of nm
   end if
   return {nm, ex}
end getName_andExtension

EDIT: Using some techniques learned from the link in Shane's post below, I made some modifications to make this script better.

Last edited by regulus6633 (2012-07-27 07:26:46 am)

Offline

 

#2 2012-07-26 10:26:27 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3706

Re: Import files into the Notes.app in Mountain Lion

Hank,

You might find this useful:

www.macosxautomation.com/applescript/notes/index.html

I haven't had a chance myself...


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2012-07-27 03:29:20 am

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: Import files into the Notes.app in Mountain Lion

Shane Stanley wrote:

Hank,

You might find this useful:

www.macosxautomation.com/applescript/notes/index.html

Thanks for the pointer Shane. I hadn't seen that and there's a lot of good information there that can be used to help tailor this script.

Offline

 

#4 2012-08-18 09:06:33 am

nl
Member
Registered: 2012-08-18
Posts: 1

Re: Import files into the Notes.app in Mountain Lion

Thanks, this is very helpful information.

I have been, however, unable to create an attachment for a note. I wonder if you have any insights as to how to do that?

Thanks.
nl

Offline

 

#5 2012-08-23 09:30:20 am

hotwheel67
Member
Registered: 2012-08-22
Posts: 1

Re: Import files into the Notes.app in Mountain Lion

Is it possible to modify the above script to allow selection of multiple text files? I need to import over 2000 files. I am not familiar with scripting but need a solution.
Any help would be much appreciated.
Thanks!

Offline

 

#6 2012-08-23 10:00:07 am

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: Import files into the Notes.app in Mountain Lion

hotwheel67 wrote:

Is it possible to modify the above script to allow selection of multiple text files? I need to import over 2000 files. I am not familiar with scripting but need a solution.
Any help would be much appreciated.
Thanks!

You can change the script to this. Now you choose a folder and all of the files in that folder (even subfolders) will be imported into Notes.

NOTE: I did not test this script but I think it will work. Good luck! big_smile

Applescript:

-- this imports all the files in a folder into Notes.app in MacOSX 10.8 Mountain Lion
-- it converts files into a format compatible with Notes.app and creates a new note from that converted text
-- find lots of Notes scripts here: http://www.macosxautomation.com/applescript/notes/index.html

-- choose the folder with the notes
set notesFolder to choose folder

-- find al the files in that folder
tell application "Finder"
   set noteFiles to (files of entire contents of notesFolder) as alias list
end tell

-- get the name of the account that you want to add the notes to
set accountName to getNameOfTargetAccount()

-- get the name of the folder to add the notes to
set folderName to getNameOfTargetFolderOfAccount(accountName)

-- make sure the folder exists in Notes
tell application "Notes"
   tell account accountName
       if not (exists folder folderName) then make new folder with properties {name:folderName}
   end tell
end tell

repeat with noteFile in noteFiles
   -- get the note name from the file name
   set {noteName, ext} to getName_andExtension(noteFile)
   
   -- get the file's text as html
   if ext is "html" then
       set noteText to read noteFile
   else
       -- convert the file's contents into html code
       set noteText to do shell script "/usr/bin/textutil -stdout -convert html " & quoted form of POSIX path of noteFile & " | /usr/bin/tidy -ib -utf8"
   end if
   
   -- add the note to the folder
   tell application "Notes"
       tell account accountName
           tell folder folderName
               make new note with properties {name:noteName, body:noteText}
           end tell
       end tell
   end tell
end repeat


(****************** SUBROUTINES *****************)
-- this lets you choose which acount you want to target
-- if there's only 1 account then that account name is returned
on getNameOfTargetAccount()
   tell application "Notes"
       if (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 "Add note to which account?"
           if thisAccountName is false then error number -128
           set thisAccountName to thisAccountName as text
       else
           set thisAccountName to the name of first account
       end if
       return thisAccountName
   end tell
end getNameOfTargetAccount

-- this lets you choose which folder you want to target from an account
-- if there's only 1 folder then that folder name is returned
on getNameOfTargetFolderOfAccount(accountName)
   set folderName to missing value
   tell application "Notes"
       tell account accountName
           if (count of folders) is greater than 1 then
               set theseFolderNames to the name of every folder
               set folderName to choose from list theseFolderNames with prompt "Add note to which folder?"
               if folderName is false then error number -128
               set folderName to folderName as text
           else
               set folderName to the name of first folder
           end if
       end tell
   end tell
   return folderName
end getNameOfTargetFolderOfAccount

on getName_andExtension(f)
   set f to f as text
   set {name:nm, name extension:ex} to info for file f without size
   if ex is missing value then
       set ex to ""
   else
       set nm to text 1 thru ((count nm) - (count ex) - 1) of nm
   end if
   return {nm, ex}
end getName_andExtension

Last edited by regulus6633 (2012-08-23 10:10:27 am)

Offline

 

#7 2012-08-30 04:31:36 pm

perfectsound
Member
Registered: 2012-08-27
Posts: 1

Re: Import files into the Notes.app in Mountain Lion

regulus6633 wrote:

hotwheel67 wrote:

Is it possible to modify the above script to allow selection of multiple text files? I need to import over 2000 files. I am not familiar with scripting but need a solution.
Any help would be much appreciated.
Thanks!

You can change the script to this. Now you choose a folder and all of the files in that folder (even subfolders) will be imported into Notes.

NOTE: I did not test this script but I think it will work. Good luck! big_smile

Applescript:

-- this imports all the files in a folder into Notes.app in MacOSX 10.8 Mountain Lion
-- it converts files into a format compatible with Notes.app and creates a new note from that converted text
-- find lots of Notes scripts here: http://www.macosxautomation.com/applescript/notes/index.html

-- choose the folder with the notes
set notesFolder to choose folder

-- find al the files in that folder
tell application "Finder"
   set noteFiles to (files of entire contents of notesFolder) as alias list
end tell

-- get the name of the account that you want to add the notes to
set accountName to getNameOfTargetAccount()

-- get the name of the folder to add the notes to
set folderName to getNameOfTargetFolderOfAccount(accountName)

-- make sure the folder exists in Notes
tell application "Notes"
   tell account accountName
       if not (exists folder folderName) then make new folder with properties {name:folderName}
   end tell
end tell

repeat with noteFile in noteFiles
   -- get the note name from the file name
   set {noteName, ext} to getName_andExtension(noteFile)
   
   -- get the file's text as html
   if ext is "html" then
       set noteText to read noteFile
   else
       -- convert the file's contents into html code
       set noteText to do shell script "/usr/bin/textutil -stdout -convert html " & quoted form of POSIX path of noteFile & " | /usr/bin/tidy -ib -utf8"
   end if
   
   -- add the note to the folder
   tell application "Notes"
       tell account accountName
           tell folder folderName
               make new note with properties {name:noteName, body:noteText}
           end tell
       end tell
   end tell
end repeat


(****************** SUBROUTINES *****************)
-- this lets you choose which acount you want to target
-- if there's only 1 account then that account name is returned
on getNameOfTargetAccount()
   tell application "Notes"
       if (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 "Add note to which account?"
           if thisAccountName is false then error number -128
           set thisAccountName to thisAccountName as text
       else
           set thisAccountName to the name of first account
       end if
       return thisAccountName
   end tell
end getNameOfTargetAccount

-- this lets you choose which folder you want to target from an account
-- if there's only 1 folder then that folder name is returned
on getNameOfTargetFolderOfAccount(accountName)
   set folderName to missing value
   tell application "Notes"
       tell account accountName
           if (count of folders) is greater than 1 then
               set theseFolderNames to the name of every folder
               set folderName to choose from list theseFolderNames with prompt "Add note to which folder?"
               if folderName is false then error number -128
               set folderName to folderName as text
           else
               set folderName to the name of first folder
           end if
       end tell
   end tell
   return folderName
end getNameOfTargetFolderOfAccount

on getName_andExtension(f)
   set f to f as text
   set {name:nm, name extension:ex} to info for file f without size
   if ex is missing value then
       set ex to ""
   else
       set nm to text 1 thru ((count nm) - (count ex) - 1) of nm
   end if
   return {nm, ex}
end getName_andExtension

I can't get this script to run. I get an error like this:

error "line 9 column 3 - Warning: trimming empty <style>
Info: Doctype given is \"-//W3C//DTD HTML 4.01//EN\"
Info: Document content looks like HTML 4.01 Strict
1 warning, 0 errors were found!


To learn more about HTML Tidy see http://tidy.sourceforge.net
Please send bug reports to html-tidy@w3.org
HTML and CSS specifications are available from http://www.w3.org/
Lobby your company to join W3C, see http://www.w3.org/Consortium" number 1

Offline

 

#8 2012-11-07 07:12:20 am

Chacapamac
Member
Registered: 2012-11-07
Posts: 2

Re: Import files into the Notes.app in Mountain Lion

Working but stopping on some txt files wit errors

Error
Warning trimming empty >span> Doctype blablabla


Need help on this one????

Last edited by Chacapamac (2012-11-07 07:30:29 am)

Offline

 

#9 2012-11-07 07:38:15 am

Chacapamac
Member
Registered: 2012-11-07
Posts: 2

Re: Import files into the Notes.app in Mountain Lion

Take out

& " | /usr/bin/tidy -ib -utf8"

Work perfect....

Offline

 

#10 2013-01-11 12:43:10 pm

PetieG
Member
Registered: 2012-08-23
Posts: 1

Re: Import files into the Notes.app in Mountain Lion

Ditto... worked great after removal of that line... It crashed on one note, i don't know why, but now I can get rid of Notational Velocity nvalt and stick w/ Apple Notes.  Somebody should turn this into an app w/ proper error messages and submit to macupdate.com ... Thank you all for your help!

Offline

 

#11 2013-02-25 11:09:17 am

tstrand
Member
Registered: 2005-08-10
Posts: 1

Re: Import files into the Notes.app in Mountain Lion

Thanks. The script does create Notes in ML. 

But there is a glitch when they sync to iOS Notes.

The first line syncs over and the rest is blank. 

However, if I make any edit in the notes record in OSX Notes, the full note text body appears in iOS.

Odd. Something about the way Notes creates note titles from text I think.

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.051 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)