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

2

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

Dyun answered 22/2, 2010 at 4:38 Comment(2)
hi, did you achieve it ???? I am also trying to do thisThreecornered
sadly, no - I will update this question if I ever do find a way.Dyun
J
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.

Jacinthe answered 3/3, 2010 at 5:18 Comment(4)
Note on 64bit, the int's are now long'sDyun
Can anyone give some basic instructions/steps on how to actually implement this?Resiniferous
Is this still the recommended way to move a window to another workspace in 2015?Vitriolize
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.Servitor
D
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);
Dyun answered 12/3, 2010 at 11:24 Comment(5)
On 64bit, change the "I"->"L" and "IIiII" to "LLlLL"Dyun
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.Lefkowitz
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]Dyun
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?Lefkowitz
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.Vitriolize

© 2022 - 2024 — McMap. All rights reserved.