I was asked to update the script according to changes in the operating system.
Here is a version tested under High Sierra.
I apologize but my iMac is too old to host Mojave so I can’t test under this OS.
(*
Export notes from Notes to PDF files
Yvan KOENIG (VALLAURIS, France)
2015/09/16
Modified on 2018-10-30 to take care of changes discovered in High Sierra
…
Modified again on 2018-11-05
Modified again on 2018-11-07
*)
my Germaine()
#=====
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
# Original code borrowed to Nigel Garvey (http://macscripter.net/viewtopic.php?id=41654)
tell application "System Events"
tell process "Notes"
set frontmost to true
set mt to 3 # Index of menu "File"
tell menu bar 1
-- get name of menu bar items --> {"Apple", "Notes", "Fichier", "Édition", "Format", "Présentation", "Fenêtre", "Aide"}
-- get name of menu bar item mt --> "Fichier"
tell menu bar item mt to tell menu 1
set menuNames to get name of menu items
# According to system in use, may be
(*Nouvelle note, Nouveau dossier, missing value, Fermer, Tout fermer, missing value, Partager, Exporter au format PDF…, missing value, Imprimer…*)
(*Nouvelle note, Nouveau dossier, missing value, Fermer, Tout fermer, missing value, Partager, missing value, Importer dans Notes…, Exporter au format PDF…, missing value, Épingler la note, Verrouiller la note, missing value, Imprimer…*)
(*New Note, New Folder, missing value, Close, Close All, missing value, Export as PDF…, Share, missing value, Import, Import from iPhone or iPad, missing value, Unpin Note, Lock Note, missing value, Print…*)
repeat with mi from 1 to count menuNames
if item mi of menuNames contains "PDF" then exit repeat
end repeat
# Here, mi is the index of the menu item "Export as PDF…"
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
(*
# old code
repeat until exists sheet 1 of window 1
delay 0.02
end repeat
*)
set cnt to 1
repeat
if exists sheet 1 of window 1 then exit repeat
set cnt to cnt + 1
if cnt = 20 then error "sheet unavailable"
end repeat
tell sheet 1 of window 1
set wichElements to class of UI elements
if wichElements contains combo box then
get position of combo boxes --> {{910, 118}, {910, 148}}
set value of combo box 1 to pdfName
else
--> {static text, text field, UI element, static text, text field, group, radio group, group, pop up button, text field, splitter group, button, button, button}
get position of text fields --> {{1337, 170}, {1337, 200}, {1533, 239}}
set value of text field 1 to pdfName
end if
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 wichElements to class of UI elements
# According to system in use, may be
--> {static text, text field, button, button}
--> {static text, combo box, button, button}
if wichElements contains combo box then
set SaveIndex to -1
set value of combo box 1 to posixFolderPath
else
set SaveIndex to 1
set value of text item 1 to posixFolderPath
end if
name of buttons --> {"Aller", "Annuler"}
click button 1
end tell
tell sheet 1 of window 1
set theButtons to get name of buttons
# According to system in use, may be
--> {"Enregistrer", "Nouveau dossier", "Annuler"}
--> {"Nouveau dossier", "Annuler", "Enregistrer"}
click button SaveIndex
end tell
end tell # process
end tell # "System Events"
delay 0.1 # Try to run with the value .1 and if it fails before exporting every notes, increment the delay to .2, .3 … to reach the correct value or use delay 1 as I did.
end exportAsPDF
#=====
on Germaine()
tell application "Notes"
activate
set nbNotes to count of notes
end tell
tell application (path to frontmost application as string)
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
# Triggers System Events for 1st time to decide if we are in the "modern" structure
tell application "System Events" to tell process "Notes"
set frontmost to true
set myWindow to window 1
tell myWindow
class of UI elements --> {splitter group, button, button, button, toolbar}
tell first splitter group
set whichElements to class of UI elements
--> {button, scroll area, splitter, splitter group} # at least since High Sierra
--> {scroll area, splitter, group, splitter, group} with # older systems
set newStructure to whichElements contains splitter group
end tell # 1st splitter group
end tell # window 1
end tell # System Events
if newStructure then
# at least since High Sierra
log "case with 2 levels of splitter group" # ADDED
tell application "System Events" to tell process "Notes"
set frontmost to true
tell myWindow
-- class of UI elements --> {splitter group, button, button, button, toolbar}
tell first splitter group
-- class of UI elements --> {button, scroll area, splitter, splitter group}
tell first splitter group
-- class of UI elements --> {button, scroll area, splitter, splitter group}
-- position of groups --> {{717, 135}, {959, 135}}
tell first group
-- class of UI elements --> {scroll area}
tell first scroll area
-- class of UI elements --> {table, scroll bar}
tell first table
-- class of UI elements --> {row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, column}
set nbRows to count rows
end tell # 1st table
end tell # 1st scroll area
end tell # 1st group
end tell # 1st splitter group
end tell # 1st splitter group
end tell # window 1
end tell # System Events
# No longer in System Events
# It seems that in some cases, speaking the application Notes may loose the target in the GUI
repeat with i from 1 to nbRows
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
delay 0.2
# Re-enter System Events hoping that we no longer will loose the target
tell application "System Events" to tell process "Notes"
set frontmost to true
tell myWindow
class of UI elements --> {splitter group, button, button, button, toolbar}
tell first splitter group
class of UI elements --> {button, scroll area, splitter, splitter group}
tell first splitter group
class of UI elements --> {group, splitter, group}
-- (get position of groups) --> {{717, 135}, {959, 135}}
tell first group
class of UI elements --> {scroll area}
tell first scroll area
class of UI elements --> {table, scroll bar}
tell first table
get class of UI elements --> {row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, column}
set value of attribute "AXSelected" of row i to true # select the note to export
end tell # 1st table
end tell # 1st scroll area
end tell # 1st group
end tell # 1st splitter group
end tell # 1st splitter group
end tell # window 1
end tell # System Events
set noteTitle to my buildTitle(noteName)
set theName to noteDate & " " & noteTitle & ".pdf"
my exportAsPDF(exportFolder as text, theName) # Export the note
end repeat
else
log "case without splitter group"
tell application "System Events" to tell process "Notes"
set frontmost to true
tell myWindow
class of UI elements --> {splitter group, button, button, button, toolbar}
tell first splitter group
-- class of UI elements --> {scroll area, splitter, group, splitter, group}
-- position of groups --> {{717, 135}, {959, 135}}
tell first group
-- class of UI elements --> {text field, scroll area}
-- value of first text field --> ""
tell first scroll area
-- class of UI elements --> {table, scroll bar}
set theTable to first table # Here we have a guaranteed reference to the table
tell theTable
-- class of UI elements --> {row, row, row, row, row, row, row, row, row, row, row, column}
set nbRows to count rows
end tell # 1st table
end tell # 1st scroll area
end tell # 1st group
end tell # 1st splitter group
end tell # myWindow
end tell # System Events
repeat with i from 1 to nbRows
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
delay 0.2
tell application "System Events" to tell process "Notes"
set frontmost to true
tell myWindow
class of UI elements --> {splitter group, button, button, button, toolbar}
tell first splitter group
-- class of UI elements --> {scroll area, splitter, group, splitter, group}
-- position of groups --> {{717, 135}, {959, 135}}
tell first group
-- class of UI elements --> {text field, scroll area}
-- value of first text field --> ""
tell first scroll area
-- class of UI elements --> {table, scroll bar}
set theTable to first table # Here we have a guaranteed reference to the table
tell theTable
-- class of UI elements --> {row, row, row, row, row, row, row, row, row, row, row, column}
set value of attribute "AXSelected" of row i to true # select the note to exportset nbRows to count rows
end tell # 1st table
end tell # 1st scroll area
end tell # 1st group
end tell # 1st splitter group
end tell # myWindow
end tell # System Events
set noteTitle to my buildTitle(noteName)
set theName to noteDate & " " & noteTitle & ".pdf"
my exportAsPDF(exportFolder as text, theName) # Export the note
end repeat
end if
end Germaine
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 février 2019 17:24:21