Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically move one Terminal.app window to another space?

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.)

like image 452
Gavin Brock Avatar asked Feb 22 '10 04:02

Gavin Brock


2 Answers

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.

like image 90
cobbal Avatar answered Oct 01 '22 14:10

cobbal


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);
like image 30
Gavin Brock Avatar answered Oct 01 '22 14:10

Gavin Brock