By Gavin Brock

2010-02-22 04:38:01 8 Comments

If I have several OS-X 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.)


@Gavin Brock 2010-03-12 11:24:04

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 =;[0], wid, 1, 4);

@Gavin Brock 2010-08-11 01:20:22

On 64bit, change the "I"->"L" and "IIiII" to "LLlLL"

@user681561 2011-03-29 07:31:07

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.

@Gavin Brock 2011-03-30 04:22:46

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]

@user681561 2011-04-01 19:51:53

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?

@Noitidart 2015-07-30 15:03:01

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.

@cobbal 2010-03-03 05:18:33

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.

@Gavin Brock 2010-08-11 01:19:27

Note on 64bit, the int's are now long's

@Jed Daniels 2011-09-27 17:51:50

Can anyone give some basic instructions/steps on how to actually implement this?

@Noitidart 2015-07-30 14:20:33

Is this still the recommended way to move a window to another workspace in 2015?

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] How can I Remove .DS_Store files from a Git repository?

  • 2008-09-20 09:15:50
  • John Topley
  • 620630 View
  • 1285 Score
  • 25 Answer
  • Tags:   macos git gitignore

2 Answered Questions

2 Answered Questions

[SOLVED] OSX: How can I programatically tell if I'm running in Terminal or iTerm?

1 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] Programmatically move Mac menu bar between displays

3 Answered Questions

2 Answered Questions

[SOLVED] Moving a Window to a New Space in Mission Control

2 Answered Questions

Sponsored Content