Search code examples
safariapplescriptios-simulatordevtools

how do we reset the size of safari devtools with applescript for ios simulators?


how do we reset the size of safari dev-tools with apple-script for simulators? The url can be random and anything(any site). As can be seen from the screenshot, there is a setting option we get in safari devtools for simulators, where we can set the devtools size with "zoom" option. I wanted it to set to 100% with applescript doing the same job.

This script i was trying as shown below is trying to activate safari, then go the simulator device and click on the url. Then i am stuck on how to click on the settings tab on safari devtools and finally on zoom option to resize devtools with applescript

Below is the image:

Please see this image

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


Solution

  • I am not able to test in the same environment as you at the present time, however, he example AppleScript code, shown below, was tested in Script Editor under macOS Catalina and Safari version 14.0.3 (15610.4.3.1.6, 15610) with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

    • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

    Example AppleScript code:

    tell application "Safari" to activate
    delay 0.2
    tell application "System Events"
        tell front window of application process "Safari"
            
            if (value of pop up button 1 of group 19 of ¬
                group 4 of UI element 1 of scroll area 1 of ¬
                group 1 of group 1) is not "100%" then
                
                click ¬
                    pop up button 1 of group 19 of ¬
                    group 4 of UI element 1 of ¬
                    scroll area 1 of group 1 of group 1
                
                delay 0.1
                
                click ¬
                    menu item "100%" of menu 1 of group 1
                
            end if
            
        end tell
    end tell
    

    Notes:

    The example AppleScript code assumes that the front window of Safari is already loaded to the target page and or Web Inspector tab/window.

    The target tab of the Web Inspector does not appear to be JavaScript scriptable via AppleScript, however, it can be scripted using UI Scripting. Note that UI Scripting is kludgy at best and prone to failure due to changes in the hierarchical UI element structure of the OS and or application being scripted. Changes to the example AppleScript code presented may need to be made to your use in macOS Big Sur, or any other version of macOS and or Safari other than what it was tested and worked under.

    You can use Accessibility Inspector to check the hierarchical UI element structure under the current version of macOS and Safari your are using. You can also use AppleScript to query the various hierarchical UI element structures to ascertain which it the right one for the pop up button button.




    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.