How to get window id and tab number of terminal window using AppleScript via ScriptingBridge?

I can open the Terminal tab using the following AppleScript:

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

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

In Cocoa ScriptingBridge, I can do:

SBApplication  *terminal;
SBObject       *tab;

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

How to get window id and tab number from tab object?


Edit 2009/4/27 - Why?

In answer to the question why I want to do this, I open the command in the terminal window (as indicated above) and return the tab object. However, I want to move / resize this window, so I need to access the object of the "window" tab.

Objective-C (, , Objective-C, Perl) , , NSAppleScript ScriptingBridge ( perl applescript 64- ). NSAppleScript, .

- TTY ( ) , , . , ( , !).


2009/4/30 -

has , NSAppleEventDescriptor API. NSAppleScript executeAndReturnError(). , NSAppleScript , ScriptingBridge.

ClassDump, SBObject, specifierDescription() qualifiedSpecifier(). "tab X of window id Y". Apple, .

( perl):

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";
+5
3

; - ?

(, , API Apple Event Manager objc-appscript, AEDesc/NSAppleEventDescriptor, . SB, , API AEDesc, emptor , , , .)

+1

, , , . :

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 id
    repeat with i from 1 to the count of theWindow tabs
        if item i of theWindow tabs is newTab then set tabNumber to i
    end repeat
    get {windowId, tabNumber}
end tell
+2

- :

""   set new_win script ""    w_id id end tell

0
source

All Articles