[英]How to hide the Dock icon
我想優先隱藏 Dock 圖標並顯示NSStatusItem
。 我可以創建 StatusItem,但我不知道如何從 Dock 中刪除圖標。 :-/
有任何想法嗎?
我認為您正在尋找 Info.plist 中的LSUIElement
LSUIElement(字符串)。 如果此鍵設置為“1”,則啟動服務將應用程序作為代理應用程序運行。 代理應用程序不會出現在 Dock 或 Force Quit window 中。 盡管它們通常作為后台應用程序運行,但如果需要,它們可以到前台呈現用戶界面。
在此處查看有關打開/關閉它的簡短討論
您可以使用所謂的激活策略:
// 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];
// 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);
顯示它的停靠圖標,或者如果用戶選擇不想要該圖標,則繞過它。
只有一個副作用,應用程序的菜單在失去並重新獲得焦點之前不會顯示。
我個人不喜歡設置任何 Info.plist 選項,而是根據用戶設置使用TransformProcessType(&psn, kProcessTransformToForegroundApplication)
或TransformProcessType(&psn, kProcessTransformToUIElementApplication)
。
在 Xcode 4 中,它顯示為“應用程序是代理 (UIElement)”,它是 Boolean。
在您的 Info.plist 控件中單擊空白區域並 select 從菜單類型“應用程序是代理(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 位於應用程序包中,您不應編輯應用程序包中的任何文件,因為這將使包簽名無效。
我發現的最佳解決方案是基於這篇優秀的文章。 我的解決方案基於 Dan 的評論。 簡而言之,Cocoa 無法做到這一點,但只需一點點 Carbon 代碼就可以做到。
該文章還建議制作一個專門處理停靠圖標的輔助應用程序。 然后主應用程序會根據用戶的偏好啟動並終止該應用程序。 這種方法讓我覺得比使用 Carbon 代碼更健壯,但我還沒有嘗試過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.