I have been working on getting our new Mac Mini (macmini4,1) machines ready to become Mac OS X 10.6 testing machines. One of the last things needed before I start testing on a larger scale is a program to set the resolution on the command line. We need this because our tests depend on having a specific resolution set. Even if there was no dependency on a specific resolution, the GPU could perform differently depending on the size of the display it is working on. As well, we run these minis without a real display so its really important that we have active confirmation that the desired resolution is set.
Previously, we used cscreen. This is a utility that has fallen off the internet and was only ever released as a PowerPC binary. I needed to decide between finding a replacement for cscreen and installing Rosetta. While we currently have Rosetta on our testing slaves, I didn’t want us to depend on this. This is because partly because I just don’t think its good hygiene and partly because I want to prepare for OS X 10.7, where Rosetta has been removed. I searched the internet for a tool to do this, and sadly, there was nothing that fit the bill. I tried alternatives like modifying the com.apple.Boot.plist file to specify resolution, but it didn’t work. I also found some binary plist files that I could have edited and might have worked, but it seemed like a really flaky solution that would break *all* *the* *time*. I decided that I’d have to write something to do this.
I did find a program that looked like it would be a good starting point, however, this program lacked any error checking. Furthermore, when I investigated the APIs used, it looked like many of the important APIs were deprecated in 10.6 and would not be available in 10.7. One particularly annoying deprecation was CGDisplayBestModeForParameters, because it takes the exact data I have (height, width and bit depth) and picked the best mode available. In the end, I think I have a better solution that iterates through all modes and picks one that matches my inputs exactly. I also noticed that CGDisplayBestModeForParameters doesn’t always return a mode that matches exactly to what was requestion, though, it does have facilities to deal with this case.
Alas, I started writing an application called screenresolution. This program can be used to query the current and available resolutions as well as setting the resolution on one or more screens attached to the system. This is my first C project in quite some time and I was never a C rockstar to begin with. If you have a desire for Mozilla testing infrastructure to not suck and know C and CoreGraphics please slide on over to the source to check for issues! I know that I could organize the code better and write a better command line interface, but I don’t think the benefits of that outweigh the cost of waiting even longer for these new machines.
I have posted thecode on my github account and have licensed it with the GPL v2 license in hopes that no one ever go through the search engine hell that was Friday Aug 26, 2011. Below is a build of verison 1.3. Why not 1.0? Even though I wrote a test for the program, I neglected to actually run it. Multiple times
Once I started to actually run the tests, I started finding issues before bumping version numbers, gasp!
Update: Thanks Josh Aas and Richard Newman for reviewing my code and the great suggestions! I have a new version posted
screenresolution-1.4.dmg
Because this is a command line application, I recorded a video of my machine running the test suite.

I’d really like to find a 10.5 version of this code.I can’t compile it (for some strange reason my /Developer is not working with the distributed makefile, I don’t know why. (I’m not a Mac developer, I’m a Unix developer so the makefile didn’t scare me, just the development environment).
What I’ve got is a PPC mini (just delivered by Marshall Clow) and I’d like to set it up headless to run some older applications.
Any help here?
Thanks,
David
Hi John
Thanks for sharing this tool with us.
It is a great help.
One thing I am missing though is the support for multiple monitors.
How do I change the resolution of the external display?
Thanks,
Kurt
Sorry, but the download-links are not working anymore
sorry, I’ll try to fix that when I get some time.
Links are still broken, a binary would be much appreciated! Thanks.
Ditto. Download links broken (gives 404). I downloaded source from git and was able to install version 1.6DEV (after editing the makefile to point to my location of PackageMaker)… but wanted to test out previous versions.
I have a question: I am not able to grep or wc the output. Why? I tried:
#screenresolution | grep “Display 1″
and
#screensresolution | wc -l
and they both output the same thing, and wc outputs that it found 0 lines..
I even tried:
resol=`screenresolution get` && echo $resol | grep “Display 1″
and still the same result:
2012-05-14 13:33:39.894 screenresolution[1505:707] starting screenresolution argv=screenresolution get
2012-05-14 13:33:39.964 screenresolution[1505:707] Display 0: 1440x900x32@0
2012-05-14 13:33:39.966 screenresolution[1505:707] Display 1: 1680x1050x32@60
Granted, the program works fine (it gets the correct measurements)… I just can’t script this guy because I can’t grep or wc it.
PS: I also just tried screenresolution get > resol.txt and the resulting file is empty!
Daniel, you’ll see that there are tags on the repo. You should be able to “git checkout 1.5″ to get a copy of the source tree as of 1.5, for example.
The reason you can’t grep stdout is that nothing is written to stdout. In order to get system logging to work, I switched to using NSLog, which prints messages to stderr. This was a good choice because it means that all calls to screenresolution are recorded in the system logs, but bad because it’s printing general information information to stderr instead of stdout. I’m happy to take a look at a pull request that fixes this
What we’ve been doing is directing stderr to stdout in our resolution checks in puppet.
Ah. Sorry. you responded before I did.
That explains it. I would love to help you out.. but I don’t know C, which is what I think you used to code. I know BASH
. But I’ll take a look any who. Perhaps I’ll learn a thing or two
.
But thanks for the response! I think I’ll just go ahead and direct stderr to stdout for now.
Running on Lion, 10.7.4, BTW.
Thanks for looking into the broken links!
..And it worked. Thanks, dude.
screenresolution get 2>&1 | grep “Display 1″
or the like for those who read this excellent post.
glad to hear!
Hello, John — I’d love to get my hands on the “beta” version of screenresolution that allows the user to specify refresh rate. (It would be perfect for writing a utility that would automatically set resolutions for our classrooms equipped with projectors.) Would it be possible for you (or possibly one of the other followers of this post) to compile and share the executable? (I’m not very knowledgeable in the art of compiling and I tried and failed to compile it on my own using the source at GitHub). Thanks to anyone who’s willing to lend a hand. — Steve
Links of 1.3/1.4 pkg builds are still broken, but I was able to pull it from The Wayback Machine’s back up!
Thanks, very useful.