【问题标题】:Error getting the number of tabs in "Terminal" window via Applescript on OSX El Capitan通过 OSX El Capitan 上的 Applescript 获取“终端”窗口中的选项卡数量时出错
【发布时间】:2016-04-08 18:20:01
【问题描述】:

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

经过一番挖掘,我发现了一个applescript,它设置了终端窗口当前选项卡的主题。我创建了一个

/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 对此做任何事情。但是,我应该能够将新窗口或新选项卡与 applescript 本身区分开来。因此,我正在寻找一个 if 语句,它可以让脚本仅在创建新窗口时运行。比如:

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

但是查看终端应用程序的 applescript 文档和脚本字典,我无法弄清楚如何实现这一点。请帮忙

更新

我尝试如下编辑脚本:

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

【问题讨论】:

    标签: bash terminal applescript osx-elcapitan


    【解决方案1】:

    我的方法是正确的,但我犯了一个简单的错误,即在选择应用程序范围之前尝试获取选项卡或窗口数据。简单来说,先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 中的第五个。代码如下:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多