Dockアイコンを非表示にして。を表示することを優先したいNSStatusItem
。StatusItemを作成できますが、Dockからアイコンを削除する方法がわかりません。:-/
何か案は?
Dockアイコンを非表示にして。を表示することを優先したいNSStatusItem
。StatusItemを作成できますが、Dockからアイコンを削除する方法がわかりません。:-/
何か案は?
アクティベーション ポリシーと呼ばれるものを使用できます。
// 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);
ドックアイコンを表示するか、ユーザーがアイコンを望まない場合はこれをバイパスします。
副作用が 1 つだけあります。アプリケーションのメニューは、フォーカスを失って回復するまで表示されません。
出典: Dock アイコンのオンとオフを切り替えるチェックボックスを作成する
個人的には、Info.plistオプションを設定せず、ユーザー設定に基づいてTransformProcessType(&psn, kProcessTransformToForegroundApplication)
orを使用することを好みます。TransformProcessType(&psn, kProcessTransformToUIElementApplication)
Xcode では「アプリケーションはエージェント (UIElement) です」と表示され、ブール値です。
Info.plist で空のスペースをコントロール クリックし、メニューから [行の追加] を選択します。「アプリケーションはエージェント (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
}