【问题标题】:OSX: How can I programatically tell if I'm running in Terminal or iTerm?OSX:我如何以编程方式判断我是在终端还是 iTerm 中运行?
【发布时间】:2016-01-27 06:52:11
【问题描述】:

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

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

【问题讨论】:

    标签: macos terminal applescript iterm


    【解决方案1】:

    $TERM_PROGRAM 环境变量设置为iTerm.app 或Apple_Terminal,因此如果您将其作为参数传递给osascript,您可以确定要运行哪些AppleScript cmds(假设您的shelling到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
    

    【讨论】:

      【解决方案2】:
      osascript -e 'tell application "Finder" to get the name of every process whose visible is true'
      

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

      用列表做你想做的事

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-18
        • 2020-06-18
        • 1970-01-01
        • 2014-07-04
        • 2015-10-18
        • 2014-06-17
        • 1970-01-01
        • 2010-11-09
        相关资源
        最近更新 更多