How to make every-time last option selected in case of service workers sites in simulators for devtools using given applescript?
If you see the image, there are 2 sites: "linkedin.com and skype.com", I want if two sites are there, then everytime last option should be selected.
If only one site shows up, then only that should be selected.(this check should be included)
For now, my only need is last option should be selected if two options are showed up as shown in the image(linkedin.com and skype.com),then skype.com should be selected, else if one site shows up then only that should be selected.
Please help me to modify the below code itself, that will help me. It should be a small modification which I am not able to figure out in the below code.
tell application "Safari"
activate
delay 1
set opened to ""
tell application "System Events"
repeat 5 times
try
set SimulatorOptions to (get every menu item of menu 1 of menu bar item "Develop" of menu bar 1 of process "Safari" whose name starts with "Simulator")
log (count of SimulatorOptions)
if (count of SimulatorOptions) > 0 then
set options to item 1 of SimulatorOptions
set pagename to name of menu item 2 of menu 1 of options
if (pagename as string is equal to "missing value")
log ("Still not opened")
else
click menu item 2 of menu 1 of options
set opened to "true"
exit repeat
end if
end if
delay 1
end try
end repeat
end tell
close (every tab of every window whose name is equal to "Favourites" or name is equal to "Untitled" or name is equal to "Start Page")
end tell
copy opened to stdout
The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
- 1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
The following example AppleScript code assumes that Safari is already opened, Simulator is opened and Safari is opened in Simulator.
Example AppleScript code:
tell application "System Events"
tell application process "Safari"
if not (exists ¬
menu bar item "Develop" of ¬
menu bar 1) then return
tell ¬
menu 1 of ¬
menu bar item "Develop" of ¬
menu bar 1
set simulatorMenuName to ¬
the name of ¬
(menu items ¬
whose name starts with "Simulator") ¬
as string
if simulatorMenuName is equal to "" then return
set simulatorMenuNameMenuItems to ¬
the name of ¬
menu items of ¬
menu 1 of ¬
menu item simulatorMenuName
if item 1 of simulatorMenuNameMenuItems ¬
is not "Safari" then return
repeat with i from 1 to count simulatorMenuNameMenuItems
if item i of simulatorMenuNameMenuItems ¬
is equal to missing value then
set menuItemNumber to i - 1
exit repeat
end if
end repeat
tell menu 1 of menu item simulatorMenuName to ¬
click menu item menuItemNumber
end tell
end tell
end tell
Notes:
As coded, this will click the last URL, or first URL if only one is listed, on the Safari > Develop > Simulator — … menu under Safari on that menu.
The tell application "System Events"
block in this example AppleScript code stands alone and does not belong within a tell application "Safari"
block as shown in the code of the question.
If you prefer the code without the AppleScript line continuation characters and written in compact manner, then use:
tell application "System Events"
tell application process "Safari"
if not (exists menu bar item "Develop" of menu bar 1) then return
tell menu 1 of menu bar item "Develop" of menu bar 1
set simulatorMenuName to the name of (menu items whose name starts with "Simulator") as string
if simulatorMenuName is equal to "" then return
set simulatorMenuNameMenuItems to the name of menu items of menu 1 of menu item simulatorMenuName
if item 1 of simulatorMenuNameMenuItems is not "Safari" then return
repeat with i from 1 to count simulatorMenuNameMenuItems
if item i of simulatorMenuNameMenuItems is equal to missing value then
set menuItemNumber to i - 1
exit repeat
end if
end repeat
tell menu 1 of menu item simulatorMenuName to click menu item menuItemNumber
end tell
end tell
end tell
To address comment:
yes, but as i asked in the question itself, is it possible that you can modify my code only?, that i have posted.
If you want to use your code, but fixed to do what you want, then use:
tell application "Safari"
activate
delay 1
end tell
set opened to ""
tell application "System Events"
repeat 5 times
try
set SimulatorOptions to (get every menu item of menu 1 of menu bar item "Develop" of menu bar 1 of process "Safari" whose name starts with "Simulator")
log (count of SimulatorOptions)
if (count of SimulatorOptions) > 0 then
set options to item 1 of SimulatorOptions
set pagename to name of menu item 2 of menu 1 of options
if (pagename as string is equal to "missing value") then
log ("Still not opened")
else
repeat with i from 1 to count menu items of menu 1 of options
if name of menu item i of menu 1 of options is equal to missing value then
set menuItemNumber to i - 1
exit repeat
end if
end repeat
click menu item menuItemNumber of menu 1 of options
set opened to "true"
exit repeat
end if
end if
delay 1
end try
end repeat
end tell
tell application "Safari"
close (every tab of every window whose name is equal to "Favourites" or name is equal to "Untitled" or name is equal to "Start Page")
end tell
copy opened to stdout
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.