通过OSX El Capitan上的Applescript获取窗口中"Terminal"选项卡数量时出错



基本上,当我打开新窗口时,我想改变bash的主题,而不是新的选项卡,然后让一个窗口的选项卡共享相同的主题;而单独窗口的主题是随机确定的。

经过一番挖掘,我找到了一个设置终端窗口当前选项卡主题的应用程序说明。我创建了一个

/usr/local/terminal color.scpt为:

on run argv
    tell application "Terminal" to set current settings of selected tab of front window to some settings set
end run

我在我的bash配置文件中添加了以下声明:

osascript /usr/local/terminal-color.scpt

现在,这个脚本当然会与bash的每个新实例一起运行。我无法从bash_profile对此做任何事情。但是,我应该能够将新窗口或新选项卡与appdescription本身区分开来。因此,我正在寻找一个if语句,它只允许脚本在创建新窗口时运行。类似于:

on run argv
    if index of selected tab is 0
        tell application "Terminal" ....
    end if
end run

但是,通过查看终端应用程序的appescription文档和脚本字典,我不知道如何实现这一点。请帮助

更新

我尝试按如下方式编辑脚本:

set tabNum to number of tabs of front window
if tabNum = 1 then
    tell app ...

如果出现错误tabs of window 1 doesn’t understand the “count” message

,这也不会起作用

我的方法是正确的,但我有一个简单的错误,即在选择应用程序范围之前尝试获取选项卡或窗口数据。简单地说,首先tell应用程序,然后询问其属性。以下是有效的代码:

on run argv
    tell application "Terminal"
        if number of tabs of front window = 1 then
            set current settings of selected tab of front window to some settings set
        end if
    end tell
end run

甚至更好

改进以前的脚本;这不是随机选择一个主题,而是根据您已经打开的窗口在可用的终端主题中进行迭代。您还可以设置您的默认主题,该主题将在您首次启动终端时设置。就我而言,这是settings set中的第5个。代码如下:

tell application "Terminal"
    if number of tabs in front window = 1 then
        set defaultThemeOffset to 5
        set allThemes to number of settings set
        set allWindows to number of windows
        set themeIndex to (defaultThemeOffset + allWindows) mod allThemes
        set current settings of selected tab of front window to settings set themeIndex
    end if
end tell

最新更新