Dockアイコンを非表示にする方法
-
05-07-2019 - |
質問
Dockアイコンを非表示にし、 NSStatusItem
を表示するように設定したい。
StatusItemは作成できますが、Dockからアイコンを削除する方法がわかりません。 :-/
アイデアはありますか
解決
Info.plistで LSUIElement
を探していると思います
LSUIElement(文字列)。このキーが“ 1”に設定されている場合、Launch Servicesはアプリケーションをエージェントアプリケーションとして実行します。エージェントアプリケーションは、Dockまたは強制終了ウィンドウに表示されません。通常はバックグラウンドアプリケーションとして実行されますが、必要に応じてフォアグラウンドに移動してユーザーインターフェイスを表示できます。
有効にする方法については、こちらの短いディスカッションをご覧くださいオン/オフ
他のヒント
アプリケーションバンドルを変更しないというAppleのガイドラインを順守し、Mac App Storeアプリ/(Lionアプリ?)がinfo.plistの変更によって署名が破損しないようにするには、デフォルトでLSUIElementを1に設定できますその後、アプリケーションが起動したら:
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
ドックアイコンを表示するか、ユーザーがアイコンを望まない場合はこれをバイパスします。
副作用が1つだけあります。アプリケーションのメニューは、フォーカスを失い、取り戻すまで表示されません。
出典:チェックボックスの作成Dockアイコンの切り替えオン/オフ
個人的には、Info.plistオプションを設定せず、 TransformProcessType(& psn、kProcessTransformToForegroundApplication)
または TransformProcessType(& psn、kProcessTransformToUIElementApplication)
を使用しますユーザー設定。
アクティベーションポリシーと呼ばれるものを使用できます:
Objective-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];
Swift 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)
これにより、ドックアイコンが非表示になります。
参照
- これに影響を与えた回答:(リムーバブル)ドックアイコンも持つヘルパーアプリケーション(LSUIElement)を作成する方法
-
NSRunningApplicationActivationPolicy
のドキュメント。 >
- 関連する質問:" Mac OS XでドックアイコンなしでGUIプロセスを開始する" 。
Xcode 4では、「Application is agent(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
}
それをユーザー設定にする場合は、UIElementを使用できません。 UIElementはアプリケーションバンドルに存在します。アプリケーションバンドルのファイルは編集しないでください。編集するとバンドルの署名が無効になります。
私が見つけた最良の解決策は、この優れた記事。私の解決策はダンのコメントに基づいています。要するに、Cocoaでこれを行う方法はありませんが、ほんの少しのCarbonコードで可能です。
この記事では、ドックアイコンのみを処理するヘルパーアプリを作成することも提案しています。メインのアプリが起動し、ユーザーの好みに応じてこのアプリを終了します。このアプローチは、Carbonコードを使用するよりも堅牢であると思いますが、まだ試していません。