3

My own answer at https://apple.stackexchange.com/a/436069/53510 does not work for me now that my MacBook Pro has 12.6, and neither does this answer: https://apple.stackexchange.com/a/448556/53510

What do I need to update to get it to work again?

I want to easily toggle between "Default" and "More Space" shown here:

screenshot

To have a single keyboard shortcut that toggles between 2 resolutions:

  1. Automator > File > New > Quick Action > Choose
  2. Search for "Run AppleScript" and double-click the result.
  3. Replace the default script code with what I provide below.
  4. Try clicking the Run button a couple times to see if the toggling works.
  5. Save as "toggle_display_resolution". (Later, if you ever want to edit it, you'll open /Users/your_username/Library/Services/toggle_display_resolution.workflow in Automator).
  6. Close Automator.
  7. System Preferences > Keyboard > Shortcuts > Services
  8. Assign a keyboard shortcut to the new service that you created (which will probably be at the bottom of the list).

By the way, you may need to edit your "Security & Privacy" > Accessibility. See answer.

# https://apple.stackexchange.com/q/449891/53510
set monitor to 2
tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.displays"
    delay 2
    tell application "System Events"
        tell window "Displays" of application process "System Preferences"
            click button "Display Settings…"
            delay 2
            click row monitor of outline 1 of scroll area 1 of sheet 1
            click radio button "Scaled" of radio group 1 of sheet 1
            tell sheet 1
                # ----------------------------------
                # from "try" to "end try" is the problem:
                try
                    set selected_button to button "Resolution4, Selected"
                    click button "Resolution1"
                on error
                    click button "Resolution4"
                end try
                # ----------------------------------
            end tell
            delay 0.5
            click button "Done" of sheet 1
        end tell
    end tell
end tell
# The next line is optional and could be commented out by prepending with a hash (#).
delay 2
quit application "System Preferences"

I don't know how to specify the Object Specifier for buttons Resolution1 and Resolution4.

I tried adding "in collection 1" because the Accessibility Inspector says that the container's type is "collection" (see screenshot below), but that didn't work.

Accessibility Inspector screenshot

error screenshot

3 Answers 3

1

The command line utility screenresolution allows you to very easily change your display resolutions.

In Terminal.app, I installed screenresolution using Homebrew with the command...

brew install screenresolution

After the successful installation of the screenresolution command line utility in Terminal, the commands...

screenresolution list lists all of the possible Display resolutions I can use.

screenresolution get shows me my current Display resolution.

So now if I want to change my Display resolution to 1440x900, it's as easy as using this one line of code...

screenresolution set 1440x900x32@0

So now instead of adding a Run AppleScript command to your workflow, add a Run Shell Script command instead. You may need to add the full path like...

/usr/local/bin/screenresolution set 1440x900x32@0


NOTE: The following is what I used for testing and troubleshooting.


screenresolution list gave me the following results.

https://i.imgur.com/lay4HVe.png

From there I wanted to create a sorted list with only 1 available resolution per line and wrote that to a file called tmp.txt on my Desktop. I used that file to loop through each item in that file to test changing my Display resolution to that item.

This following command what is what I used to create the tmp.txt on my Desktop...

screenresolution list > ~/Desktop/tmp.txt ;grep -E '^ ' < ~/Desktop/tmp.txt |tr -cd "[:print:]" |tr ' ' '\n' |sort -n |uniq |sed '/^$/d' |pbcopy ;pbpaste > ~/Desktop/tmp.txt

Now the contents of my tmp.txt looks like this...

enter image description here

This following command is what I used to loop through each line of the ~/Desktop/tmp.txt file to try changing my Display resolution to that item with a 3 second delay between each.

while read line ;do screenresolution set "$line" ;sleep 3 ;done < ~/Desktop/tmp.txt

These are the results. Now I see which ones I can definitely use and which ones wont work.

enter image description here

I purposely edited a few lines of the tmp.txt file so i could generate some errors.

4
  • This looks so cool if it would work for me! But: github.com/jhford/screenresolution/issues/…
    – Ryan
    Commented Nov 10, 2022 at 20:56
  • Did you try the screenresolution get command first to see what you are currently set at? I would do that first and then manually in System Settings.app and change your Display Res. to something else and then go back in Terminal and screenresolution set and insert the result from the screenresolution get result and see if that works.
    – wch1zpink
    Commented Nov 10, 2022 at 21:24
  • Yep, that's what I'd tried.
    – Ryan
    Commented Nov 10, 2022 at 22:23
  • I just noticed your update. Thanks. I tried your commands (including your loop), and none worked. All said Error: mode ____.000000 not available on display 0 with lots of extra precision.
    – Ryan
    Commented Dec 5, 2022 at 14:20
0

I finally figured it out:

To have a single keyboard shortcut that toggles between 2 resolutions:

  1. Automator > File > New > Quick Action > Choose
  2. Search for "Run AppleScript" and double-click the result.
  3. Replace the default script code with what I provide below.
  4. Try clicking the Run button a couple times to see if the toggling works.
  5. Save as "toggle_display_resolution". (Later, if you ever want to edit it, you'll open /Users/your_username/Library/Services/toggle_display_resolution.workflow in Automator).
  6. Close Automator.
  7. System Preferences > Keyboard > Shortcuts > Services
  8. Assign a keyboard shortcut to the new service that you created (which will probably be at the bottom of the list).

By the way, you may need to edit your "Security & Privacy" > Accessibility. See answer.

# https://apple.stackexchange.com/q/449891/53510
set monitor to 2
tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.displays"
    delay 2
    tell application "System Events"
        tell window "Displays" of application process "System Preferences"
            click button "Display Settings…"
            delay 2
            click row monitor of outline 1 of scroll area 1 of sheet 1
            click radio button "Scaled" of radio group 1 of sheet 1
            tell sheet 1
                # ----------------------------------
                # from "try" to "end try" is the problem:
                try
                    set selected_button to button "Resolution4, Selected" of UI element 8
                    click button "Resolution1" of UI element 8
                on error
                    click button "Resolution4" of UI element 8
                end try
                # ----------------------------------
            end tell
            delay 0.5
            click button "Done" of sheet 1
        end tell
    end tell
end tell
# The next line is optional and could be commented out by prepending with a hash (#).
delay 2
quit application "System Preferences"

How did I figure out the "of UI element 8" part?

In Automator, I pressed the Record button, which recorded a Watch Me Do script of me manually editing Displays settings.

Then I dragged the steps from Watch Me Do into AppleScript and got the hint.

0

Now that I'm on macOS Sonoma 14.5, my previous answer no longer works.

https://apple.stackexchange.com/a/417794/53510 led me to this solution, which seems to work well for me:

  1. brew tap jakehilborn/jakehilborn && brew install displayplacer
  2. touch toggle_resolution.sh
  3. Make it executable: chmod +x toggle_resolution.sh
  4. displayplacer list and find the ID of your display.
  5. Edit its contents to be the code below, but using your display's ID and your desired resolutions.
#!/bin/bash

# https://apple.stackexchange.com/q/449891/53510

low_resolution="1920x1080"
high_resolution="3840x2160"

# Run displayplacer list and capture its output:
display_output=$(/opt/homebrew/bin/displayplacer list)

# Find the first instance of "Resolution:" and extract the current resolution value:
current_resolution=$(echo "$display_output" | grep -m 1 "Resolution:" | awk '{print $2}' | tr -d '[:space:]')

echo "Current resolution: [$current_resolution]"
echo "high_resolution: [$high_resolution]"

if [ "$current_resolution" == "$high_resolution" ]; then
    echo "Decreasing resolution to $low_resolution:"
    /opt/homebrew/bin/displayplacer "id:A5FA6A38-265A-E603-947C-278988C5CF85 res:$low_resolution hz:60 color_depth:8 scaling:on degree:0"
else
    echo "Increasing resolution to $high_resolution:"
    /opt/homebrew/bin/displayplacer "id:A5FA6A38-265A-E603-947C-278988C5CF85 res:3840x2160 hz:60 color_depth:8 scaling:off degree:0"
fi

Create a keyboard shortcut to run that file:

  1. Automator > New > Quick Action > Run Shell Script
/Users/yourName/code/toggle_resolution.sh
  1. At the top, ensure that your Quick Action receives "no input".

Quick Action

  1. Save it as toggle_resolution
  2. System Settings > Keyboard > Keyboard Shortcuts... > Services > General > toggle_resolution (set a shortcut, such as CmdScrollLock, which I prefer on my Win keyboard)
3
  • Now this answer doesn't even work, now that my Mac is on Ventura 13.6.
    – Ryan
    Commented Oct 13, 2023 at 21:24
  • Now this version works for macOS Sonoma 14.5 (I updated my answer).
    – Ryan
    Commented Aug 5 at 18:06
  • Ryan, if needed, I just updated my original script for 15.1.1: apple.stackexchange.com/a/477031/27373
    – Zade
    Commented Nov 28 at 4:21

You must log in to answer this question.

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