Applescript to toggle mirror displays

Hi there

I use two monitors with my desktop computer (2009 iMac running the latest version of mountain lion). If I don’t turn on the second monitor it is still detected by the computer, and therefore screens are not gathered. I could turn on the screen and drag the window, but I can also toggle the mirror displays checkbox in system preferences on and then off.

So, I’ve been trying to find an applescript to do this. There are a number of posts discussing it but none which seem to work. And, all of them do it graphically with the system prefs opening.

My questions are

  1. should it be possible to write an applescript that toggles mirror displays on and/or off?
  2. should it be possible to do that without using the GUI? ie something that runs in the background

If it should be possible then I’ll spend a bit more time trying to find out how - unless someone has an already working script lying around :smiley:

Cheers

Gareth

Browser: Safari 536.29.13
Operating System: Mac OS X (10.8)

Hi,

AppleScript is not able to change display settings without GUI scripting.
I wrote a small command line tool using Quartz Display Services.

toggleMirrorDisplays

Download it, save it somewhere and call it with

do shell script "/full/path/to/toggleMirrorDisplays"

replace /full/path/to with the real full path to the executable.
If the path contains space characters, use quoted form of

Hi Stefan

Thanks for posting the code. This has been bugging me for a while. I’ll give it a try when I get home.

How difficult are these to write? I quite like the idea of having a folder in the dock which provides shortcuts to things in system preferences. Is there a list of these anywhere?

Big cheers

G

If you’re familiar with the C language, it’s not very difficult

You can find the documentation here: Introduction to Quartz Display Services Programming Topics

Hello.

Quartz seems like a most interesting technology to learn. :slight_smile:

I had a script for showing the preference pane for organizing screens, so I modified it a wee bit to toggle mirroring.
You’ll discover soon why Stefan’s solution is preferable.

-- Very slow fade-in / fade-out: just for prosperity, use StefanK's
tell application id "sevs" to if not (UI elements enabled) is true then set UI elements enabled to true
tell application "System Preferences"
	reveal anchor "displaysArrangementTab" of pane "com.apple.preference.displays"
	activate
	tell application id "sevs"
		tell window 1 of application process "System Preferences"
			tell tab group 1
				click radio button 2
				click checkbox 1 of group 1
			end tell
		end tell
	end tell
	quit
end tell

McUserII

I had tried scripts similar to the one you have posted. I’m especially grateful to Stefan as his method using command line is a much less clunky solution. Not seen it anywhere else on google.

Stefan

The commend runs fine. However when I put the file in my folders it introduces a space. When I run the script it returns an error. You suggest using the quoted form. Am I right to understand that as “/full/name/to path/”?

Thanks

Gareth

just

do shell script quoted form of "/fu ll/pa th/to/toggleMirrorDisplays"

or

do shell script "'/fu ll/pa th/to/toggleMirrorDisplays'" -- double quote - single quote - text - single quote - double quote

I agree totally. The showing up of the preferences pane, is one thing, ( I think of that as cool, rather than clunky), but StefanK has lowered the fade in / fade out times to an acceptable level. Because at least I have to wait until like forever, until the UIScript is done. :slight_smile:

the fade in / fade out times are the default ones, there is no change.

By the way: the actual code are 24 lines, the error handling about 100

Then I urge you to try my script Stefan, because then I think UI Scripting or Applescript adds to the FadeIn/FadeOut.

The code would have been nice to see too, but I do understand if you want to keep it for yourself, I think I should be able to re-create something like it, should I need it.

It’s similar to that

I was also thinking of asking for the code.

I don’t know if it can be used with this but automator runs shell scripts, and I thought about adding it to that. My hope then was I could set up a workflow which runs the command twice (toggling on then off gathers the displays) and then quits terminal to lose the windows.

I may also eventually pluck up the courage to ask how to hide the terminal windows when the commend runs, but given what Stefan has done I feel that would be pushing things.

Cheers again.

Goldmine!

Could that be cut and pasted into automators “run shell script” command?

Cheers

No

Tried it, and it doesn’t work if you cut and paste. A noobie error.

The extra quotes works though - again, thank you.

G

Thanks for sharing Stefan! :slight_smile: