如何使用Sciptingbridge使用AppleScript获取终端窗口的窗口ID和选项卡号?
-
01-10-2019 - |
题
我可以使用以下AppleScript打开终端选项卡:
tell application "Terminal"
set myTab to do script "exec sleep 1"
get myTab
end tell
这返回一个字符串,例如: tab 1 of window id 3263 of application "Terminal"
. 。太好了,我可以看到窗口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桥接的Objectives-C),并且想坚持使用标准的操作系统组件,因此我相信我只有NSApplescript和Scriptingbridge框架可以使用(所有Perl Applescript模块都与64位破裂去除碳)。我会尝试nsapplescript,但是处理返回的值似乎是黑色艺术品。
我目前的解决方案是获得 TTY 标签对象(保证唯一的)并列举每个窗口的每个选项卡,直到找到包含标签的窗口为止。我认为这不是最好的方法(肯定不会快!)。
编辑2009/4/30-解决方案
基于“拥有“下面,我勇敢地 nsappleeventdescriptor API。最初,我只能与Nsapplescript的 executeAndReturnError()
称呼。但是我发现Nsapplescript比Scriptingbridge慢得多。
使用后 classDump 要提取更多的sbobject调用,我发现了未记录的 specifierDescription()
和 qualifiedSpecifier()
呼叫。前者给了我不错的窗口的选项卡x 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";
解决方案
从技术上讲,你不能;一个更好的问题是为什么要?
(好吧,好吧,如果您使用Apple Event Manager API或 objc-appscript, ,这两者都可以为您提供一个RAW AEDESC/NSAPPLEEVENTDESCRIPTOR,您可以递归 拉开自己. 。或者,您可能会在SB中戳戳,以查看是否有无证件的API可以到达基础的AEDESC,但当然要警告。另外,可能有一种更好的方法来实现您的实际目标而无需求助于黑客,但是您需要提供更多信息。)
其他提示
我知道这是一个古老的问题,但是我今天只是遇到了这个问题,我在网上没有找到好的答案。这对我有用:
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
那样简单的事情怎么样?
告诉应用程序“终端”设置new_win进行执行脚本”“将w_id设置为前窗口端的ID