Gavin Brock
Gavin Brock

Reputation: 5087

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

Upvotes: 17

Views: 4946

Answers (2)

Gavin Brock
Gavin Brock

Reputation: 5087

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

Upvotes: 1

cobbal
cobbal

Reputation: 70713

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.

Upvotes: 10

Related Questions