Maybe some of you would be interested by this script.
I know, it’s not particularly optimized, it just work.
I’m too lazy to write the code needed to guarantee that Safari display the very first chapter when the script begin to work.
If you want to edit it, take care that it reach one limit of GUI Scripting: we can’t add a complementary nested tell in its blocks of code.
This is why it’s organized an ugly way and why I moved the calls to cliclick in handlers.
When I tried to include them the clean way by:
tell me do do shell script, the compiler refused to do the job.
#
# Open the URL:
# https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html#//apple_ref/doc/uid/TP40000983-CH208-SW1
# By hand, click [Table of Contents]
# Then click [Introduction]
# The online AppleScript User Guide will display its first chapter.
# So you may execute this script to create, in your Documents folder, a PDF file for every chapter.
#
# Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 26 mai 2020 22:38:22
# 2020/05/27 Changed the fileName, it's now "AppleScript Language Guide chapter #"…
#
# I apologizes, I can't test under more recent systems.
#
# Download the free CLI cliclick from <https://www.bluem.net/en/projects/cliclick/>
# and install it as : "/usr/local/bin/cliclick"
# If you use an other location, edit accordingly the two handlers at the very end of the script.
#
# As I always does, Il left many disabled instructions used during the coding process.
set saveFolder to (path to documents folder) as text
tell application "Safari" to activate
-- Scan the web page to determine the count of chapters
tell application "System Events" to tell process "Safari"
set frontmost to true
tell window 1
-- class of UI elements --> {splitter group, button, button, button, group, button, group, toolbar}
tell splitter group 1
-- class of UI elements ---> {splitter, tab}
tell tab group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {scroll area}
tell scroll area 1
-- class of UI elements --> {UI element, scroll bar}
tell UI element 1 -- WebArea
-- class of UI elements --> {group, group, group, group, group, group}
tell group 4
-- get its position --> {574, 312}
-- get its size --> {230, 804}
-- class of UI elements --> {group}
tell group 1
-- get its position --> {574, 312}
-- get its size --> {230, 804}
-- class of UI elements --> {group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group}
set cntChapters to count groups
end tell -- group 1
end tell -- group 4
end tell -- UI element 1 -- WebArea
end tell -- scroll area 1
end tell -- group 1
end tell -- group 1
end tell -- tab group 1
end tell -- splitter group 1
end tell -- window 1
end tell -- System Events
repeat with r from 1 to cntChapters
log ">>>>>>>> group r : " & r
tell application "System Events" to tell process "Safari"
set frontmost to true
tell window 1
-- get its properties --> {minimum value:missing value, orientation:missing value, position:{581, 138}, class:window, accessibility description:missing value, role description:"fenêtre standard", focused:false, title:"Introduction to AppleScript Language Guide", size:{1339, 913}, help:missing value, entire contents:{}, enabled:missing value, maximum value:missing value, role:"AXWindow", value:missing value, subrole:"AXStandardWindow", selected:missing value, name:"Introduction to AppleScript Language Guide", description:"fenêtre standard"}
-- get class of UI elements --> {splitter group, button, button, button, group, button, group, toolbar}
tell splitter group 1
-- class of UI elements --> {splitter, tab}
tell tab group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {scroll area}
tell scroll area 1
-- class of UI elements --> {UI element, scroll bar}
tell UI element 1 -- AXWebArea
-- class of UI elements --> {group, group, group, group, group, group}
set knt to count groups
if knt = 6 then
log "case #1, knt = 6"
tell group 4
-- get its position --> {574, 312}
-- get its size --> {230, 804}
-- class of UI elements --> {group}
tell group 1
-- get its position --> {574, 312}
-- get its size --> {230, 804}
-- class of UI elements --> {group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group}
tell group r
set {x, y} to get its position --> {574, 344}
set {w, h} to get its size --> {209, 33}
-- class of UI elements --> {UI element, group, UI element}
-- tell UI element 1 to get class of UI elements--> {}
-- tell group 1 to get class of UI elements--> {}
tell UI element 3
-- class of UI elements --> {static text}
value of static text 1 --> "AppleScript Lexical Conventions"
end tell
my cliClick2((x + w div 2), (y + 2))
end tell -- group r
end tell -- group 1
end tell -- group 4
set ready to true
else -- count group ≠ 6
log "case #2, knt = " & knt -- may be 3 or 5
tell group 1 to get class of UI elements --> {}
tell group 2
-- set {x, y} to get its position --> {574, 344}
-- set {w, h} to get its size --> {209, 33}
class of UI elements --> {UI element, UI element, button}
(*
tell UI element 1
-- get class of UI elements --> {static text}
-- value of static text 1 -> "Documentation Archive"
end tell -- UI element 1
tell UI element 2
-- class of UI elements --> {static text}
-- value of static text 1 --> "Developer"
end tell -- UI element 2
try
tell button 1 to get its name
end try
*)
end tell -- group 2
tell group 3
-- set {x, y} to get its position --> {574, 377}
-- set {w, h} to get its size --> {209, 20}
-- class of UI elements --> --> {group, toolbar}
(*
tell group 1
-- class of UI elements --> {UI element, group}
tell UI element 1
-- class of UI elements --> {static text}
end tell
tell group 1
-- class of UI elements --> {}
end tell
end tell-- group 1
*)
tell toolbar 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {checkbox}
tell checkbox 1
-- get its description --> "Show Table of Contents"
set {xc, yc} to get its position
set {wc, hc} to get its size
set switch to its value
log "value of checkbox : " & switch
my cliClick((xc + wc div 4), (yc + hc div 2))
end tell
end tell -- group 1
end tell -- toolbar 1
end tell -- group 3
set ready to false
end if
end tell -- UI element 1 -- AXWebArea
end tell -- scroll area 1
end tell -- group 1
end tell -- group 1
end tell -- tab group 1
end tell -- splitter group 1
end tell -- window 1
end tell -- System Events
if not ready then
tell application "System Events" to tell process "Safari"
set frontmost to true
tell window 1
-- get its properties --> {minimum value:missing value, orientation:missing value, position:{581, 138}, class:window, accessibility description:missing value, role description:"fenêtre standard", focused:false, title:"Introduction to AppleScript Language Guide", size:{1339, 913}, help:missing value, entire contents:{}, enabled:missing value, maximum value:missing value, role:"AXWindow", value:missing value, subrole:"AXStandardWindow", selected:missing value, name:"Introduction to AppleScript Language Guide", description:"fenêtre standard"}
-- get class of UI elements --> {splitter group, button, button, button, group, button, group, toolbar}
tell splitter group 1
-- class of UI elements --> {splitter, tab}
tell tab group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {scroll area}
tell scroll area 1
-- class of UI elements --> {UI element, scroll bar}
tell UI element 1 -- AXWebArea
-- class of UI elements --> {group, group, group, group, group, group}
set knt to count groups
log "case #3, knt = " & knt -- may be 5 or 6
if knt = 6 then
tell group 4
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group}
tell group r
set {x, y} to get its position --> {574, 344}
set {w, h} to get its size --> {209, 33}
-- class of UI elements --> {UI element, group, UI element}
-- tell UI element 1 to get class of UI elements--> {}
-- tell group 1 to get class of UI elements--> {}
tell UI element 3
-- class of UI elements --> {static text}
value of static text 1 --> "AppleScript Lexical Conventions"
end tell
my cliClick2((x + w div 2), (y + 2))
end tell -- group r
end tell -- group 1
end tell -- group 4
else
tell group 1 to get class of UI elements --> {}
tell group 2
-- set {x, y} to get its position --> {574, 344}
-- set {w, h} to get its size --> {209, 33}
class of UI elements --> {UI element, UI element, button}
(*
tell UI element 1
-- get class of UI elements --> {static text}
-- value of static text 1 -> "Documentation Archive"
end tell -- UI element 1
tell UI element 2
-- class of UI elements --> {static text}
-- value of static text 1 --> "Developer"
end tell -- UI element 2
try
tell button 1 to get its name
end try
*)
end tell -- group 2
tell group 3
-- set {x, y} to get its position --> {574, 377}
-- set {w, h} to get its size --> {209, 20}
-- class of UI elements --> --> {group, toolbar}
(*
tell group 1
-- class of UI elements --> {UI element, group}
tell UI element 1
-- class of UI elements --> {static text}
end tell -- UI element 1
tell group 1
-- class of UI elements --> {}
end tell -- group 1
end tell-- group 1
*)
tell toolbar 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {checkbox}
tell checkbox 1
-- get its description --> "Show Table of Contents"
set {xc, yc} to get its position
set {wc, hc} to get its size
set switch to its value
log "value of checkbox : " & switch
my cliClick((xc + wc div 4), (yc + hc div 2))
end tell
end tell -- group 1
end tell -- toolbar 1
end tell -- group 3
set ready to false
end if
end tell -- UI element 1 -- AXWebArea
end tell -- scroll area 1
end tell -- group 1
end tell -- group 1
end tell -- tab group 1
end tell -- splitter group 1
end tell -- window 1
end tell -- System Events
end if
-- Now, print the chapter in a PDF
my saveInPDF(saveFolder, r)
-- Now reveal the TOC
tell application "System Events" to tell process "Safari"
set frontmost to true
tell window 1
--get its properties --> {minimum value:missing value, orientation:missing value, position:{581, 138}, class:window, accessibility description:missing value, role description:"fenêtre standard", focused:false, title:"Introduction to AppleScript Language Guide", size:{1339, 913}, help:missing value, entire contents:{}, enabled:missing value, maximum value:missing value, role:"AXWindow", value:missing value, subrole:"AXStandardWindow", selected:missing valued name:"Introduction to AppleScript Language Guide", description:"fenêtre standard"}
get class of UI elements --> {splitter group, button, button, button, group, button, group, toolbar}
tell splitter group 1
-- class of UI elements --> {splitter, tab}
tell tab group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {scroll area}
tell scroll area 1
-- class of UI elements --> {UI element, scroll bar}
tell UI element 1 -- AXWebArea
-- class of UI elements
--> {group, group, group, group, group, group}
--> {group, group, group}
try
tell group 3
-- class of UI elements --> {group, toolbar}
tell toolbar 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {checkbox}
tell checkbox 1
-- get its description --> "Show Table of Contents"
set {xc, yc} to get its position
set {wc, hc} to get its size
set switch to its value
log "value of checkbox : " & switch
my cliClick((xc + wc div 4), (yc + hc div 2))
end tell -- checkbox 1
end tell -- group 1
end tell -- toolbar 1
end tell -- group 3
end try
end tell -- UI element 1 -- AXWebArea
end tell -- scroll area 1
end tell -- group 1
end tell -- group 1
end tell -- tab group 1
end tell -- splitter group 1
end tell -- window 1
end tell -- System Events
delay 0.2
end repeat
#=====
on saveInPDF(saveFolder, r)
set pdfName to "AppleScript Language Guide chapter #" & r & ".pdf" -- Changed the name
set pdfPath to saveFolder & pdfName
tell application "System Events"
if exists disk item pdfPath then delete disk item pdfPath
end tell
tell application "System Events" to tell process "Safari"
-- get class of UI elements --> {window, menu bar}
keystroke "p" using {command down} -- activate print menu item
-- no need for a delay here
-- class of UI elements --> {window, menu bar}
tell window 1
repeat 10 times
if exists sheet 1 then exit repeat -- it seems that this delay isn't needed
delay 1
end repeat
-- class of UI elements --> {splitter group, group, button, toolbar, button, button, button, sheet}
tell sheet 1
-- class of UI elements
--> {group, static text, group, static text, pop up button, static text, pop up button, static text, text field, checkbox, static text, radio button, radio button, text field, static text, text field, static text, pop up button, static text, static text, radio group, static text, text field, pop up button, checkbox, checkbox, button, menu button, button, button, button} under High Sierra
--> {group, static text, group, static text, pop up button, static text, pop up button, static text, text field, static text, radio button, radio button, text field, static text, text field, static text, pop up button, static text, static text, radio group, static text, text field, pop up button, checkbox, checkbox, button, menu button, button, image, static text, button, button, button, sheet} under Catalina
-- description of pop up buttons --> {"Imprimantes", "Préréglages", "Taille du papier", "PDE"}
-- title of menu buttons --> {"PDF"}
tell menu button "PDF"
get its value --> missing value
click it
-- class of UI elements --> {menu}
repeat until exists menu 1
delay 0.02
end repeat
-- get its value --> missing value
-- name of menu items of menu 1
--> {"Ouvrir dans Aperçu", "Enregistrer au format PDF", "Enregistrer au format PostScript", missing value, "@ PDF-BAT.qfilter", "@ PDF-prépresse CMJN.qfilter", "@ PDF-web.qfilter", "@ PDFX3-ISO.qfilter", "Ajouter à iBooks", "Save as Adobe PDF", "Enregistrer en PDF-X", "Save PDF to Aperture", "Save PDF to folder as JPEG", "Save PDF to folder as TIFF", "Enregistrer le document PDF vers iPhoto", "Enregistrer sur iCloud Drive", "Enregistrer avec les reçus web", "Envoyer via Mail", "Envoyer via Messages", "Copie de AbracadabraPDF-web.qfilter", missing value, "Modifier le menu…"} under High Sierra
--> {"Ouvrir dans Aperçu", "Enregistrer au format PDF", "Enregistrer au format PostScript", missing value, "Envoyer via Mail", "Envoyer via Messages", "Enregistrer sur iCloud Drive", "Enregistrer avec les reçus web", missing value, "Modifier le menu…"} under Catalina
click menu item 2 of menu 1 --> "Enregistrer au format PDF / "Save as PDF"
end tell -- menu button "PDF"
-- class of UI elements
--> {group, static text, group, static text, pop up button, static text, pop up button, static text, text field, checkbox, static text, radio button, radio button, text field, static text, text field, static text, pop up button, static text, static text, radio group, static text, text field, pop up button, checkbox, checkbox, button, menu button, button, button, button, sheet}
keystroke "g" using {shift down, command down} -- to select the folder location
-- class of UI elements --> {group, static text, group, static text, pop up button, static text, pop up button, static text, text field, checkbox, static text, radio button, radio button, text field, static text, text field, static text, pop up button, static text, static text, radio group, static text, text field, pop up button, checkbox, checkbox, button, menu button, button, button, button, sheet}
repeat until exists sheet 1
delay 0.02
end repeat
-- class of UI elements --> après :{group, static text, group, static text, pop up button, static text, pop up button, static text, text field, checkbox, static text, radio button, radio button, text field, static text, text field, static text, pop up button, static text, static text, radio group, static text, text field, pop up button, checkbox, checkbox, button, menu button, button, button, button, sheet}
tell sheet 1
-- class of UI elements --> {static text, text field, UI element, static text, text field, static text, pop up button, text field, static text, button, text field, static text, text field, static text, static text, text field, button, button, sheet}
repeat until exists sheet 1
delay 0.02
end repeat
-- class of UI elements
--> {static text, text field, UI element, static text, text field, static text, pop up button, text field, static text, button, text field, static text, text field, static text, static text, text field, button, button, sheet}
tell sheet 1
set theClasses to class of UI elements --> {static text, combo box, button, button} Recent systems
-- name of buttons --> {"Aller", "Annuler"}
if theClasses contains combo box then
set target to combo box 1
else
set target to text field 1
end if
set value of target to saveFolder
click button 1 --> "Aller"
end tell -- sheet 1
repeat while exists sheet 1
delay 0.02
end repeat
-- class of UI elements --> {static text, text field, UI element, static text, text field, group, radio group, group, pop up button, text field, splitter group, text field, static text, button, text field, static text, text field, static text, static text, text field, button, button, button}
-- name of text fields --> {"Enregistrer sous :", "Tags :", missing value, missing value, missing value, missing value, missing value}
set value of text field 1 to pdfName
-- name of buttons
--> {"Options de sécurité…", "Nouveau dossier", "Annuler", "Enregistrer"} under High Sierra
--> { "Options de sécurité…", "Annuler", "Enregistrer"} under Mojave
--> {missing value, "Options de sécurité…", "Nouveau dossier", "Annuler", "Enregistrer"} under Catalina
-- name of button -1 --> "Enregistrer"
click button -1
end tell -- sheet 1
end tell -- sheet 1
end tell -- window 1
end tell -- System Events
end saveInPDF
on cliClick(xx, yy)
do shell script "/usr/local/bin/cliclick c:" & xx & "," & yy
end cliClick
on cliClick2(xx, yy)
do shell script "/usr/local/bin/cliclick dc:" & xx & "," & yy
end cliClick2
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 26 mai 2020 22:57:59