Created
July 21, 2016 20:38
-
-
Save jminor/3a8c806a22a91b898008063d1ecc4383 to your computer and use it in GitHub Desktop.
Change your Mac's screen resolution from the command line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/swift | |
import Foundation | |
var shouldPrintUsage = true | |
var shouldPrintModes = true | |
var shouldSwitchMode = false | |
var didSwitchMode = false | |
var desiredDisplayNumber: Int? = nil | |
var desiredWidth: Int? = nil | |
var desiredHeight: Int? = nil | |
var desiredRefreshRate: Double? = nil | |
if Process.arguments.count > 1 { | |
desiredDisplayNumber = Int(Process.arguments[1]) | |
} | |
if Process.arguments.count > 2 { | |
desiredWidth = Int(Process.arguments[2]) | |
shouldSwitchMode = true | |
shouldPrintUsage = false | |
shouldPrintModes = false | |
} | |
if Process.arguments.count > 3 { | |
desiredHeight = Int(Process.arguments[3]) | |
} | |
if Process.arguments.count > 4 { | |
desiredRefreshRate = Double(Process.arguments[4]) | |
} | |
if shouldPrintUsage { | |
print("Usage: \(Process.arguments[0]) [<displayNumber> <width>] [<height>] [<refreshRate>]") | |
print("If no arguments are specified, then a list of available modes for each display will be printed.") | |
print("If you specify a displayNumber and width (and optionally height and refreshRate) then that display will switch to the first mode that matches the values you specified.") | |
print("After switching to a new mode, press Enter to quit the program and revert back to the default display mode.") | |
} | |
func isDesirableMode(displayNumber: Int, mode: CGDisplayModeRef) -> Bool | |
{ | |
if !shouldSwitchMode { return false } | |
let height = CGDisplayModeGetHeight(mode) | |
let width = CGDisplayModeGetWidth(mode) | |
let refreshRate = CGDisplayModeGetRefreshRate(mode) | |
if (desiredWidth == nil || width == desiredWidth) { | |
if (desiredHeight == nil || height == desiredHeight) { | |
if (desiredRefreshRate == nil || refreshRate == desiredRefreshRate) { | |
return true | |
} | |
} | |
} | |
return false | |
} | |
let MAX_DISPLAYS : UInt32 = 32 | |
var displays: [CGDirectDisplayID] = [] | |
for i in 0 ..< Int(MAX_DISPLAYS) { displays.append(0) } | |
var numDisplays: UInt32 = 0 | |
CGGetActiveDisplayList (MAX_DISPLAYS, &displays, &numDisplays) | |
if numDisplays == 0 { | |
print("ERROR: No displays found") | |
exit(1) | |
} | |
for displayNumber in 0 ..< Int(numDisplays) | |
{ | |
if shouldPrintModes { | |
print("Display: \(displayNumber)") | |
} | |
guard let modeArray = CGDisplayCopyAllDisplayModes (displays[displayNumber], nil) as [AnyObject]? else { | |
print("ERROR: CGDisplayCopyAllDisplayModes returned nil") | |
exit(2) | |
} | |
let count = CFArrayGetCount (modeArray) | |
if count == 0 { | |
print("WARNING: Display \(displayNumber) has no modes.") | |
} | |
for modeNumber in 0 ..< count | |
{ | |
let mode = modeArray[modeNumber] as! CGDisplayMode | |
if shouldPrintModes { | |
print(" \(CGDisplayModeGetWidth(mode)) x \(CGDisplayModeGetHeight(mode)) @ \(CGDisplayModeGetRefreshRate(mode)) Hz") | |
} | |
if (isDesirableMode(displayNumber, mode: mode)) { | |
CGDisplaySetDisplayMode (displays[displayNumber], mode, nil) | |
didSwitchMode = true | |
break | |
} | |
} | |
} | |
if didSwitchMode { | |
print("Press Enter to revert back to the default display mode.") | |
_ = readLine() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment