Skip to content

Instantly share code, notes, and snippets.

@nicoster
Forked from jminor/screenres.swift
Last active November 28, 2022 02:41
Show Gist options
  • Select an option

  • Save nicoster/1c5b2dc5862caf60ff686c05cfa3e1e8 to your computer and use it in GitHub Desktop.

Select an option

Save nicoster/1c5b2dc5862caf60ff686c05cfa3e1e8 to your computer and use it in GitHub Desktop.
Change your Mac's screen resolution from the command line.
#!/usr/bin/swift
// fix build errors with Swift 5.
// Known issues: some resolution is not HiDPI
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 CommandLine.arguments.count > 1 {
desiredDisplayNumber = Int(CommandLine.arguments[1])
}
if CommandLine.arguments.count > 2 {
desiredWidth = Int(CommandLine.arguments[2])
shouldSwitchMode = true
shouldPrintUsage = false
shouldPrintModes = false
}
if CommandLine.arguments.count > 3 {
desiredHeight = Int(CommandLine.arguments[3])
}
if CommandLine.arguments.count > 4 {
desiredRefreshRate = Double(CommandLine.arguments[4])
}
if shouldPrintUsage {
print("Usage: \(CommandLine.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: CGDisplayMode) -> Bool
{
if !shouldSwitchMode { return false }
let height = mode.height
let width = mode.width
let refreshRate = mode.refreshRate
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 _ 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 as CFArray)
if count == 0 {
print("WARNING: Display \(displayNumber) has no modes.")
}
for modeNumber in 0 ..< count
{
let mode = modeArray[modeNumber] as! CGDisplayMode
if shouldPrintModes {
print(" \(mode.width) x \(mode.height) @ \(mode.refreshRate) Hz")
}
if (isDesirableMode(displayNumber: 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