Hi everyone - first post in this forum; first attempt at AS; please be kind
I thought rather than mix in Apple Script and pseudocode together I’d just post the pseudocode. From reading these forums I’ve seen how to tell Terminal to run a script, so it’s more the case of opening new tabs and checking for focus.
I want to perform the following (pseudocode) with Terminal - can anyone convert for me? :
The idea is that I’ll click an icon and it opens Terminal up, opens 4 tabs (in total) and in each tab performs an ssh to SERVER_NAME (and opens a screen session). Ideally, I’d like to test if the window already exists and if it does, give it focus. This will prevent the screen sessions from being “dropped” in the original window and opening again in the new window, if it exists. I’d also like to attach the scripts to individual icons, so I can click on hotrod, prime, soundwave or any other cool Transformers icon that I have in my dock to represent our servers.
OK, well I managed to hack together something which does exactly what I want, but it doesn’t “feel” like I’m doing it the cleanest way. I’m emulating menu selections to create the tabs, which works, but might not on your setup. Also, the loop repeats for all windows, even if you find the target window in the first pass. I don’t know how to bail out of loops in AS
Anyway, here’s my code:
-- `menu_click`, by Jacob Rus, September 2006
--
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item. In this case, assuming the Finder
-- is the active application, arranging the frontmost folder by date.
on menu_click(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click
on menu_click_recurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menu_click_recurse
-- Matthew Lambie, November 2007
-- http://lambie.org
-- Opens a Terminal with four tabs that then each reconnect to named screen sessions
-- or if that window is already opened, reapply focus
global window_id
global window_name
set window_id to 0
set window_name to ""
set user to "you_username"
set server to "your_server"
set user_at to user & "@" & server
-- MAINLINE
tell application "Terminal"
activate
-- get every window id
set w_ids to (id of every window)
-- with each window id...
repeat with w_id in w_ids
-- have we found our target window_id?
if window_id is equal to 0 then
-- load this window's name
set w_name to name of window id w_id
-- is this the window we're looking for?
if (texts 1 thru (count user_at) of w_name) is equal to user_at then
set window_id to w_id
set window_name to name of window id window_id
end if
end if
end repeat
-- if we have a window_id then we can give that window
-- the focus, otherwise we need to make a new window
-- with four tabs and execute the ssh/screen command in
-- each of those four tab
if (window_id is not equal to 0) then
-- give that window the focus
set frontmost of window id window_id to true
else
-- make a new window with the execution of a trivial command
do script "clear"
-- load up the window id of the window we just created
set window_id to id of first window whose frontmost is true
-- make tabs 2, 3 and 4
repeat with i from 1 to 3
my menu_click({"Terminal", "Shell", "New Tab", "Pro"})
end repeat
-- for each of the four tabs we've now made
repeat with i from 1 to 4
-- build the command, then execute it
set cmd to "clear ; ssh -t " & server & " 'screen -d -R tfg_" & i & "'"
do script cmd in tab i of window id window_id of application "Terminal"
end repeat
end if
end tell
Hopefully this will help other people out in the future.