How do I get the window id and tab number of a Terminal window using AppleScript via the ScriptingBridge?
Asked Answered
A

4

6

I can open a Terminal tab using the following AppleScript:

tell application "Terminal"
    set myTab to do script "exec sleep 1"
    get myTab
end tell

This returns a string like: tab 1 of window id 3263 of application "Terminal". This is great, I can see the window id 3263 and tab number 1 (although I don't know how to query myTab to get only these values).

In the Cocoa ScriptingBridge, I can do:

SBApplication  *terminal;
SBObject       *tab;

terminal = [SBApplication applicationWithBundleIdentifier:@"com.apple.terminal"]
tab = [terminal doScript:@"exec sleep 1" in:nil]

How do I get the window id and tab number from the tab object?


Edit 2009/4/27 - Why?

In answer to why I want to do this - I am opening a command in a Terminal window (as above), and am getting back the tab object. However I want to move/resize this window, so I need to get access to the tab's "window" object.

I am using Objective-C (well actually, Objective-C bridged from Perl), and want to stick to standard OS components, so I believe I only have the NSAppleScript and ScriptingBridge frameworks to play with (all the perl applescript modules broke with 64bit carbon removal). I would try NSAppleScript, but processing the returned values seems to be a black-art.

My current solution is to get the TTY of the tab object (guaranteed unique) and enumerate every tab of every window until I find the window containing the tab. I assumed that this couldn't be the best way (it sure ain't fast!).


Edit 2009/4/30 - Solution

Based on the suggestions of "has" below, I braved the NSAppleEventDescriptor API. Initially, I was only able to get to this with NSAppleScript's executeAndReturnError() call. However I found that NSAppleScript was much, much slower than ScriptingBridge.

After using ClassDump to extract some more SBObject calls, I found the undocumented specifierDescription() and qualifiedSpecifier() calls. The former gives me the nice "tab X of window id Y" string. The latter returns the apple event descriptor, which I can then decode.

My final code (in perl) is:

use Foundation;

NSBundle->bundleWithPath_('/System/Library/Frameworks/ScriptingBridge.framework')->load;

# Create an OSType (bid endian long) from a string
sub OSType ($) { return unpack('N', $_[0]) }

my $terminal = SBApplication->applicationWithBundleIdentifier_("com.apple.terminal");

my $tab         = $terminal->doScript_in_("exec sleep 1", undef);
my $tab_ev_desc = $tab->qualifiedSpecifier;
my $tab_id      = $tab_ev_desc->descriptorForKeyword_(OSType 'seld')->int32Value;
my $win_ev_desc = $tab_ev_desc->descriptorForKeyword_(OSType 'from');
my $window_id   = $win_ev_desc->descriptorForKeyword_(OSType 'seld')->int32Value;

print "Window:$window_id Tab:$tab_id\n";
Aile answered 26/4, 2010 at 10:26 Comment(0)
I
1

Technically you can't; a better question is why do want to?

(Well, okay, you could if you use the Apple Event Manager API or objc-appscript, both of which can give you a raw AEDesc/NSAppleEventDescriptor which you can recursively pull apart yourself. Or you might poke around in SB to see if there's an undocumented API to get at the underlying AEDesc, but caveat emptor, of course. Alternatively, there may be a better way to achieve your actual goal without resorting to hackery, but you'd need to provide more information.)

Incoordination answered 26/4, 2010 at 16:57 Comment(3)
Thanks for the comment. I updated the question to add the "why" part. I suspected I might end up messing with the AppleEvents. Is this possible in NSAppleScript? I am trying to work with only things that a re preinstalled on the machine.Aile
Yeah, should've thought of NSAppleScript too. If you run your 'do script' command in AppleScript using -[NSAppleScript executeAppleEvent:error:] (assuming you want to pass in parameters as well) and return the reference, you'll get an NSAppleEventDescriptor containing your object specifier. Bit messy, but if you're not in [Obj-]C and can't afford external dependencies then it's really your only option. As for packing and unpacking AEDescs, it's not too hard once you know how they're structured. e.g. Take a look at objc-appscript's AEMCodecs class for tips.Incoordination
Thanks - it appears that the NSAppleEventDescriptor is the way to go. Since I didn't like the performance of NSAppleScript, I found an undocumented ScriptingBridge call that returned the descriptor. I added my code to the end of the question.Aile
T
4

I know this is an old question, but I just had this problem today, and I found no good answers online. This worked for me:

tell application "Terminal"
    set newTab to do script "echo hello"
    set theWindow to first window of (every window whose tabs contains newTab)
    set windowId to theWindow's id
    repeat with i from 1 to the count of theWindow's tabs
        if item i of theWindow's tabs is newTab then set tabNumber to i
    end repeat
    get {windowId, tabNumber}
end tell
Tsuda answered 17/7, 2015 at 18:59 Comment(0)
I
1

Technically you can't; a better question is why do want to?

(Well, okay, you could if you use the Apple Event Manager API or objc-appscript, both of which can give you a raw AEDesc/NSAppleEventDescriptor which you can recursively pull apart yourself. Or you might poke around in SB to see if there's an undocumented API to get at the underlying AEDesc, but caveat emptor, of course. Alternatively, there may be a better way to achieve your actual goal without resorting to hackery, but you'd need to provide more information.)

Incoordination answered 26/4, 2010 at 16:57 Comment(3)
Thanks for the comment. I updated the question to add the "why" part. I suspected I might end up messing with the AppleEvents. Is this possible in NSAppleScript? I am trying to work with only things that a re preinstalled on the machine.Aile
Yeah, should've thought of NSAppleScript too. If you run your 'do script' command in AppleScript using -[NSAppleScript executeAppleEvent:error:] (assuming you want to pass in parameters as well) and return the reference, you'll get an NSAppleEventDescriptor containing your object specifier. Bit messy, but if you're not in [Obj-]C and can't afford external dependencies then it's really your only option. As for packing and unpacking AEDescs, it's not too hard once you know how they're structured. e.g. Take a look at objc-appscript's AEMCodecs class for tips.Incoordination
Thanks - it appears that the NSAppleEventDescriptor is the way to go. Since I didn't like the performance of NSAppleScript, I found an undocumented ScriptingBridge call that returned the descriptor. I added my code to the end of the question.Aile
R
1

I'm using the following trick:

on getWindow(tab_)
    tell application "Terminal"
        repeat with i in (get windows)
            if tty of tabs of i contains tty of tab_ then return i
        end repeat
    end tell
end getWindow

Every tab should have a unique TTY device associated with it, so the window containing the referenced tab can be identified.

Rashad answered 8/5, 2024 at 11:39 Comment(1)
Nice. You might consider adding contents of when you return i. Then you get only the window specifier.Dispersive
T
0

How about something very simple like this:

tell application "Terminal"
    set new_win to do script ""
    set w_id to id of front window
end tell
Thoroughbred answered 14/3, 2012 at 17:52 Comment(1)
Yes, I used that for a while, but there is a race condition if the user switched windows between the "do script" and the "set id", which happens more than you would initially expect.Aile

© 2022 - 2025 — McMap. All rights reserved.