GeertCu

Q: Script to switch between 2 resolutions

I'm fairly new to OSX (bought my first MAC last summer)

 

I find myself switching between 2 resolutions on my retina MacBook Pro.

I use 1920x1080 at work (professionaly, I run Windows8 in Fusion6 to manage the IT infrastructure) and 1280x720 at home (in the couch, with half an eye on the TV)

 

To switch between these 2 resolutions, I do the following steps

- CMD+SPACE

- type "display"

- ENTER

- use the mouse to switch to another resolution

- press OK

 

Wouldn't it be easyer if I had 1 program that:

- switches to 1920x1080 if the current resolution is 1280x720

- switches to 1280x720 if the current resolution is 1920x1080

 

A "shortcut" (or whatever it's called in OSX) on the desktop, or even with a key-combination would be great.

 

Any insights?

MacBook Pro (Retina, Mid 2012), OS X Mountain Lion (10.8.2), 16GB 256SSD w/ VMwareFusion

Posted on Sep 10, 2013 12:41 PM

Close

Q: Script to switch between 2 resolutions

  • All replies
  • Helpful answers

Page 1 Next
  • by Pierre L.,

    Pierre L. Pierre L. Sep 10, 2013 4:38 PM in response to GeertCu
    Level 5 (4,484 points)
    Sep 10, 2013 4:38 PM in response to GeertCu

    Maybe you might want to try the following script. Before using the script, you must enable access for assistive devices in the Accessibility System Preference pane.  To use the script, just copy and paste it into the AppleScript Editor window, then click the Run button.

     

     

    set resolution1 to "1920 × 1080"

    set resolution2 to "1280 × 720"

     

    tell application "Finder" to set {a, b, c, d} to bounds of window of desktop

    set currentResolution to (c as text) & " × " & d

     

    if currentResolution is resolution1 then

        set newResolution to resolution2

    else

        set newResolution to resolution1

    end if

     

    tell application "System Preferences"

        -- activate -- only if you wish to see the System Preferences window

        reveal pane id "com.apple.preference.displays"

        tell application "System Events"

            tell process "System Preferences"

                tell window 1

                    tell tab group 1

                        click radio button 1

                        click radio button 4

                        tell table 1 of scroll area 1

                            select (row 1 whose value of text field 1 is newResolution)

                        end tell

                    end tell

                end tell

            end tell

        end tell

        quit

    end tell

  • by GeertCu,

    GeertCu GeertCu Sep 12, 2013 4:24 AM in response to Pierre L.
    Level 1 (0 points)
    Sep 12, 2013 4:24 AM in response to Pierre L.

    That didn't work.

     

    This does

     

    local index1, index2, index3

     

    set index1 to 3 -- 1440 x 900 (Best for Retina)

    set index2 to 5 -- 1920 x 1200 (More Space)

     

    -- Launch "System Preferences", open the "Displays" options and change to the "Display" tab

    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 window "Color LCD" of application process "System Preferences" of application "System Events"

                        tell tab group 1

     

      -- Click the "Scaled" radio button

      click radio button "Scaled"

     

                                  tell radio group 1 of group 1

      -- Depending on what scale option/index is current selected, set the appropriate new option/index to use

                                            if (value of radio button index1) is true then

                                                      set indexToUse to index2

                                            else if (value of radio button index2) is true then

                                                      set indexToUse to index1

                                            end if

     

      -- Click the radio button for the new scale option/index

      click radio button indexToUse

                                  end tell

     

                        end tell

     

      -- If option/index 1 is selected a warning prompt is displayed, click the OK button to dismiss the prompt

                        if indexToUse = 1 then

      click button "OK" of sheet 1

                        end if

              end tell

    end tell

     

    -- Quit "System Preferences"

    quit application "System Preferences"

     

    It works, but it is painfully slow

  • by Pierre L.,

    Pierre L. Pierre L. Sep 12, 2013 7:06 AM in response to GeertCu
    Level 5 (4,484 points)
    Sep 12, 2013 7:06 AM in response to GeertCu

    GeertCu wrote:

     

    That didn't work.

     

    Could you be a little more explicit? Is there any error message?

     

    The script works flawlessly on my MacBook Pro under OS X 10.8.4, with the following values:

     

    set resolution1 to "1440 × 900"

    set resolution2 to "1280 × 800"

     

    Have you enabled access for assistive devices in the Accessibility System Preference pane?

     

    Message was edited by: Pierre L.

  • by Pierre L.,

    Pierre L. Pierre L. Sep 12, 2013 7:45 AM in response to GeertCu
    Level 5 (4,484 points)
    Sep 12, 2013 7:45 AM in response to GeertCu

    GeertCu wrote:

     

    That didn't work.

     

    Does the following improved version work?

     

    set resolution1 to "1920 × 1080" -- if that resolution exists on your monitor

    set resolution2 to "1280 × 720" -- if that resolution exists on your monitor

     

    tell application "Finder" to set {a, b, c, d} to bounds of window of desktop

    set currentResolution to (c as text) & " × " & d

     

    if currentResolution is resolution1 then

        set newResolution to resolution2

    else

        set newResolution to resolution1

    end if

     

    tell application "System Preferences"

        activate -- only if you wish to see the System Preferences window

        set current pane to pane id "com.apple.preference.displays"

        reveal anchor "displaysDisplayTab" of current pane

        tell application "System Events"

            tell process "System Preferences"

                tell window 1

                    tell tab group 1

                        click radio button "Scaled"

                        tell table 1 of scroll area 1

                            select (row 1 whose value of text field 1 is newResolution)

                        end tell

                    end tell

                end tell

            end tell

        end tell

        quit

    end tell

  • by GeertCu,

    GeertCu GeertCu Sep 13, 2013 12:06 PM in response to Pierre L.
    Level 1 (0 points)
    Sep 13, 2013 12:06 PM in response to Pierre L.

    Nope, doesn't work either...

     

    And yes, assistive touch is enabled, otherwise my working script will not work either...

     

    Screen Shot 2013-09-13 at 21.04.30.png

  • by Pierre L.,

    Pierre L. Pierre L. Sep 13, 2013 2:16 PM in response to GeertCu
    Level 5 (4,484 points)
    Sep 13, 2013 2:16 PM in response to GeertCu

    At this point, I can see only two explanations why the script doesn't work:

     

    1) A short delay is needed before telling table 1 to select a row, like this:

     

                        click radio button "Scaled"

                        repeat until table 1 of scroll area 1 exists

                        end repeat

                        tell table 1 of scroll area 1

                            select (row 1 whose value of text field 1 is newResolution)

                        end tell

     

    2) Maybe your Color LCD window is not exactly the same as mine:

     

    Capture d’écran 2013-09-13 à 5.05.47 PM.png

     

    If such is the case, I can't help you.


  • by GeertCu,

    GeertCu GeertCu Oct 23, 2013 12:17 AM in response to GeertCu
    Level 1 (0 points)
    Oct 23, 2013 12:17 AM in response to GeertCu

    ****.  My originel script doesn't work in Maverick anymore, it bugs off with:

     

    "Can't get window "Color LCD" of <class pcap> "System Preferences" of application "System Events".

     

    Urgh.

  • by twtwtw,

    twtwtw twtwtw Oct 23, 2013 2:51 AM in response to GeertCu
    Level 5 (4,935 points)
    Oct 23, 2013 2:51 AM in response to GeertCu

    I haven't downloaded Mav yet, so this is just extrapolation, but <class pcap> should be the code for application process in System Event's scripting dictionary. Check the scripting dictionary to make sure that they haven't for some odd reason removed that term (it should be in the processes suite), and if it's still there in the dictionary, delete and retype the phrase in the script. That forces the script to reread the terminology rather than relying on the previously compiled code.

  • by Kingster21,

    Kingster21 Kingster21 Oct 26, 2013 12:30 AM in response to GeertCu
    Level 1 (10 points)
    Oct 26, 2013 12:30 AM in response to GeertCu

    @GeertCu: I had the same problem. I had to change "Color LCD" to "Built-in Retina Display". Here's my script to switch resolution between retina and more space:

     

    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 "Built-in Retina Display"

              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 isRetinaOptimized to get value of radio button 3

              end tell

              if isRetinaOptimized then

                        click radio button 5 of radio group 1 of group 1 of tab group 1

              else

                        click radio button 3 of radio group 1 of group 1 of tab group 1

              end if

    end tell

    quit application "System Preferences"

  • by GeertCu,

    GeertCu GeertCu Oct 30, 2013 11:11 AM in response to GeertCu
    Level 1 (0 points)
    Oct 30, 2013 11:11 AM in response to GeertCu

    Changed the "Display" to "Built-in Retina Display", compiled the script and then ran it.  Now it bugs of with the following error message...

     

    Screenshot 2013-10-30 19.08.49.png

     

    I don't have the slightest idea what to do next.  Because there IS a radio button with that name?

  • by Kingster21,

    Kingster21 Kingster21 Oct 31, 2013 1:08 AM in response to GeertCu
    Level 1 (10 points)
    Oct 31, 2013 1:08 AM in response to GeertCu

    GeertCu,

     

    You seem to go from Built-in Retina Display to Scaled directly, while it first needs to select the "Display" tab group.

    See my script:

     

    tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"

              click radio button "Display" of tab group 1

              click radio button "Scaled" of radio group 1 of tab group 1

     

    First it selects Built-in Reinta Display, then it clicks the "Display" tab, THEN it clicks "Scaled"

  • by GeertCu,

    GeertCu GeertCu Oct 31, 2013 1:46 AM in response to Kingster21
    Level 1 (0 points)
    Oct 31, 2013 1:46 AM in response to Kingster21

    Hi Kingster,

     

    I gave up and just copied your script... It's a _lot_ faster than my original script, thanks!

  • by Kingster21,Solvedanswer

    Kingster21 Kingster21 Oct 31, 2013 1:46 AM in response to GeertCu
    Level 1 (10 points)
    Oct 31, 2013 1:46 AM in response to GeertCu

    Hi GeertCu,

     

    Sure:

     

    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 "Built-in Retina Display"

              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 isRetinaOptimized to get value of radio button 3

              end tell

              if isRetinaOptimized then

                        click radio button 5 of radio group 1 of group 1 of tab group 1

              else

                        click radio button 3 of radio group 1 of group 1 of tab group 1

              end if

    end tell

    quit application "System Preferences"

  • by GeertCu,

    GeertCu GeertCu Oct 31, 2013 2:13 AM in response to GeertCu
    Level 1 (0 points)
    Oct 31, 2013 2:13 AM in response to GeertCu

    Next up ...

     

    It would be _very_ nice to have something like this:

     

    At work I use the 1080p resolution, and at home in the couch I use the "best for retina" resolution.

    The script above switched _very_ easy between those 2 resolutions.

     

    But what if it would automatically switch - or even better "semi-automatically"...

     

    At home I have an Apple Airport in the 10.0.1.0/24 subnet, with 10.0.1.1 as gateway via DHCP.  At work it's 172.16.0.0/23 with gateway 172.16.0.254.  I'm sure we can use that to information to do the following.

     

    Automated version:

     

    - computer wakes from sleep

    - connects to WIFI

    - checks the default getaway

    - if it is home > set retina resolution

    - if it is work > set 1080p resolution

     

    Semi-automated version:

     

    - use the OSX Notification center with a custom alert "hey, you're at home, I'm gonna switch your resolution, if you don't want it, press "cancel" in the notification"

     

    That would be Awesome with a capital A.

Page 1 Next