Cocoa - 以编程方式将应用程序添加到所有空间

发布于 2024-12-04 21:14:46 字数 49 浏览 0 评论 0原文

有没有办法以编程方式将应用程序添加到所有空间?我希望我的应用程序默认位于所有空间上。

is there a way to add an application to all spaces programmatically? I'd like my application to be on all spaces by default.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲念泪 2024-12-11 21:14:46

你需要的方法在 NSWindow 中。

对于 Lion 使用:

- (void)setCollectionBehavior:(NSWindowCollectionBehavior)behavior

对于 pre-Lion 覆盖以下内容以返回 YES:

- (BOOL)canBeVisibleOnAllSpaces

The methods you need are in NSWindow.

For Lion use:

- (void)setCollectionBehavior:(NSWindowCollectionBehavior)behavior

For pre-Lion override the following to return YES:

- (BOOL)canBeVisibleOnAllSpaces
梅窗月明清似水 2024-12-11 21:14:46

这段代码对我有用(至少在我最近从事的一个小项目中的 10.6.8 上):

-(void)windowDidLoad {
    // Make the window visible on all Spaces
    if([[self window] respondsToSelector: @selector(setCollectionBehavior:)]) {
        [[self window] setCollectionBehavior: NSWindowCollectionBehaviorCanJoinAllSpaces];
    }
    else if([[self window] respondsToSelector: @selector(canBeVisibleOnAllSpaces)]) {
        [[self window] canBeVisibleOnAllSpaces]; // AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER_BUT_DEPRECATED
    }
}

我将此代码放在主应用程序窗口的 WindowController(自定义子类)中。

This piece of code works for me (at least on 10.6.8 in a little project I recently worked on):

-(void)windowDidLoad {
    // Make the window visible on all Spaces
    if([[self window] respondsToSelector: @selector(setCollectionBehavior:)]) {
        [[self window] setCollectionBehavior: NSWindowCollectionBehaviorCanJoinAllSpaces];
    }
    else if([[self window] respondsToSelector: @selector(canBeVisibleOnAllSpaces)]) {
        [[self window] canBeVisibleOnAllSpaces]; // AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER_BUT_DEPRECATED
    }
}

I put this code in a (custom subclass of a) WindowController for the main app window.

听闻余生 2024-12-11 21:14:46

好的。仅以编程方式设置工作区应用程序绑定是行不通的。我尝试过:

1) Verified no entries were in System Preferences->Spaces

2) defaults write com.apple.dock workspaces-app-bindings -dict-add com.apple.mail 65544

3) killall Dock (also needed to kill System Preferences )

4) Opened System Preferences->Spaces to verify the Mail app entry 
   appeared and was set to Every Space

5) Launched Mail, but it was still stuck to Space 1

6) Only when I went back into System Preferences->Spaces and changed the 
   Mail app *from* Every Space and then *back* to Every Space did the Mail 
   app stick to every space

很明显系统首选项正在做一些额外的事情来激活设置。有谁知道这可能是什么?谢谢!

更新:所以我能够通过使用 applescript api 而不是用户默认 api 来实现此功能。下面的文章讲述了如何使用 applescript 附加条目。然后干掉码头。

Applescript;在 N 号空间中打开应用

Ok. Just setting the workspaces-app-bindings programmatically didn't work. I tried:

1) Verified no entries were in System Preferences->Spaces

2) defaults write com.apple.dock workspaces-app-bindings -dict-add com.apple.mail 65544

3) killall Dock (also needed to kill System Preferences )

4) Opened System Preferences->Spaces to verify the Mail app entry 
   appeared and was set to Every Space

5) Launched Mail, but it was still stuck to Space 1

6) Only when I went back into System Preferences->Spaces and changed the 
   Mail app *from* Every Space and then *back* to Every Space did the Mail 
   app stick to every space

So clearly system preferences is doing something extra to activate the setting. Does anyone know what this could be? Thanks!

Update: So I was able to get this working by using the applescript api instead of the user defaults api. The following post tells how to append an entry using applescript. Then just kill the dock.

Applescript; opening an app in Space number N

一个人的夜不怕黑 2024-12-11 21:14:46

使用 OS X 附带的默认命令,如下所示:

defaults write com.apple.dock workspaces-app-bindings -dict-add com.apple.mail 65544

通过发出上述命令,您可以将“com.apple.mail”标识的应用程序设置为出现在每个空间上。 65544 是一个神奇的值,表示“每个空格”。如果键值对(标识符+设置)存在,它将被覆盖。

请注意,您必须重新加载 Dock (killall Dock) 并以某种方式从应用程序内执行这些命令。在 Objective-C 中,您可以使用以下代码片段退出 Dock:

NSRunningApplication *dock = [NSRunningApplicationrunningApplicationWithBundleIdentifier:@"com.apple.dock"];
[dock terminate];

在 AppleScript 中,使用以下代码:

quit application "Dock"

Use the defaults-command that ships with OS X, like so:

defaults write com.apple.dock workspaces-app-bindings -dict-add com.apple.mail 65544

By issuing the above command, you set the application identified by “com.apple.mail” to appear on every space. 65544 is a magic value saying “every space”. If the key-value pair (identifier + settings) exists it will be overwritten.

Note that you have to reload the Dock (killall Dock) and somehow execute these commands from within your application. From within objective-c you can use the following snippet to quit the Dock:

NSRunningApplication *dock = [NSRunningApplicationrunningApplicationWithBundleIdentifier:@"com.apple.dock"];
[dock terminate];

From within AppleScript use the following:

quit application "Dock"
撞了怀 2024-12-11 21:14:46

您的应用程序委托应该如下所示......

#import "alwaysOnTopAppDelegate.h"

@implementation alwaysOnTopAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [window setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
}

@end

Your app delegate should look like this...

#import "alwaysOnTopAppDelegate.h"

@implementation alwaysOnTopAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [window setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文