Making a Checkbox Toggle The Dock Icon On and Off

前端 未结 5 881
不思量自难忘°
不思量自难忘° 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条回答
  • 2021-01-31 06:52

    Setup your application as an LSUIElement and then call:

    [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
    

    on launch.

    This works for the MAS too.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2021-01-31 06:57

    (Posting this as an answer because comments don't have code formatting)

    QSBApplicationDelegate.m:223-228

    BOOL iconInDock = [[NSUserDefaults standardUserDefaults] boolForKey:kQSBIconInDockKey];
    if (iconInDock) {
      ProcessSerialNumber psn = { 0, kCurrentProcess };
      TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    }
    
    0 讨论(0)
  • 2021-01-31 07:13

    i've use this code:

    BOOL iconInDock = [[NSUserDefaults standardUserDefaults] boolForKey:smHideShowIcon];
    if (iconInDock) {
        ProcessSerialNumber psn = { 0, kCurrentProcess };
        // display dock icon
        TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    }
    

    ok, it's work for my application if I to set LSUIElement=1 in the Info.plist. That's code works only for show, but how I can hide icon?

    0 讨论(0)
  • 2021-01-31 07:13

    You would want to set up your application as LSUIElement, and then use TransformProcessType to enable the Dock icon. The app will need to be relaunched for the change to take effect. See the Google Quick Search Box project for an example.

    0 讨论(0)
提交回复
热议问题