ThisSpace

One of the first things that happened to me in Spaces that irked me was its auto-switching to another space when I didn’t want it to. Sure, that’s probably what I’d want it to do a lot of the time, but if I’m trying to open a new window for an app that already has windows in other spaces, I have to select that app, create a new window, grab its title bar, and move back to the space I was in when I selected the app.

I was wondering what would happen if you could create a new window without activating the app, so I typed this into Script Editor:

tell application "iTerm"
set term to (make new terminal)
tell term
    launch session "default session"
    activate
end tell
end tell

{: lang=applescript }

The result is a brand new iTerm window in my current space, without regard to whether there might already be such a window in another space. Switching spaces from a script is pretty easy too — as long as you aren’t holding down any modifiers when the script runs (say, by having used command-r in Script Editor):

tell application "System Events"
    delay 1 -- so I can let go of command
    keystroke "2" using control down
    delay 1 -- so you can see one switch happen and then the other
    key code 125 using control down
end tell

{: lang=applescript }

The one missing piece is moving a window — I don’t think it’s possible to drag an object via GUI scripting, so you can’t grab the title bar of a window, switch spaces, and then release it. At least it might be possible to create a fairly complex setup from a login script…if you had a lot of time on your hands. :)

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

2 Responses to ThisSpace

  1. jasonvp says:

    I stumbled upon this entry while looking to solve the same problem w/Spaces and iTerm. Hopefully you can help me with this one: what if I want the new iTerm window that pops open to automatically run a command? I’m having a hell of a time trying to add that to your iTerm script without it automatically moving the window to the original Space.

    Is there any way to send the new iTerm window the:

    exec command “/path/to/command” ?

  2. Josh says:

    My iTerm Here script shows how to send commands to the new iTerm session.