Saturday, September 23, 2017

#1 2015-09-13 08:47:14 am

johncatalano
Member
Registered: 2014-03-07
Posts: 21

Notes.app export (to PDF); append "creation date"

I've found an Applescript that exports notes in Notes.app to html files: https://github.com/pmatiello/notes-expo … pplescript.

What I really want is, instead of appending numbers to the start of the filenames, is to append the note "creation date" to the start of the filename in the format YYYY-MM-DD HHMMSS.

I've replaced the counter with a noteDate variable but then the script breaks. My questions:

1. How can I export notes from the Notes.app and append the create date to the start?
2. Is there a way to export, instead of HTML, to PDF?

Here's the script with my edits:

Applescript:

on buildTitle(originalText)
   set normalizedText to my replace(originalText, ":", "-")
   set finalTitle to my firstChars(normalizedText, 100)
   return finalTitle
end buildTitle

on replace(originalText, fromText, toText)
   set AppleScript's text item delimiters to the fromText
   set the item_list to every text item of originalText
   set AppleScript's text item delimiters to the toText
   set originalText to the item_list as string
   set AppleScript's text item delimiters to ""
   return originalText
end replace

on firstChars(originalText, maxChars)
   if length of originalText is less than maxChars then
       return originalText
   else
       set limitedText to text 1 thru maxChars of originalText
       return limitedText
   end if
end firstChars

on writeToFile(filename, filecontents)
   set the output to open for access file filename with write permission
   set eof of the output to 0
   write ((ASCII character 239) & (ASCII character 187) & (ASCII character 191)) to output
   write filecontents to the output starting at eof as «class utf8»
   close access the output
end writeToFile


tell application "Notes"
   activate
   display dialog "This is the export utility for Notes.app.
"
& "Exactly " & (count of notes) & " notes are stored in the application. " & "Each one of them will be exported as a simple HTML file stored in a folder of your choice." with title "Notes Export" buttons {"Cancel", "Proceed"} cancel button "Cancel" default button "Proceed"
   set exportFolder to choose folder
   set counter to 0

repeat with each in every note
   set noteName to name of each
   set noteBody to body of each
   set noteDate to creation date of each -- replaced counter with noteDate
   set noteTitle to my buildTitle(noteName)
   set filename to ((exportFolder as string) & noteDate & " " & noteTitle & ".html") -- replaced counter with noteDate
   my writeToFile(filename, noteBody as text)
end repeat

   display alert "Notes Export" message "All notes were exported successfully." as informational
end tell


Filed under: PDF, Export, date, creation

Offline

 

#2 2015-09-13 10:59:36 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3194

Re: Notes.app export (to PDF); append "creation date"

(1) an easy way is to print the note in a pdf

(2) you wrote that the number/ creation date is at the beginning of the file name but in your code, you insert it at the end of the name, just before the name extension. I can't guess what is the correct behavior.

(3) when I want to dateTime stamp a file I convert the date with this kind of code :

Applescript:

set uneDate to (current date) - 10 * days
set stamp to my horodateur(uneDate)

#=====

on horodateur(une_date)
   tell une_date to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end horodateur

#=====

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) dimanche 13 septembre 2015 17:58:45

Offline

 

#3 2015-09-14 08:52:47 am

johncatalano
Member
Registered: 2014-03-07
Posts: 21

Re: Notes.app export (to PDF); append "creation date"

Yvan Koenig wrote:

(1) an easy way is to print the note in a pdf

(2) you wrote that the number/ creation date is at the beginning of the file name but in your code, you insert it at the end of the name, just before the name extension. I can't guess what is the correct behavior.

(3) when I want to dateTime stamp a file I convert the date with this kind of code :

Applescript:

set uneDate to (current date) - 10 * days
set stamp to my horodateur(uneDate)

#=====

on horodateur(une_date)
   tell une_date to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end horodateur

#=====


Thanks for your comments, but we're not in-sync with our ideas. The script I posted throws a very specific error. Can you help me identify the cause of that error?

Thanks,

John

Offline

 

#4 2015-09-14 09:22:00 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3194

Re: Notes.app export (to PDF); append "creation date"

I just tried to answer to two of your questions :


1. How can I export notes from the Notes.app and append the create date to the start?
2. Is there a way to export, instead of HTML, to PDF?


I have no use for Notes so I never opened it and have more interesting things to do that learning how it works.


Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) lundi 14 septembre 2015 16:21:07

Offline

 

#5 2015-09-14 09:29:18 am

johncatalano
Member
Registered: 2014-03-07
Posts: 21

Re: Notes.app export (to PDF); append "creation date"

Yvan Koenig wrote:

I just tried to answer to two of your questions :

1. How can I export notes from the Notes.app and append the create date to the start?
2. Is there a way to export, instead of HTML, to PDF?


I have no use for Notes so I never opened it and have more interesting things to do that learning how it works.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) lundi 14 septembre 2015 16:21:07


I apologize for not making my questions clear enough. Your answers aren't what I am looking for. Thank you.

Anyone else that can help?

Offline

 

#6 2015-09-14 10:16:30 am

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 765

Re: Notes.app export (to PDF); append "creation date"

Any question specific to an error message was actually unasked in your original post, but the error is relative to your use of the date format; because seconds include colon separators, there is never a writeable file created by open for access. Yvan's supplied code neatly put the date into a serviceable format, although it lacks dashes, which may or may not be important.

Offline

 

#7 2015-09-14 10:23:22 am

johncatalano
Member
Registered: 2014-03-07
Posts: 21

Re: Notes.app export (to PDF); append "creation date"

Marc Anthony wrote:

Any question specific to an error message was actually unasked in your original post, but the error is relative to your use of the date format; because seconds include colon separators, there is never a writeable file created by open for access. Yvan's supplied code neatly put the date into a serviceable format, although it lacks dashes, which may or may not be important.


Hi Marc - Thanks for the explanation. I didn't understand what Yvan was trying to tell me but I'm starting to.

@Yvan - thanks for your help, despite me not recognizing it.

Will keep poking at the script and see what I can make happen.

Offline

 

#8 2015-09-14 10:58:23 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3194

Re: Notes.app export (to PDF); append "creation date"

Your script fails because you build a pathname embedding a date-time value which contain colons.

I bet that this edited version will behave correctly.

Applescript:

on buildTitle(originalText)
   set normalizedText to my replace(originalText, ":", "-")
   set finalTitle to my firstChars(normalizedText, 100)
   return finalTitle
end buildTitle

on replace(originalText, fromText, toText)
   set AppleScript's text item delimiters to the fromText
   set the item_list to every text item of originalText
   set AppleScript's text item delimiters to the toText
   set originalText to the item_list as string
   set AppleScript's text item delimiters to ""
   return originalText
end replace

on firstChars(originalText, maxChars)
   if length of originalText is less than maxChars then
       return originalText
   else
       set limitedText to text 1 thru maxChars of originalText
       return limitedText
   end if
end firstChars

on writeToFile(filename, filecontents)
   set the output to open for access file filename with write permission
   set eof of the output to 0
   write ((ASCII character 239) & (ASCII character 187) & (ASCII character 191)) to output
   write filecontents to the output starting at eof as «class utf8»
   close access the output
end writeToFile


tell application "Notes"
   activate
   display dialog "This is the export utility for Notes.app.
"
& "Exactly " & (count of notes) & " notes are stored in the application. " & "Each one of them will be exported as a simple HTML file stored in a folder of your choice." with title "Notes Export" buttons {"Cancel", "Proceed"} cancel button "Cancel" default button "Proceed"
   set exportFolder to choose folder
   set counter to 0
   
   repeat with each in every note
       set noteName to name of each
       set noteBody to body of each
       set noteDate to my horodateur(creation date of each) -- replaced counter with noteDate # EDITED
       -- set noteDate to my horodateurAlt(creation date of each) -- replaced counter with noteDate # EDITED
       set noteTitle to my buildTitle(noteName)
       
       set filename to ((exportFolder as string) & noteDate & " " & noteTitle & ".html") -- replaced counter with noteDate
       my writeToFile(filename, noteBody as text)
   end repeat
   
   display alert "Notes Export" message "All notes were exported successfully." as informational
end tell

on horodateur(une_date) # builds yyyymmdd_hhmmss
   tell une_date to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end horodateur

on horodateurAlt(une_date) # builds yyyy-mm-dd_hhmmss
   tell une_date to return ((text -4 thru -1 of ((its year) + 10000 as text)) & "-" & (text -2 thru -1 of ((its month) + 100 as text)) & "-" & (text -2 thru -1 of ((its day) + 100 as text))) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end horodateurAlt

As you will see, using my handler was the required change !


Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) lundi 14 septembre 2015 17:58:15

Offline

 

#9 2015-09-15 04:01:25 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3194

Re: Notes.app export (to PDF); append "creation date"

Thanks a lot for your "missing" feedback.
Trying to help in such conditions was a pleasure roll


Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 15 septembre 2015 11:01:15

Offline

 

#10 2015-09-15 08:32:20 pm

johncatalano
Member
Registered: 2014-03-07
Posts: 21

Re: Notes.app export (to PDF); append "creation date"

That did the trick as far as dates go, yes. "horodateurAlt" was just what Iw as looking for.

Thanks again.

Offline

 

#11 2015-09-16 02:39:41 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3194

Re: Notes.app export (to PDF); append "creation date"

Hello

Here is a new version which export the notes in PDFs files.

I apologize but at this time I have no way to do that without GUIScripting.

Applescript:

(*
Export notes from Notes to PDF files

Yvan KOENIG (VALLAURIS, France)
2015/09/16
*)


#=====

on buildTitle(originalText)
   set normalizedText to my replace(originalText, ":", "-")
   return my firstChars(normalizedText, 100)
end buildTitle

#=====

on replace(originalText, fromText, toText)
   set oTIDs to AppleScript's text item delimiters
   set AppleScript's text item delimiters to the fromText
   set the item_list to every text item of originalText
   set AppleScript's text item delimiters to the toText
   set originalText to the item_list as string
   set AppleScript's text item delimiters to oTIDs
   return originalText
end replace

on firstChars(originalText, maxChars)
   if length of originalText < maxChars then
       return originalText
   else
       return text 1 thru maxChars of originalText
   end if
end firstChars

(*
on horodateur(une_date) # builds yyyymmdd_hhmmss
   tell une_date to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end horodateur
*)

on horodateurAlt(une_date) # builds yyyy-mm-dd_hhmmss
   tell une_date to return ((text -4 thru -1 of ((its year) + 10000 as text)) & "-" & (text -2 thru -1 of ((its month) + 100 as text)) & "-" & (text -2 thru -1 of ((its day) + 100 as text))) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end horodateurAlt


on exportAsPDF(FolderPath, pdfName)
   
   set posixFolderPath to POSIX path of FolderPath
   set pdfPath to POSIX path of (FolderPath & pdfName)
   
   tell application "System Events"
       if exists disk item pdfPath then
           delete disk item pdfPath
           delay 0.2
       end if
   end tell
   # Code borrowed to Nigel Garvey ([url]http://macscripter.net/viewtopic.php?id=41654[/url])
   tell application "System Events"
       tell process "Notes"
           set frontmost to true
           set {mt, mi} to {3, 8}
           
           tell menu bar 1
               log (get name of menu bar items) (*Apple, Notes, Fichier, Édition, Format, Présentation, Fenêtre, Aide*)
               --log (get name of menu bar item mt) (*Fichier*)
               tell menu bar item mt to tell menu 1
                   log (get name of menu items) (*Nouvelle note, Nouveau dossier, missing value, Fermer, Tout fermer, missing value, Partager, Exporter au format PDF…, missing value, Imprimer…*)
                   log (get name of menu item mi) (*Exporter au format PDF…*)
                   click menu item mi
               end tell # menu bar item mt
           end tell # menu bar 1
           
           repeat until exists sheet 1 of window 1
               delay 0.02
           end repeat
           tell sheet 1 of window 1
               -- log (get position of text fields) (*910, 118, 910, 148*)
               set value of text field 1 to pdfName
           end tell
           
           keystroke "g" using {command down, shift down}
           repeat until exists sheet 1 of sheet 1 of window 1
               delay 0.02
           end repeat
           tell sheet 1 of sheet 1 of window 1
               set value of text field 1 to posixFolderPath
               -- log (get name of buttons) (*Aller, Annuler*)
               click button 1
           end tell
           tell sheet 1 of window 1
               -- log (get name of buttons) (*Enregistrer, Nouveau dossier, Annuler*)
               click button 1
           end tell
           
       end tell # process
       
   end tell # "System Events"
   
end exportAsPDF

#=====

tell application "Notes"
   activate
   set nbNotes to count of notes
end tell
tell application "SystemUIServer"
   display dialog "This is the export utility for Notes.app.
"
& "Exactly " & nbNotes & " notes are stored in the application. " & "Each one of them will be exported as a simple PDF file stored in a folder of your choice." with title "Notes Export" buttons {"Cancel", "Proceed"} cancel button 1 default button 2
   set exportFolder to choose folder
end tell

tell application "System Events" to tell process "Notes"
   set frontmost to true
   tell window 1
       -- log (get class of UI elements) (*splitter group, button, button, button, static text*)
       tell first splitter group
           -- log (get class of UI elements) (*scroll area, splitter, group, splitter, group*)
           -- log (get position of groups) (*717, 135, 959, 135*)
           tell first group
               -- log (get class of UI elements) (*text field, scroll area*)
               -- log (get value of first text field) (**)
               tell first scroll area
                   -- log (get class of UI elements) (*table, scroll bar*)
                   tell first table
                       -- log (get class of UI elements) (*row, row, row, row, row, row, row, row, row, row, row, column*)
                       repeat with i from 1 to count row
                           tell application "Notes" to tell note i # Grab parameters to build the PDF name
                               set noteName to name
                               set noteDate to my horodateurAlt(creation date)
                           end tell
                           set value of attribute "AXSelected" of row i to true # select the note to export
                           set noteTitle to my buildTitle(noteName)
                           set theName to noteDate & " " & noteTitle & ".pdf"
                           
                           my exportAsPDF(exportFolder as text, theName) # Export the note
                           
                       end repeat
                   end tell # 1st table
               end tell # 1st scroll area
           end tell # 1st group
       end tell # 1st splitter group
   end tell # window 1
end tell # System Events…

As you may see, I left numerous instruction which helped me to identify the UI Elements to trigger.
I left also what they returned on my system running in French.
I just commented them so they don't eat processor cycles.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mercredi 16 septembre 2015 21:39:21

Last edited by Yvan Koenig (2015-09-19 12:08:05 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)