Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa - Programmatically adding an application to all spaces

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

like image 491
Larvell Jones Avatar asked Sep 17 '11 22:09

Larvell Jones


3 Answers

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
like image 111
sosborn Avatar answered Oct 01 '22 13:10

sosborn


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.

like image 40
Sjoerd Avatar answered Oct 01 '22 13:10

Sjoerd


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

like image 34
Larvell Jones Avatar answered Oct 01 '22 15:10

Larvell Jones