Search code examples
macossafariapplescript

how to set safari zoom level to 100% from the below shared image/steps with applescript or python script for macos desktop?


safari->preferences->websites->Page Zoom-> click on "when visiting other websites" and make this to 100% if option ranges in number from 50% to 200%

Need an applscript for this.

click on this to see the image where i am looking for the zoom setting to 100%


Solution

  • AppleScript 1: Set Safari default Page Zoom to 100%

    tell application "System Events" to tell application process "Safari"
      set frontmost to true
      keystroke "," using command down -- Open Safari Preferences window
      click button "Websites" of toolbar 1 of window 1
        -- Need for seeing, selecting or clicking groups (or panes) under Websites window
      tell group 1 of group 1 of window "Websites"
        select row 5 of table 1 of scroll area 1 of group 1
          -- group 1: left pane. row 5: Page Zoom 
        click pop up button of group 2
          -- group 2: right pane. Revealed from the command "UI element" in AppleScript 2 below
        click menu item "100%" of menu 1 of pop up button of group 2
      end tell
      keystroke "w" using {shift down, command down}
        -- Or, keystroke "w" using command down: close Preferences window
    end tell
    
    1. MacOS 11.6.8 Big Sur
    2. Explanation: see comments after -- on each lines.
    3. Add a line "delay 1" between action command (Eg: keystroke, click and select) lines if your Mac needs.
    4. You may script changes of Safari and other apps' Preferences in a similar way.

    AppleScript 2: Find out all UI elements after some UI actions

    To find out which UI elements (such as window, toolbar, button, group, scroll area, table, row, pop up button, menu, menu item) are available after some UI actions, put the command "UI element" under their parent UI element in Script Editor (NOT Automator). Eg:

    tell application "System Events" to tell application process "Safari"
      set frontmost to true
      keystroke "," using command down
      click button "Websites" of toolbar 1 of window 1 -- Need
      UI element of group 2 of group 1 of group 1 of window "Websites"
    end tell
    
    Result: (below the script in Script Editor) 
    --> {static text "Control the page zoom level on the websites below:" ...,
    group 1 ...,
    pop up button 1 of group 2 of group 1 of group 1 of window "Websites" of application process "Safari" of application "System Events",
      -- Useful in AppleScript 1 above
    static text "When visiting other websites:" ...,
    button "Remove" ...}
    

    Attention: the action command 'click button "Websites" ...' is required to display the UI elements after it.

    Note: Pls encourage new contributor and vote up if you find this answer useful. Thanks!