2

I would like to have an AppleScript that toggles between "Default for display" in System Preferences and "Scaled" "Larger Text".

The script should detect the current state of the display and toggle to the other state (i.e., if in Default for display, switch to scaled larger text).

I gut this far with my script (that I got from this site and this stackexchange page), but it seems that I can't get to virtually "click" left most icon out of the five available ones:

tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.displays"
    reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
end tell

local indexToUse

-- Now lets make the necessary changes
tell application "System Events"
    tell tab group 1 of window "LG UltraFine" of application process "System Preferences" of application "System Events"
        tell radio group 1

            if (value of radio button "Scaled") = 0 then
                -- Click the "Scaled" radio button
                click radio button "Scaled"

                tell radio group 2
                    click radio button 1 of radio group 2
                end tell

            else
                click radio button "Default for display"
            end if
        end tell

    end tell
end tell

-- Quit "System Preferences"
quit application "System Preferences"

When running the script, if System Preferences are in "Default for display", I get the following Script Error: System Events got an error: Can’t get radio group 2 of radio group 1 of tab group 1 of window "LG UltraFine" of application process "System Preferences". Invalid index.

I would like to be able to click on the button circled in red in the attached screenshot. Screenshot of System Preferences → Displays → Scaled

  • Feel free to ask your follow on question - that could stand as a new thread. – bmike Aug 8 '17 at 12:04
  • To answer the now deleted bonus question, tell application "System Events" to get every UI element of window 1 of application process "System Preferences" and then e.g. tell application "System Events" to get properties of radio button 1 of window 1 of application process "System Preferences" where radio button 1 can be that or radio button "Left" and other designators, e.g. radio button 2 or radio button "Bottom", etc. – user3439894 Aug 8 '17 at 12:49
  • @pdeli Just using the reveal command for system preferences and omitting activate, runs the script in the background without bringing system preferences to the front – wch1zpink Aug 9 '17 at 3:52
  • @user3439894 Thank you for your answer. I tried your script, but for a strange reason AS does not seem to return the results for the "buttons" between the Scaled radio button and the "Larger texte – More Space" (I cannot stick the result given by AS because it is too long). However, as has been suggested by bmike who edited my post, I will open another thread for this "issue". Thanks again. – pdeli Aug 9 '17 at 13:09
  • @wch1zpink Thank you for your remark. You are completely right and I intended in doing what you suggest. However, I thought having the app appear would perhaps help me for "debugging"… Thanks again for your suggestion. – pdeli Aug 9 '17 at 13:11
1

Thanks to @wch1zpink and the suggestion to use the Watch Me Do option in Automator, here is a script that finally seems to do the job:

-- Portions of the script found on https://gist.github.com/mvaneijgen/2f48f859ca07d2e75b3a
-- Launch "System Preferences", open the "Displays" options and change to the "Display" tab
(* If error "System Events got an error: Script Editor is not allowed assistive access" appears, then System Preferences → Security & Privacy → Privacy → add Script Editor to "Allow the app to control your computer"*)
(* as per: https://stackoverflow.com/questions/31019916/is-not-allowed-for-assistive-access-error-when-running-applescript-from-java) *)

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
end tell

-- Now lets make the necessary changes
tell application "System Events"
    tell tab group 1 of window "LG UltraFine" of application process "System Preferences" of application "System Events"
        tell radio group 1

            if (value of radio button "Scaled") = 0 then -- Check if Scaled radio button is not selected
                click radio button "Scaled" -- Click the "Scaled" radio button

                -- and click on the icon above "Larger Text" (which is in fact a radio button)
                tell application "System Events"
                    tell tab group 1 of window "LG UltraFine" of application process "System Preferences" of application "System Events"
                        tell radio group 1 of group 2
                            click radio button 1
                        end tell
                    end tell
                end tell

                else -- Scaled radio button is already selected
                    click radio button "Default for display" -- therefore click on "Default for display"
            end if
        end tell
    end tell
end tell
  • Just an FYI... When using reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays" the line of code above it, set the current pane to pane id "com.apple.preference.displays", is totally needless. Also, why is local indexToUse there? It's not used at all. – user3439894 Aug 14 '17 at 23:05
  • @user3439894: Thank you for your remarks. Both set the current pane to pane id "com.apple.preference.displays" and local indexToUse were in the original script found at the web address mentioned at the beginning of the code. In indeed tried the script by commenting out those lines and it works perfectly. Thanks again… – pdeli Aug 15 '17 at 8:15
0

This works on my MacBook Pro running the latest version of Sierra. I just substituted "Built in Retina display" with yours. I think it should work for you.

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "LG UltraFine"
    click radio button "Scaled" of radio group 1 of tab group 1
    click radio button 1 of radio group 1 of group 1 of tab group 1
    delay 0.1 -- adjust this value as needed if you get a message that you need to click "ok".
    click button "OK" of sheet 1 -- on my MBP, choosing the lowest resolution displays a message that I need to click "ok". Just delete this command if you don't need it
end tell
quit application "System Preferences"

This is the message that shows up that I added the "click button" command for

enter image description here

This should set your display resolution back to default

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "LG UltraFine"
    click radio button "Scaled" of radio group 1 of tab group 1
    click radio button 3 of radio group 1 of group 1 of tab group 1
end tell
quit application "System Preferences"

And here is the toggle version

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "LG UltraFine"
    click radio button "Display" of tab group 1
    click radio button "Scaled" of radio group 1 of tab group 1
    tell radio group 1 of group 1 of tab group 1
        set getResolution to get value of radio button 1
    end tell
    if getResolution then
        click radio button 3 of radio group 1 of group 1 of tab group 1
    else
        click radio button 1 of radio group 1 of group 1 of tab group 1
        delay 0.1 -- adjust this value as needed if you get a message that you need to click "ok".
        try
            click button "OK" of sheet 1 -- on my MBP, choosing the lowest resolution displays a message that I need to click "ok". Just delete this command if you don't need it
        end try
    end if
end tell
quit application "System Preferences"

You can also “reverse engineer” it (for a lack of a better term) using Automator and “watch me do”. In Automator, start A “Watch me do recording” and when you are finished recording, select all of the steps in the recorded action and menu item Edit/Copy. Then switch over to the ScriptEditor and make a new document and “Paste” what you copied from Automator into the new document.

enter image description here

enter image description here

You can compile the script and run it if you would like. More importantly though, it will show you the exact names of your UI elements (tab 1, scroll area, etc.) That you can use to substitute into the code I posted earlier.

  • Thanks for your answer. However, when trying the scripts you uploaded, all of them give the same Script Error: System Events got an error: Can’t get radio group 1 of group 1 of tab group 1 of window "LG UltraFine" of process "System Preferences". Invalid index. The "Result" pane in Script Editor reads: error "System Events got an error: Can’t get radio group 1 of group 1 of tab group 1 of window \"LG UltraFine\" of process \"System Preferences\". Invalid index." number -1719 from radio group 1 of group 1 of tab group 1 of window "LG UltraFine" of process "System Preferences" – pdeli Aug 9 '17 at 13:05
0

I wrote a command line tool called displayplacer that let's you change screen resolutions through scripts for both single and multi-monitor setups.

Configure your screens how you like and execute displayplacer list. It will display the command to run to configure and arrange your screens in their current setup.

For example, this sets my 4K screen to "1080p" with scaling enabled. displayplacer "id:A46D2F5E-487B-CC69-C588-ECFD519016E5 res:1920x1080 scaling:on origin:(0,0) degree:0"

Also available via Homebrew brew tap jakehilborn/jakehilborn && brew install displayplacer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .