1

I'm looking for an AppleScript (or anything, really) that will do the following:

  • If a Terminal window is open in the current space, bring it to the front.
  • If not, open a new one in the current space.

I don't want it to switch to another space if a Terminal window happens to be open there.

Using iTerm2 instead of Terminal is fine, if that makes things easier :).

I've been looking around for scripts that do this, but the closest I could find is one that opens a new Terminal every time. I don't want that, because I probably have a session that I want to return to.

5
  • I've been working at this for a while, and controlling spaces–with or without GUI scripting, which I don't use since it's inelegant and breaks–is really hard. AppleScript lacks functions needed to properly control spaces. What can be hacked together through shell scripts and writing to/from files can't even do what's needed. Here's what I have so far: tell application "Terminal" if not application "Terminal" is running then activate else if (exists window 1) then activate else do script "" activate end if end if end tell
    – JMY1000
    Commented Mar 18, 2016 at 7:03
  • So basically, once my Terminal windows are already open, it does what I want, except I have to open them myself. That's acceptable for me. If you turn this into a proper answer, I'll give you the bounty!
    – jqno
    Commented Mar 19, 2016 at 10:05
  • Actually, it should open them as appropriate. It doesn't like spaces so much though.
    – JMY1000
    Commented Mar 20, 2016 at 4:58
  • Why not assigning the Terminal to all Spaces? With this You can activate the Terminal without switching Spaces. I've assigned the shortcut to activate the Terminal and if it's already activated hide it ( like ⌘H). With this I have some kind of Visor Window (similar to Total Terminal). Commented Mar 20, 2016 at 10:19
  • @MateuszSzlosek I guess that would help too, but I tend to work on multiple projects simultaneously (one per space) and it's nice to jump directly into the session that belongs to that project/space without having to switch tabs or anything.
    – jqno
    Commented Mar 20, 2016 at 10:54

1 Answer 1

0
+100

I've been working at this for a while, and controlling spaces–with or without GUI scripting, which I don't use since it's inelegant and breaks–is really hard. AppleScript lacks functions needed to properly control spaces. What can be hacked together through shell scripts and writing to/from files can't even do what's needed.

Here's what I could manage:

tell application "Terminal"
    if not application "Terminal" is running then
        activate
    else
        if (exists window 1) then
            activate
        else
            do script ""
            activate
        end if
    end if
end tell

This script does:

  • If a Terminal window is open, bring it to the front.
  • If not, open a new one in the current space.

It does not:

  • Check only within the current space when testing for an open window

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .