我可以使用以下 AppleScript 打开“终端”选项卡:
tell application "Terminal"
set myTab to do script "exec sleep 1"
get myTab
end tell
这会返回一个字符串,例如:应用程序“Terminal”窗口 ID 3263 的选项卡 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(实际上是从 Perl 桥接的 Objective-C),并且想要坚持使用标准操作系统组件,所以我相信我只有 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";
最佳答案
我知道这是一个老问题,但我今天才遇到这个问题,并且在网上找不到好的答案。这对我有用:
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
关于cocoa - 如何通过 ScriptingBridge 使用 AppleScript 获取终端窗口的窗口 ID 和选项卡号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2712655/