I want to make a function to change the display size of macOS with swift.

Asked 2 years ago, Updated 2 years ago, 341 views

I would like to change the display size by pressing the icon that resides in menubar and the pop-up opens and pressing the change button.
I was able to get the current display size and decide the next display size, but I have not been able to change the important display size.
How do I change the display size?

Here's the code.

 iflet screen=NSScreen.screens.first(where: {$0.localizedName=="WR44-PLUS"){
    let frame = screen.frame
    let previousWidth = frame.width
    let height = frame.height
    let nextWidth=previousWidth==3840.0 ?1920.0:3840.0
    let size = NSSize (width: nextWidth, height: height)
    let rect = NSRect (origin:frame.origin, size:size)
    screen.setValue(rect, forKey: "frame")
    
    print(rect.size)
    // The size changes every time you press it, but the actual display size has not changed.
    // ↓↓↓ Output Results ↓↓↓
    // (1920.0, 1080.0) <- 1st button click
    // (3840.0, 1080.0) < - Second button click
    // (1920.0, 1080.0) <- 3rd button click
    // (3840.0, 1080.0) <- 4th button click
}

I am using an ultra-wide monitor.
Using PBP, one monitor has two display outputs (ps4 on the left and macOS on the right).
When I use macOS only, I use one display output, but it takes a little time to change the size of the display from the system environment settings, so I wanted to change it with the buttons that reside in menubar.

I look forward to hearing from you.

swift macos

2022-10-11 01:00

1 Answers

displayplacer now works, but now I call shell (disable sandbox) in swift...

If possible, I would like to make it within Swift.

I looked inside the displayplacer, but the process was so complicated that I gave up chasing it.
Using CGConfigureDisplayOrigin, CGDisplayConfigRef, I understood up to xxx... but I couldn't get to the understandable materials.
If you know any good materials or methods, I look forward to hearing from you.

Current code.

import SwiftUI
import AppKit
import Foundation

@main
structureResolutionChangerApp:App {
    @ NSA ApplicationDelegateAdaptor (AppDelegate.self) vardelegate
    varbody:someScene{
        Settings {}
    }
}

classAppDelegate:NSObject,NSApplicationDelegate {
    
    var statusBarItem —NSStatusItem!
    var popover = NSPopover()
    
    func applicationDidFinishLaunching(_notification:Notification){
        popover.behavior=.transient
        popover.contentViewController=NSHostingController(rootView:ContentView())
        self.statusBarItem = NSSStatusBar.system.statusItem (withLength: CGFloat (NSStatusItem.variableLength))
        guard let button=self.statusBarItem.button else {return}
        button.image=NSImage(systemSymbolName: "camera.macro", accessibilityDescription:nil)
        button.action=#selector(menuButtonAction(sender:))
    }
    
    @objc func menuButtonAction(sender:AnyObject){
        iflet screen=NSScreen.screens.first(where: {$0.localizedName=="WR44-PLUS"){
            let frame = screen.frame
            let previousWidth = frame.width
            let height = frame.height
            let nextWidth=previousWidth==3840.0?1920:3840
            
            let task = Process()
            task.launchPath="/usr/bin/env"
            task.arguments=["/opt/homebrew/bin/displayplacer", "id:secretres:\(nextWidth)x\(height)hz:60 color_depth:8 scaling:offorigin:(0,0)degre:0" ]
            task.launch()
            task.waitUntilExit()
        }
    }
}


2022-10-11 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.