Making a Checkbox Toggle The Dock Icon On and Off

前端 未结 5 889
不思量自难忘°
不思量自难忘° 2021-01-31 06:17

How would I make a checkbox hide the dock icon if it was checked? I have made a checkbox toggle a menubar item but how would you do it with the dock icon? Looking for some code

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 06:54

    Update for Swift, use both ways has been presented above (they give the same result):

    public class func toggleDockIcon_Way1(showIcon state: Bool) -> Bool {
        // Get transform state.
        var transformState: ProcessApplicationTransformState
        if state {
            transformState = ProcessApplicationTransformState(kProcessTransformToForegroundApplication)
        }
        else {
            transformState = ProcessApplicationTransformState(kProcessTransformToUIElementApplication)
        }
    
        // Show / hide dock icon.
        var psn = ProcessSerialNumber(highLongOfPSN: 0, lowLongOfPSN: UInt32(kCurrentProcess))
        let transformStatus: OSStatus = TransformProcessType(&psn, transformState)
        return transformStatus == 0
    }
    
    public class func toggleDockIcon_Way2(showIcon state: Bool) -> Bool {
        var result: Bool
        if state {
            result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.Regular)
        }
        else {
            result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.Accessory)
        }
        return result
    }
    

提交回复
热议问题