如何通过 ScriptingBridge 使用 AppleScript 获取终端窗口的窗口 ID 和选项卡号?

发布于 2024-08-30 06:05:39 字数 2574 浏览 3 评论 0原文

我可以使用以下 AppleScript 打开“终端”选项卡:

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

这会返回一个字符串,例如:应用程序“终端”窗口 id 3263 的 tab 1。这太棒了,我可以看到窗口 ID 3263 和选项卡编号 1 (尽管我不知道如何查询 myTab 来仅获取这些值)。

在 Cocoa ScriptingBridge 中,我可以执行以下操作:

SBApplication  *terminal;
SBObject       *tab;

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

如何从选项卡对象获取窗口 id 和选项卡编号?


编辑 2009/4/27 - 为什么?

回答为什么我想这样做 - 我在终端窗口中打开一个命令(如上所述),并返回 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”字符串。后者返回苹果事件描述符,然后我可以对其进行解码。

我的最终代码(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";

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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪冰清 2024-09-06 06:05:39

我知道这是一个老问题,但我今天才遇到这个问题,并且在网上找不到好的答案。这对我有用:

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

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
听风念你 2024-09-06 06:05:39

从技术上讲你不能;更好的问题是为什么要这样做?

(好吧,如果您使用 Apple Event Manager API 或 objc-appscript,则可以,两者都可以给你一个原始的 AEDesc/NSAppleEventDescriptor ,你可以递归地 自己拆开。或者你可以在 SB 中闲逛,看看是否有一个未记录的 API 可以获取底层 AEDesc,但当然要买者自负。或者,可能有更好的方法来实现你的实际目标,无需诉诸黑客手段,但您需要提供更多信息。)

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.)

满身野味 2024-09-06 06:05:39

我使用以下技巧:

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

每个选项卡都应该有一个与其关联的唯一 TTY 设备,以便可以识别包含引用选项卡的窗口。

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.

记忆で 2024-09-06 06:05:39

像这样非常简单的事情怎么样:

tell application "Terminal"
    set new_win to do script ""
    set w_id to id of front window
end tell

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文