An user asked me for a script exporting more than 10,000 AppleWorks documents as PDF files.
I have a script doing the job but as it requires the application AppleWorks, it must run on an old machine which is really slow.
As LibreOffice is able to open the AppleWorks file, I'm working upon a code doing the job with this free app running on modern machines.
I have a working script but I'm not satisfied by a detail : when I must click in a checkbox I found no other tip than using the brave old cliclick.
I publish the script here hoping that some of you ( maybe Bill Cheeseman )will be able to find a neater scheme.
set theDoc to choose file of type {"public.rtf"}
set theApp to path to application id "org.libreoffice.script"
set theDoc to theDoc as text
tell application "Finder" to open file theDoc using theApp
-- for tests, define a fake PDF name and a fake destination folder
set nom_PDF to "kezako.pdf"
set posixFolderPath to POSIX path of (path to documents folder as text)
tell application "System Events"
-- The used scheme dislike overwriting an existing file
if exists disk item (posixFolderPath & nom_PDF) then delete disk item (posixFolderPath & nom_PDF)
--name of processes --> {"loginwindow", "SystemUIServer", …,"Script Editor", "soffice", …, "System Events"}
tell process "soffice"
-- Opening the doc may be long, we must wait a bit
repeat
try
set theWindow to name of window 1 --> "• au carré"
# If the doc was never opened with LibreOffice the app starts opening a window (whose name is "") containing thumbnails of opened once documents.
# We must wait until the document window appears !
if theWindow is not "" then exit repeat
on error
delay 0.2
end try
end repeat
end tell -- "soffice"
end tell -- "System Events"
-- For the first attemps, use select_Menu which give a lot of infos
-- my select_Menu("LibreOffice", 3, 23) -- Fichier > "Exporter au format PDF..."
-- But in real life, use selectMenu which is faster.
my selectMenu("LibreOffice", 3, 23) -- Fichier > "Exporter au format PDF..."
delay 0.2
tell application "System Events" to tell process "soffice"
set frontmost to true
set dialogName to name of window 1 --> "Options PDF"
tell window dialogName
set itsPosition to its position --> {564, 512}
set itsSize to its size --> {946, 484}
set theClasses to class of UI elements --> {button, button, button, tab, button, button, button, static text}
--name of buttons --> {missing value, missing value, missing value, "Aide", "Exporter", "Annuler"}
(*
-- If we are finicky we may use some code to get the index of the tab item
set theTab to 1
repeat with aClass in theClasses
log (get aClass)
if (aClass as text) is not "button" then exit repeat
set theTab to theTab + 1
end repeat
-- but I am able to count from 1 thru 4 ;-)
*)
--class of UI element theTab --> tab (not recognize class so must trigger the element by its index)
tell UI element 4
name of buttons --> {"Général", "Vue initiale", "Interface utilisateur", "Liens", "Sécurité", "Signatures numériques"}
tell button 1
--class of UI elements --> {group}
tell group 1 --(1)
--class of UI elements --> {group}
tell group 1 --(2)
--class of UI elements --> {group, group}
--position of groups --> {{143, 286}, {605, 286}}
tell group 2 --(3)
--class of UI elements --> {static text, group}
tell group 1 --(4)
--class of UI elements --> {group}
tell group 1 --(5)
--class of UI elements --> {checkbox, checkbox, checkbox, checkbox, group, checkbox, checkbox, checkbox, checkbox, checkbox}
--title of checkboxes
--> {"PDF hybride (fichier ODF incorporé)", "Archive PDF/A-1a (ISO 19005-1)", "PDF marqué (ajouter la structure du document)", "Créer un formulaire PDF", "Exporter les repères de texte", "Exporter les substituants", "Exporter les commentaires", "Exporter les pages vides insérées automatiquement", "Afficher le PDF après export"}
tell checkbox 2
--its title --> "Archive PDF/A-1a (ISO 19005-1)"
if (get its value) = 0 then
set {xLeft, yTop} to its position
set {theWidth, theHeight} to its size
set xTarget to xLeft + theHeight div 2
set yTarget to yTop + theHeight div 2
#•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
tell me to do shell script "/Users/admin/bin/cliclick c:" & xTarget & "," & yTarget
#•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
delay 0.1
--its value --> 1
end if
end tell -- checkbox 2
end tell -- group 1 (5)
end tell -- group 1 (4)
end tell -- group 2 (3)
end tell -- group 1 (2)
end tell -- group 1 (1)
end tell -- button 1
end tell -- UI element 4
keystroke return
end tell -- window dialogName
delay 0.2
set ExportWindow to name of window 1 --> "Exporter"
-- We are in the window allowing to define the PDF name and the destination folder
tell window ExportWindow
set value of text field 1 to nom_PDF -- passe le nom du PDF
# Open the « sheet » allowing to define the POSIX path of destination folder
keystroke "g" using {command down, shift down}
repeat until exists sheet 1
delay 0.02
end repeat
tell sheet 1
set value of text field 1 to posixFolderPath
--log (get name of buttons) (*Aller, Annuler*)
keystroke return # ditto click button 1 (or Aller)
delay 0.01 # IRequired when we use keystroke return and not click button 1
end tell
--log (get name of buttons) (*Enregistrer, Nouveau dossier, Annuler, missing value, missing value, missing value*)
keystroke return --> ditto click button 1 (ou "Engegistrer")
end tell
-- The document window is frontmost
tell window theWindow
--role description of buttons --> {"Bouton de fermeture", "bouton plein écran", "Bouton de placement dans le Dock"}
click button 1 -- Ferme la fenêtre
end tell
end tell -- "System Events" …
#=====
(*
my selectMenu("Pages",5, 12)
==== Uses GUIscripting ====
*)
on selectMenu(theApp, mt, mi)
activate application theApp
tell application "System Events" to tell process theApp
set frontmost to true
tell menu bar 1 to tell menu bar item mt to tell menu 1 to click menu item mi
end tell
end selectMenu
#=====
(*
useful to get the indexs of the triggered item
my select_Menu("LibreOffice", 3, 23) (* Fichier > Exporter au format PDF… *)
*)
on select_Menu(theApp, mt, mi)
activate application theApp
tell application id "com.apple.systemevents" to tell process theApp to tell menu bar 1
get name of menu bar items (*{
01 - "Apple",
02 - "LibreOffice",
03 - "Fichier",
04 - "Édition",
05 - "Affichage",
06 - "Insertion",
07 - "Format",
08 - "Styles",
09 - "Tableau",
10 - "Outils",
11 - "Dmaths",
12 - "Fenêtre",
13 - "Aide"}
*)
get name of menu bar item mt
-- {"Fichier"}
tell menu bar item mt to tell menu 1
get name of menu items (* {
01 - "Nouveau",
02 - "Ouvrir...",
03 - "Ouvrir un fichier distant...",
04 - "Derniers documents utilisés",
05 - "Fermer",
06 - missing value,
07 - "Assistants",
08 - "Modèles",
09 - missing value,
10 - "Recharger",
11 - "Versions...",
12 - missing value,
13 - "Enregistrer",
14 - "Enregistrer sur le serveur distant",
15 - "Enregistrer sous...",
16 - "Enregistrer une copie...",
17 - "Tout enregistrer",
18 - "Extraire",
19 - "Annuler l'extraction...",
20 - "Déposer...",
21 - missing value,
22 - "Exporter...",
23 - "Exporter au format PDF...",
24 - "Envoyer",
25 - "Aperçu dans le navigateur Web",
26 - missing value,
27 - "Aperçu",
28 - "Imprimer...",
29 - "Paramétrages de l'imprimante...",
30 - missing value,
31 - "Propriétés...",
32 - "Signatures numériques..."}
*)
get name of menu item mi --> {"Exporter au format PDF..."}
click menu item mi
end tell -- menu bar…
end tell -- System Events
end select_Menu
#=====