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

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

问题描述

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

 的/ usr / X11 /斌/ xterm的-fg天蓝-bg黑色-e的MyScript

这将推出带有蓝色的文字和黑色背景一个xterm,并运行里面的任意脚本。

我的问题:我该怎么做,相当于用Terminal.app <​​/ P>

解决方案

假设你已经拥有你在你的终端一个配置文件所需的颜色,这里就是我想出了(从的Juha's回答并从<一个href=\"http://serverfault.com/questions/130436/how-can-i-automatically-change-terminal-colors-when-i-ssh-a-server\">this Serverfault回答)。

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

#!/ bin / sh的呼应
上运行的argv
  如果argv的长度是等于0
    set命令为
  其他
    set命令的argv第1项
  万一  如果argv的长度大于1
    设置配置文件的argv项目2
    runWithProfile(命令,配置文件)
  其他
    runSimple(命令)
  万一
运行结束在runSimple(命令)
  告诉应用程序终端
    启用
    集newTab做脚本(命令)
  告诉结束
  返回newTab
结束runSimple在runWithProfile(命令,配置文件)
  设置newTab为runSimple(命令)
  告诉应用程序终端来设置newTab的当前设置(第一设置中设置其名称为配置文件)
结束runWithProfile
| osascript - $ @&GT;的/ dev / null的

...它可以调用如下:


  • term.sh

    • 打开一个新的终端窗口,没有什么特别


  • term.sh COMMAND

    • 打开一个新的终端窗口,执行指定的命令。带参数的命令可以用引号括起来,例如 term.sh的emacs -nw来打开新的终端并运行(无窗)的Emacs


  • term.sh COMMAND PROFILE

    • 打开一个新的终端窗口,执行指定的命令,并将其设置为指定的配置文件。在其名称中的空间配置文件可以用引号括起来,例如 term.sh的emacs -nw,红沙来打开新的终端并运行(无窗)与红金沙轮廓emacs的。


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

如果你有一个坏的配置文件名称调用它,窗口将仍处于打开状态,命令仍将执行,但窗口将坚持使用默认的配置文件,你会得到一个错误信息(到stderr无论你启动它)沿的线


  

525:601:执行错误:终端得到了一个错误:无法获取设置中设置1的名字=猫王。无效的索引。 (-1719)


调用稍微哈克,如果我花时间来学习也许可以改善 getopt的(例如,像 term.sh - p轮廓-e命令会更好,并会,比如,让你轻松打开指定的配置文件一个新的终端,而不必调用的命令)。而且我还是否有办法处理复杂引用拧起来也不会感到惊讶。但它适用于大多数的目的。

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

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

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

解决方案

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).

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
    • opens a new terminal window, nothing special
  • term.sh COMMAND
    • 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 COMMAND PROFILE
    • 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.

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.

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: execution error: Terminal got an error: Can’t get settings set 1 whose name = "elvis". Invalid index. (-1719)

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天全站免登陆