Search code examples
macosapplescriptosx-mountain-lion

Applescript failing to open app in new window


tell application "Terminal"
    activate
    if windows is {} then reopen
    do script "ssh user@192.168.0.1" in window 1
end tell

How can I tell apple script if there are open windows, to open new window as well, because it can ruin existing one.


Solution

  • If you don't target a window by index it will open a new window every time.

    e.g,

    tell application "Terminal"
        activate
        do script "ssh user@192.168.0.1"
    end tell
    

    or to deal with using existing windows on a fresh open.

    tell application "System Events"
        if (count (processes whose bundle identifier is "com.apple.Terminal")) is 0 then
            tell application "Terminal"
                activate
                do script "ssh user@192.168.0.1" in window 0
            end tell
        else
            tell application "Terminal"
                do script "ssh user@192.168.0.1"
                activate
            end tell
        end if
    end tell
    

    more ideas here: http://hintsforums.macworld.com/archive/index.php/t-68252.html