views:

490

answers:

3

If I have several OS-X Terminal.app windows open, how can I move one Terminal window to another space?

I'm happy to use any scripting or programming language to achieve this, but would prefer AppleScript or calls to standard frameworks.

(Note this is to move only one window of an application not all windows.)

+2  A: 

Using private calls in Objective-C/C, unofficially listed here

#import <Foundation/Foundation.h>

typedef int CGSConnection;
typedef int CGSWindow;

extern OSStatus CGSMoveWorkspaceWindowList(const CGSConnection connection,
                                       CGSWindow *wids,
                                       int count,
                                       int toWorkspace);
extern CGSConnection _CGSDefaultConnection(void);


int main(int argc, char **argv) {
    CGSConnection con = _CGSDefaultConnection();

    // replace 2004 with window number
    // see link for details on obtaining this number
    // 2004 just happened to be a window I had open to test with
    CGSWindow wids[] = {2004};

    // replace 4 with number of destination space
    CGSMoveWorkspaceWindowList(con, wids, 1, 4);

    return 0;
}

Standard warnings apply about undocumented APIs: they are subject to breaking.

cobbal
Works great - thanks!
Gavin Brock
Note on 64bit, the int's are now long's
Gavin Brock
A: 

Based on cobbal's answer, code ported to ruby:

require 'dl';

wid = 2004

dl = DL::dlopen('/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices')

_CGSDefaultConnection = dl.sym("_CGSDefaultConnection", 'I');

CGSMoveWorkspaceWindowList = dl.sym("CGSMoveWorkspaceWindowList", 'IIiII');

con = _CGSDefaultConnection.call();

CGSMoveWorkspaceWindowList.call(con[0], wid, 1, 4);
Gavin Brock
On 64bit, change the "I"->"L" and "IIiII" to "LLlLL"
Gavin Brock
A: 

I'm new to objective C, and this doesn't compile at all when I try it. I'm have SL and latest version of Xcode, and I tried to it as a commanline project -64 bit.

Please help Me :-)))

(I crave for this!

Tommy

Tommy
@Tommy - If you are having trouble getting something working, you are much better off creating a new question on Stack Overflow (rather than putting your query in an answer here). In the question, give lots of details of what you have tried and what error messages you are getting.
Gavin Brock
I'm sorry, I should have done so, I was so eager. ( I found out pretty quickly that I should create the project as a cocoa project.)Best regardsTommy
Tommy