OSX:我如何编程告诉我是否在终端或iTerm中运行?

时间:2016-01-27 06:52:12

标签: macos terminal applescript iterm

我在OSX上运行了一个命令行应用程序,希望使用AppleScript在当前窗口中创建多个选项卡。

如何判断我的程序是在Terminal,iTerm还是其他终端程序中运行?

2 个答案:

答案 0 :(得分:2)

$TERM_PROGRAM env var设置为iTerm.appApple_Terminal,因此您可以确定将其作为arg传递给osascript时要运行的AppleScript cmds(假设你的炮轰osascript

用法示例:

osascript ThisScriptName.scpt $TERM_PROGRAM

示例脚本:

--osascript ThisScriptName.scpt $TERM_PROGRAM
on run {TermType}
    if (TermType = "iTerm.app") then
        -- iTerm.app :-P
        tell application "iTerm"
            tell current window
                tell current session
                    set newSession to (split horizontally with default profile)
                end tell
            end tell
        end tell
    else if (TermType = "Apple_Terminal") then
        -- Terminal.app
        tell application "Terminal"
            do script "open 'https://iterm2.com/downloads.html'" in window 1
        end tell
    else
        -- Unknown terminal application
        return "Really? Not using iTerm.app or Terminal.app"
    end if
end run

答案 1 :(得分:0)

osascript -e 'tell application "Finder" to get the name of every process whose visible is true'

这将是一个正在运行的应用程序列表,假设您只运行终端,iTerm,...,这将工作

使用列表执行您想要的操作

希望这有帮助