developer tip

Dock 아이콘을 숨기는 방법

optionbox 2020. 11. 29. 10:14
반응형

Dock 아이콘을 숨기는 방법


Dock 아이콘을 숨기고 NSStatusItem. StatusItem을 만들 수 있지만 Dock에서 아이콘을 제거하는 방법을 모르겠습니다. :-/

어떤 아이디어?


나는 당신이 LSUIElementInfo.plist에서 찾고 있다고 생각합니다.

LSUIElement (문자열). 이 키가 "1"로 설정되면 Launch Services는 응용 프로그램을 에이전트 응용 프로그램으로 실행합니다. 에이전트 응용 프로그램은 Dock 또는 강제 종료 창에 나타나지 않습니다. 일반적으로 백그라운드 애플리케이션으로 실행되지만 원하는 경우 사용자 인터페이스를 표시하기 위해 포 그라운드로 올 수 있습니다.

여기에서 켜기 / 끄기에 대한 짧은 토론을 참조하십시오.


활성화 정책을 사용할 수 있습니다.

목표 -C

// The application is an ordinary app that appears in the Dock and may
// have a user interface.
[NSApp setActivationPolicy: NSApplicationActivationPolicyRegular];

// The application does not appear in the Dock and does not have a menu
// bar, but it may be activated programmatically or by clicking on one
// of its windows.
[NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];

// The application does not appear in the Dock and may not create
// windows or be activated.
[NSApp setActivationPolicy: NSApplicationActivationPolicyProhibited];

스위프트 4

// The application is an ordinary app that appears in the Dock and may
// have a user interface.
NSApp.setActivationPolicy(.regular)

// The application does not appear in the Dock and does not have a menu
// bar, but it may be activated programmatically or by clicking on one
// of its windows.
NSApp.setActivationPolicy(.accessory)

// The application does not appear in the Dock and may not create
// windows or be activated.
NSApp.setActivationPolicy(.prohibited)

그러면 도크 아이콘이 숨겨집니다.

또한보십시오


응용 프로그램 번들을 수정하지 않는 Apple 지침을 준수하면서이를 수행하고 Mac App Store 앱 / (Lion 앱?)이 info.plist 수정으로 인해 서명이 깨지지 않도록 보장하려면 기본적으로 LSUIElement를 1로 설정할 수 있습니다. 응용 프로그램 시작 :

ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);

도크 아이콘을 표시하거나 사용자가 아이콘을 원하지 않는 경우이를 우회합니다.

한 가지 부작용이 있지만 응용 프로그램의 메뉴는 초점을 잃고 다시 얻을 때까지 표시되지 않습니다.

출처 : 확인란 만들기 Dock 아이콘 켜기 및 끄기

개인적으로 나는 어떤의 Info.plist 옵션을 사용 설정하지 않는 선호 TransformProcessType(&psn, kProcessTransformToForegroundApplication)또는 TransformProcessType(&psn, kProcessTransformToUIElementApplication)사용자 설정이 무엇인지에 기반을.


Xcode 4에서는 "Application is agent (UIElement)"로 표시되며 Boolean입니다.

Info.plist에서 빈 공간을 control- 클릭하고 메뉴에서 "행 추가"를 선택합니다. "Application is agent (UIElement)"를 입력합니다. YES로 설정합니다.

선택 사항으로 만들기 위해 코드에 다음 줄을 추가했습니다 (Valexa에게 감사드립니다!).

 // hide/display dock icon
if (![[NSUserDefaults  standardUserDefaults] boolForKey:@"hideDockIcon"]) {
    //hide icon on Dock
    ProcessSerialNumber psn = { 0, kCurrentProcess };
    TransformProcessType(&psn, kProcessTransformToForegroundApplication);
} 

Swift 업데이트 : (위에 제시된 두 가지 방법 모두 사용하면 결과가 동일합니다)

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
}

사용자 기본 설정으로 지정하려면 UIElement를 사용할 수 없습니다. UIElement는 애플리케이션 번들에 상주하므로 번들 서명이 무효화되므로 앱 번들의 파일을 편집하면 안됩니다.

The best solution I've found is based on this excellent article . My solution is based on the comment by Dan. In short, There's no way to do this with Cocoa, but it is possible with a tiny bit of Carbon code.

The article also suggests making a helper app that handles the dock icon exclusively. The main app then starts and kills this app depending on the users preferences. This approach strikes me as being more robust than using the Carbon code, but I haven't tried it yet.

참고URL : https://stackoverflow.com/questions/620841/how-to-hide-the-dock-icon

반응형