使用指定的命令(和自定义颜色)以编程方式启动 Terminal.app [英] Programmatically launch Terminal.app with a specified command (and custom colors)

查看:18
本文介绍了使用指定的命令(和自定义颜色)以编程方式启动 Terminal.app的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以像这样从命令行(或程序,通过系统调用)启动一个 xterm:

I can launch an xterm from the command line (or a program, via a system call) like so:

/usr/X11/bin/xterm -fg SkyBlue -bg black -e myscript

这将启动一个带有蓝色文本和黑色背景的 xterm,并在其中运行任意脚本.

That will launch an xterm with blue text and a black background, and run an arbitrary script inside it.

我的问题:如何使用 Terminal.app 执行等效操作?

My question: How do I do the equivalent with Terminal.app?

推荐答案

假设您已经在其中一个终端配置文件中拥有所需的颜色,这就是我想出的(在 Juha 的回答 和来自 这个服务器错误答案).

Assuming you already have the colors you want in one of your Terminal profiles, here's what I came up with (with some help from Juha's answer and from this Serverfault answer).

更新:

回想起来,我认为这个echo 业务太复杂了.事实证明,您可以使用 osascript 制作带有 shebang 行的可执行 AppleScript 文件:

On reflection, I think this echo business is too complicated. It turns out you can use osascript to make an executable AppleScript file with a shebang line:

#!/usr/bin/osascript                                                                            

on run argv                                                                                     
  if length of argv is equal to 0                                                               
    set command to ""                                                                           
  else                                                                                          
    set command to item 1 of argv                                                               
  end if                                                                                        

  if length of argv is greater than 1                                                           
    set profile to item 2 of argv                                                               
    runWithProfile(command, profile)                                                            
  else                                                                                          
    runSimple(command)                                                                          
  end if                                                                                        
end run                                                                                         

on runSimple(command)                                                                           
  tell application "Terminal"                                                                   
    activate                                                                                    
    set newTab to do script(command)                                                            
  end tell                                                                                      
  return newTab                                                                                 
end runSimple                                                                                   

on runWithProfile(command, profile)                                                             
  set newTab to runSimple(command)                                                              
  tell application "Terminal" to set current settings of newTab to (first settings set whose name is profile)                                                                                      
end runWithProfile

将其另存为term.scpt,使用chmod +x 使其可执行,并使用与下面相同的方式,例如term.scpt "emacs -nw" "红沙".

Save that as term.scpt, make it executable with chmod +x, and use it the same way as below, e.g. term.scpt "emacs -nw" "Red Sands".

原答案:

假设我们将下面的脚本保存为 term.sh...

Assuming we save the script below as term.sh...

#!/bin/sh

echo '
on run argv
  if length of argv is equal to 0
    set command to ""
  else
    set command to item 1 of argv
  end if

  if length of argv is greater than 1
    set profile to item 2 of argv
    runWithProfile(command, profile)
  else
    runSimple(command)
  end if
end run

on runSimple(command)
  tell application "Terminal"
    activate
    set newTab to do script(command)
  end tell
  return newTab
end runSimple

on runWithProfile(command, profile)
  set newTab to runSimple(command)
  tell application "Terminal" to set current settings of newTab to (first settings set whose name is profile)
end runWithProfile
' | osascript - "$@" > /dev/null

...它可以被调用如下:

...it can be invoked as follows:

  • term.sh
    • 打开一个新的终端窗口,没什么特别的
    • 打开一个新的终端窗口,执行指定的命令.带参数的命令可以用引号括起来,例如term.sh "emacs -nw" 打开一个新终端并运行(非窗口化)emacs
    • opens a new terminal window, executing the specified command. Commands with arguments can be enclosed in quotes, e.g. term.sh "emacs -nw" to open a new terminal and run (non-windowed) emacs
    • 打开一个新的终端窗口,执行指定的命令,并将其设置为指定的配置文件.名称中带有空格的配置文件可以用引号括起来,例如term.sh "emacs -nw" "Red Sands" 打开一个新终端并使用 Red Sands 配置文件运行(非窗口化)emacs.
    • opens a new terminal window, executing the specified command, and sets it to the specified profile. Profiles with spaces in their names can be enclosed in quotes, e.g. term.sh "emacs -nw" "Red Sands" to open a new terminal and run (non-windowed) emacs with the Red Sands profile.

    如果您使用错误的命令名称调用它,它仍会打开窗口并设置配置文件,但您会在新窗口中收到 bash 的错误消息.

    If you invoke it with a bad command name, it'll still open the window and set the profile, but you'll get bash's error message in the new window.

    如果您使用错误的配置文件名称调用它,该窗口仍将打开并且命令仍将执行,但该窗口将坚持使用默认配置文件,并且您将收到一条错误消息(在您启动它的任何地方发送到 stderr)

    If you invoke it with a bad profile name, the window will still open and the command will still execute but the window will stick with the default profile and you'll get an error message (to stderr wherever you launched it) along the lines of

    525:601:执行错误:终端出错:无法获取名称 =elvis"的设置集 1.索引无效.(-1719)

    525:601: execution error: Terminal got an error: Can’t get settings set 1 whose name = "elvis". Invalid index. (-1719)

    调用有点老套,如果我花时间学习getopt(例如,类似term.sh -p profile -e command会更好,例如,允许您在不调用命令的情况下轻松打开指定配置文件中的新终端).如果有办法用复杂的引用把它搞砸,我也不会感到惊讶.但它适用于大多数目的.

    The invocation is slightly hacky, and could probably be improved if I took the time to learn getopt (e.g., something like term.sh -p profile -e command would be better and would, for instance, allow you to easily open a new terminal in the specified profile without invoking a command). And I also wouldn't be surprised if there are ways to screw it up with complex quoting. But it works for most purposes.

    这篇关于使用指定的命令(和自定义颜色)以编程方式启动 Terminal.app的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆