Pritesh Patel Pritesh Patel - 1 month ago 5x
Objective-C Question

Shell script to get all supported screen resolutions on mac

I want a shell script that

1)grabs my current screen resolution, (solved).

For example : to get current screen display code is :

system_profiler SPDisplaysDataType |grep Resolution

2)grabs all supported resolution as shown in picture below (unsolved).

or any Objective C code will be also useful

enter image description here


Swift code

    var displayConfig: CGDisplayConfigRef = nil
    let mainDisplayID = CGMainDisplayID()

    var displayMode = CGDisplayCopyDisplayMode(mainDisplayID).takeRetainedValue()
    var width = CGDisplayModeGetWidth(displayMode)
    var height = CGDisplayModeGetHeight(displayMode)

    print("current size: \(width)x\(height)\n")
    print("available sizes:\n")

    var modes = CGDisplayCopyAllDisplayModes(mainDisplayID, nil).takeRetainedValue()
    let modesCount = CFArrayGetCount(modes) - 1

    for i in 0...modesCount {
        var mode: CGDisplayModeRef = unsafeBitCast(CFArrayGetValueAtIndex(modes, i), CGDisplayModeRef.self)

        var width = CGDisplayModeGetWidth(mode)
        var height = CGDisplayModeGetHeight(mode)