Here is an other way to do the job.
set aBundle to ((path to library folder from system domain as text) & "PrivateFrameworks:Safari.framework:") as «class furl»
set general_loc to localized string "General" in bundle aBundle
activate application "Safari"
tell application "System Events"
tell application process "Safari"
set frontmost to true
keystroke "," using {command down}
repeat 20 times
try
if name of buttons of toolbar 1 of window 1 contains general_loc then exit repeat
on error
-- here if no window is open
end try
delay 0.1
end repeat
tell window 1
if its name is not general_loc then -- It's the General window, no need to switch.
-- class of UI elements --> {group, button, button, button, toolbar, static text}
tell toolbar 1
-- class of UI elements --> {button, button, button, button, button, button, button, button, button, button}
-- name of buttons --> {"Général", "Onglets", "Remplissage automatique", "Mots de passe", "Recherche", "Sécurité", "Confidentialité", "Sites web", "Extensions", "Avancées"}
click button general_loc -- select pane "General"
end tell
repeat 50 times
if its name is general_loc then exit repeat
delay 0.1
end repeat
end if
tell group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {button, pop up button, static text, text field, static text, button, static text, checkbox, pop up button, static text, pop up button, static text, static text, static text, pop up button, pop up button, pop up button, static text, static text, static text, pop up button, pop up button}
-- name of pop up buttons--> {"Au démarrage, Safari ouvre :", "Emplacement de téléchargement des fichiers :", "Supprimer la liste des téléchargements :", "Meilleurs sites affichés :", "Les nouveaux onglets ouvrent :", "Supprimer les éléments de l’historique :", "Favoris affichés :", "Les nouvelles fenêtres ouvrent :"}
set currentLoc to value of pop up button 2
end tell -- group
end tell
--Check if it's correct folder
display dialog "Is this the correct location for Downloads." & linefeed & currentLoc buttons {"Yes", "No"}
if button returned of result is not "Yes" then
tell group 1 to tell group 1 to tell pop up button 2
click it
tell menu 1
-- name of menu items--> {"Téléchargements", missing value, "Demander pour chaque téléchargement", "Autre emplacement…"}
click menu item -1
end tell -- menu
end tell
repeat 20 times
if exists sheet 1 then exit repeat
delay 0.1
end repeat
delay 0.2
repeat 600 times
-- Now the window allowing you to select the destination is open.
if not (exists sheet 1) then exit repeat
-- You will stay here as long as you will not validate your choice
delay 1
end repeat
end if
click button 1 -- close the Preferences window
end tell
end tell
end tell
Just for curious readers, here is the beginning of my original proposal, when I hadn’t found how to grab the localized name of the “General” window.
tell application "Safari"
activate
set openWith_loc to localized string "100177.title" from table "GeneralPreferences" --> "Au démarrage, Safari ouvre :"
-- at least in in High Sierra and Mojave, the English value of this string is not accessible
if openWith_loc is "100177.title" then set openWith_loc to "Safari opens with:" -- check that it's the correct English spelling
end tell
tell application "System Events"
tell application process "Safari"
set frontmost to true
-- Select General tab
keystroke "," using {command down}
delay 1
tell window 1
-- class of UI elements --> {group, button, button, button, toolbar, static text}
tell toolbar 1
-- class of UI elements --> {button, button, button, button, button, button, button, button, button, button}
-- name of buttons --> {"Général", "Onglets", "Remplissage automatique", "Mots de passe", "Recherche", "Sécurité", "Confidentialité", "Sites web", "Extensions", "Avancées"}
click button 1 -- select pane "General"
end tell
-- set wName to its name --> "Général"
tell group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {button, pop up button, static text, text field, static text, button, static text, checkbox, pop up button, static text, pop up button, static text, static text, static text, pop up button, pop up button, pop up button, static text, static text, static text, pop up button, pop up button}
-- I am unable to extract the localized name of the window "General" so I use a workaround.
repeat 50 times -- wait for the availability of window "General"
if exists (first static text whose value is openWith_loc) then exit repeat
delay 0.2
end repeat
…
Of course, you may delete the instructions which are disabled.
They were useful to build the script.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 2 mars 2020 19:40:45