17

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gavin Brock
  • 5,027
  • 1
  • 30
  • 33

2 Answers2

10

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
  • 69,903
  • 20
  • 143
  • 156
  • 1
    Note on 64bit, the int's are now long's – Gavin Brock Aug 11 '10 at 01:19
  • Can anyone give some basic instructions/steps on how to actually implement this? – Jed Daniels Sep 27 '11 at 17:51
  • 2
    Is this still the recommended way to move a window to another workspace in 2015? – Noitidart Jul 30 '15 at 14:20
  • 1
    All of the above answers do not seem to work anymore in 11.6. I get return code 0 from `CGSMoveWorkspaceWindowList`, which indicates success. But my windows do not move spaces at all. This might have broken earlier, but for sure it does not work in modern Mac OS X. I am not sure if there are replacement APIs, I did not find any. – SirVer Mar 23 '22 at 20:46
1

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
  • 5,027
  • 1
  • 30
  • 33
  • On 64bit, change the "I"->"L" and "IIiII" to "LLlLL" – Gavin Brock Aug 11 '10 at 01:20
  • I tried using this ruby script, but I get an error message that dl.sym only takes one argument. I'm running Mac OS X 10.6.6 (64bit). How should I execute your ruby script? Shouldn't I simply make a file containing the code and then running ruby *myfile*? Any help is much appreciated. –  Mar 29 '11 at 07:31
  • Make sure you are using /usr/bin/ruby - not some newer one: $ /usr/bin/ruby -v ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0] – Gavin Brock Mar 30 '11 at 04:22
  • Thank you :) Indeed I was using ruby 1.9.2. It works in ruby 1.8.7. Is it possible to change it such that it works in 1.9.2? –  Apr 01 '11 at 19:51
  • This is absolutely spectacular! Can you please link us to the documentation on ruby FFI, Im interested to see the types for arguments and return ie: `IIiII` i was wondering what they are. I am converting this to js-ctypes. – Noitidart Jul 30 '15 at 15:03