I downloaded your examples but I don’t understand how you selected the wanted block after revealing the inspector.
I take the opportunity to post an enhanced version of the KniazidisR’s proposal.
I built it because during my tests I clicked on the radio button [Resources] which modified the behavior of the Web Inspector and discovered that the setting remains as is as long as I didn’t reset the original setting.
So I decided to urge the script to do the job.
As is, my version used the ‘developed’ structure.
You are free to compact it if you want.
Of course we may add the new instructions in the code triggering the contextual menu.
Other interesting news, I found the file storing the name of the menu item triggered in KniazidisR’s script. It’s a file named localizedStrings.js located in (path to library folder from system domain as text)& “PrivateFrameworks:WebInspectorUI.framework:Versions:A:Resources:fr.lproj:” (en.lproj for the English resource).
I have a script able to extract the localized version ‘on the fly’ but, in text format it uses 3154 bytes so I’m not sure that it’s a good idea to embed it in main scripts.
I will work again to refine it and will post it here some times ago so everybody will be free to use it. I will also post the script scanning the available resource files to identify the interesting one.
It’s boring because some files are encoded using utf8 but some others use ut16 :rolleyes:
-- https://macscripter.net/viewtopic.php?id=45871
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------
# Extract the localized strings
set theBundle to ((path to library folder from system domain as text) & "PrivateFrameworks:PrintingPrivate.framework:Versions:Current:Plugins:PrintCocoaUI.bundle:Contents:Resources:") as «class furl»
set x to "Save"
set save_loc to localized string x from table "Localizable" in bundle theBundle --> "Enregistrer"
set theBundle to ((path to library folder from system domain as text) & "PrivateFrameworks:Safari.framework:") as «class furl»
set x to "Add page to Reading List"
set addPageToReadingList_loc to localized string x from table "Localizable" in bundle theBundle --> "Ajouter la page à la liste de lecture"
set x to "Reload this page"
set reloadThisPage_loc to localized string x from table "Localizable" in bundle theBundle --> "Recharger cette page"
set x to "Show Web Inspector"
set showWebInspector_loc to localized string x from table "Localizable" in bundle theBundle --> "Afficher l’inspecteur web"
set x to "Hide Web Inspector"
set hideWebInspector_loc to localized string x from table "Localizable" in bundle theBundle --> "Masquer l’inspecteur web"
set x to "Develop"
set develop_loc to localized string x from table "Localizable" in bundle theBundle --> "Développement"
set x to "Capture Screenshot"
set captureScreenshot_loc to localized string x from table "Localizable" in bundle theBundle --> "Développement"
-- build the path to the resource file so we may reuse it if several strings are defined in it.
set theSource to my getResourceFile:((path to library folder from system domain as text) & "PrivateFrameworks:WebInspectorUI.framework:Versions:A:Resources:") fromTable:"localizedStrings.js"
set x to "Capture Screenshot"
set captureScreenshot_loc to my localize:x storedIn:theSource
tell application "Safari"
activate
open location "https://www.apple.com"
end tell
tell application "System Events" to tell application process "Safari"
set frontmost to true
repeat with i from 2 to (count groups of toolbar 1 of window 1)
try
if (description of UI elements of group i of toolbar 1 of window 1) contains addPageToReadingList_loc then exit repeat
--> i = 2 --> {"uBlock Safari Icon"}
--> i = 3 --> {"Ajouter la page à la liste de lecture", "Certificat de Validation étendue Apple Inc.", "Adresse et recherche", "Interrompre le chargement de cette page"}
--> i = 4 -- why not ?
end try
end repeat
-- Now i is the index of the group to check.
-- After all we may imagine a 3rd party item installing a group at index 4
-- as uBlock install its one at index 2
repeat 100 times
if (exists UI element reloadThisPage_loc of group i of toolbar 1 of window 1) then exit repeat
delay 0.1
end repeat
tell me to display notification "The webpage is fully loaded now."
end tell
# Build the path of the folder storing the screenshot to fit your needs
set thePictures to (path to pictures folder as text)
set theFolderName to "Screenshots"
set theScreenshotsFolder to thePictures & theFolderName
tell application "System Events"
if not (exists folder theScreenshotsFolder) then make new folder at end of folder thePictures with properties {name:theFolderName}
end tell
set posixScreenshotsFolder to POSIX path of theScreenshotsFolder
tell application "System Events" to tell application process "Safari"
set frontmost to true -- Useful if we don't use the block downloading the Web page
-- keystroke "i" using {command down, option down}
click menu item showWebInspector_loc of menu 1 of menu bar item develop_loc of menu bar 1
repeat until tab group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1 exists
delay 0.1
end repeat
tell group 1 of tab group 1 of splitter group 1 of window 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}
tell UI element 1
-- class of UI elements --> {group, toolbar, tab, group, group, group, group, group}
tell tab group 1
-- class of UI elements --> {group, radio button, radio button, radio button, radio button, radio button, radio button, radio button, radio button, radio button, radio button, radio button}
-- name of radio buttons --> {"Éléments", "Réseau", "Débogueur", "Ressources", "Chronologies", "Stockage", "Canvas", "Audit", "Console", missing value, missing value}
click radio button 1 -- reset the 'standard' behavior
end tell
tell group 2
-- class of UI elements --> {group, group}
tell group 2
-- class of UI elements --> {group, group, group}
tell group 2
-- class of UI elements --> {static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text}
-- name of static text 1 --> "<"
tell static text 1 to perform action "AXShowMenu"
end tell
end tell
end tell
end tell
end tell
end tell
-- name of menu items of menu 1 --> {"Ajouter", "Modifier", "Copier", "Supprimer", "Activer/Désactiver la visibilité", missing value, "Pseudo-classes forcées", missing value, "S’arrêter", missing value, "Consigner l’élément", "Capturer l’aperçu", "Faire défiler dans la présentation", missing value, "Tout développer", "Tout condenser"}
click menu item captureScreenshot_loc of menu 1
end tell
repeat until exists sheet 1 of window 1
delay 0.1
end repeat
keystroke "g" using {command down, shift down} -- to define the storage folder
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
# CAUTION. before 10.11 the target field was a text field. Now it's a combo box
-- class of UI elements
--> {static text, text field, button, button} -- with 10.10 and older systems
--> {static text, combo box, button, button} -- 10.11, 10.12, 10.13, 10.14, 10.15
if exists combo box 1 then
set value of combo box 1 to posixScreenshotsFolder
else
set value of text field 1 to posixScreenshotsFolder
end if
-- name of buttons (*Aller, Annuler*)
keystroke return # ditto click button 1 (ou Aller)
end tell
click button save_loc of sheet 1 of window 1
click menu item hideWebInspector_loc of menu 1 of menu bar item develop_loc of menu bar 1
end tell
#=====
on getResourceFile:path2resources fromTable:jsName
set thisLocale to current application's NSLocale's currentLocale()
set langX to thisLocale's localeIdentifier as text --> "fr_FR"
set lang2 to text 1 thru 2 of langX
if langX starts with "zh_Hans" then # "zh-Hans"
set path2lproj to path2resources & "zh_CN.lproj"
else if langX starts with "zh_Hant" then # "zh-Hant"
set path2lproj to path2resources & "zh_TW.lproj"
else if langX starts with "pt_PT" then -- "pt_PT"
set path2lproj to path2resources & "pt_PT.lproj"
else if langX starts with "pt_BR" then -- "pt_BR"
set path2lproj to path2resources & "pt_BR.lproj"
else if lang2 is in {"ar", "ca", "cs", "da", "el", "fi", "he", "hr", "hu", "id", "ko", "ms", "pl", "pt", "ro", "ru", "sk", "sv", "th", "tr", "uk", "vi"} then
set path2lproj to path2resources & lang2 & ".lproj"
else if lang2 is "de" then # {"de", "de-AT", "de-CH"}
set path2lproj to path2resources & "German.lproj"
else if lang2 is "en" then # {"en", "en_AU", "en_CA", "en_GB", "en_US"}
set path2lproj to path2resources & "English.lproj"
else if lang2 is "es" then # {"es", "es_419", "es_ES", "es_MX"}
set path2lproj to path2resources & "Spanish.lproj"
else if lang2 is "fr" then # {"fr_FR", "fr_CA", "fr_CH"}
tell application "System Events"
if exists folder (path2resources & "French.lproj") then
set path2lproj to path2resources & "French.lproj"
else
set path2lproj to path2resources & "fr.lproj"
end if
end tell
else if lang2 is "it" then
set path2lproj to path2resources & "Italian.lproj"
else if lang2 is "ja" then
set path2lproj to path2resources & "Japanese.lproj"
else if lang2 is "nb" then
set path2lproj to path2resources & "no.lproj"
else if lang2 is "nl" then # {"nl", "nl_BE"}
set path2lproj to path2resources & "Dutch.lproj"
end if
return (path2lproj & ":" & jsName) as «class furl»
end getResourceFile:fromTable:
#=====
on localize:the_Key storedIn:theFile
try
set itsText to read theFile as «class utf8»
on error
set itsText to read theFile as «class ut16»
end try
set theDelim to "localizedStrings[" & quote & the_Key & quote & "] = " & quote
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theDelim}
set l to text items of itsText
set AppleScript's text item delimiters to oTIDs
set key_loc to paragraph 1 of (item 2 of l)
set key_loc to text 1 thru -3 of key_loc
return key_loc
end localize:storedIn:
#=====
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 8 mars 2020 21:56:14