Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have been working on this "dashboard" to help control my localhost server environment. It has been scripted in bin/bash for reference. I recently discovered I can use the bin command "osascript" to execute an "AppleScript"-esque command through the Terminal, and am playing around with that to incorporate into my dashboard. This way, I can execute commands and control my localhost server through one window, while in the background it can open separate tasks without obstructing the main window.

Here is my main question revolving this concept, I noticed that upon the execution of an osascript command, it returns the following information: tab 1 of window id 11148.

This is the bash statement from my .sh script:

osascript -e "tell application \"Terminal\" to do script \"cd $devFolder;svn up\""

And this is the output I can see in my terminal:

tab 1 of window id 11197

Originally, I had wanted to have it just open a new tab, but i'm fine with it opening a window as for the purpose of just having it work. So as my question states, is it possible to somehow store or retrieve that window id of the terminal window that was just created? Allowing me to interact with that specific window and control it?

share|improve this question

From AppleScript Editor:

tell application "Terminal"
    set windowInfo to do script "echo \"hi user273298\""
    do script "echo \"hi adayzdone\"" in windowInfo
end tell

EDIT

osascript -e '
tell application "Terminal"
    set windowInfo to do script "echo \"hi user273298\""
    do script "echo \"hi adayzdone\"" in windowInfo
end tell
'
share|improve this answer
    
Not exactly sure how this is supposed to store the output text that was returned when that osascript command was executed. I see that "windowInfo" variable, but I can't access it outside that command. – ManBearPixel Nov 12 '13 at 16:31

Add the tab and window id as an in specifier:

tab=$(osascript -e 'tell app "Terminal" to do script "uptime"');osascript -e 'tell app "Terminal" to do script "uptime" in '"$tab"

You can also use in window 1 to run a command in an existing window:

osascript -e 'tell app "Terminal" to do script "uptime"';osascript -e 'tell app "Terminal" to do script "uptime" in window 1'

share|improve this answer

Given your question I'm betting that you may want, in your bash script, to run osascript more than once - hoping to save the terminal's ID and reuse it ?!? I had trouble making this work without fail initially. Window #s change - tab #s change - with the creation/deletion of these. Associated ID's (created by Terminal.app) can come and go as well - afterall - they are ASSOCIATED with windows/tabs being created/deleted - and there's little definition on what that ID actually represents to Terminal.app long-term nevermind whether that definition/meaning should be counted upon within your bash script/environment itself.

I want an iron-clad method to work on "that tab in that window"; one that would not fail to identify said window/tab - from the UNIX perspective - afterall my base environment is bash (Unix), not Terminal.app !!!!!. And lastly, I want to be able to operate on more than one said window/tab from a single script. Here's how I do it. I discovered a quote-\ID/-unquote that appears to work as advertised - ALL THE TIME. Why/How/Where? The trick is NOT to count upon any data that the Terminal.app/osascript creates regarding the terminal; but rather, count upon what it GATHERS from the basic Unix system about the terminals being represented. Each "terminal" has a unique tty (as in /dev/ttyXXXX) - this is the key; or at least, the basis of one as long as you properly keep track of windows/tabs comings-and-goings (think associated Unix ProcessIDs). Terminal.app stores the output of the unix tty command in the "window->tab->tty" object that osascript can access. Read that last sentence again ...

So, say, you want to change the background color of a particular tab in a particular window e.g. the one in which your bash script is calling upon osascript to do the work... First get the output of "tty" into a bash variable [MyTTY=$(tty)]. Then reference "${MyTTY}" in your osascript. The script should loop through every tab in every window (that's a loop within a loop) until if finds that tab/tty object whose value matches the contents of "${MyTTY}". With a bit more trickery you optimize the oascript to exit the search loops after having found its target and done the real work you want done on the terminal in question. Why? Well, no two terminals under Terminal.app's control share the same tty !!!! So, why not stop the loops rather than looping through the rest of the terminals to no real avail. ---- Back to my beginning presumption. You can call upon osascript many times throughout your bash script using the basic loop structure described above to find the target terminal. The only thing that changes from incarnation to incarnation are those lines of code performing the real work you want done within each incarnation.

Think of it: one bash script that creates one or more windows each having one or more tabs and directs all those window/tabs to run their own osascripts. Each window/tab can report its /dev/tty "key" back to the master using any number of well known unix tools which we all love to love and hate. Good luck !!!!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.