使用指定的命令(和自定义颜色)以编程方式启动Terminal.app

dre*_*ves 27 macos terminal command-line applescript

我可以从命令行(或程序,通过系统调用)启动xterm,如下所示:

/usr/X11/bin/xterm -fg SkyBlue -bg black -e myscript
Run Code Online (Sandbox Code Playgroud)

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

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

Sam*_*ane 22

您也可以通过捆绑包ID打开应用程序,并提供其他参数.

如果当前目录中有可执行脚本test.sh,则会打开以下命令并在Terminal.app中运行它

 open -b com.apple.terminal test.sh 
Run Code Online (Sandbox Code Playgroud)

我能找到的唯一缺点是终端似乎没有继承你当前的环境,因此你必须安排另一种方法将参数传递给你想要运行的脚本.我想在运行中构建脚本以嵌入参数将是一种方法(考虑到当然的安全隐患......)

  • 这并不直接解决颜色问题,但它是在终端窗口中启动现有shell脚本的更好方法. (2认同)

Juh*_*uha 13

几乎所有(每个?)osx程序都可以使用以下命令从命令行启动:

appName.app/Contents/MacOS/command

对于终端,命令是:

/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal

您可以使用自动完成(选项卡)或ls来查找正确的文件名.".app"基本上是一个文件夹.

要更改颜色并运行脚本...我认为你无法使用shell脚本,因为终端不接受参数("终端myScript.sh"不启动myScript).有了iTerm,这就行了.

解决方法是使用applescript(包装在shell脚本中):

   #!/bin/sh
   osascript -e '
     tell application "Terminal"
       activate
       tell window 1
          do script "sleep 5; exit"
          set background color to {0, 11111, 11111}
          set win_id to id
       end tell

       set w_ids to (id of every window)

       repeat while w_ids contains win_id
         delay 1
         set w_ids to (id of every window)
       end repeat
    end tell'
Run Code Online (Sandbox Code Playgroud)

好的,现在它应该与xterm示例完全相同.缺点是窗口ID的持续轮询(这是糟糕的编程).

编辑:更优雅的AppleScript会使用Terminal的'busy'属性.我会保留原始代码,因为它适用于一般程序(不仅仅是终端).

tell application "Terminal"
    tell window 1
        do script "sleep 2"
        set background color to {0, 11111, 11111}
        repeat while busy
            delay 1
        end repeat
        close
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

另外,对于完全正确的程序,应检查终端是否正在运行.它会影响打开的窗口数量.所以,这应该首先运行(再次是一个讨厌的黑客,我将在稍后编辑,因为我找到一个有效的解决方案).

tell application "System Events"
    if (count (processes whose name is "Terminal")) is 0 then
        tell application "Terminal"
            tell window 1
                close
            end tell
        end tell
    end if
end tell
Run Code Online (Sandbox Code Playgroud)

br,
Juha


Dav*_*les 13

假设你已经在你的一个终端配置文件中拥有了你想要的颜色,这就是我想出的结果(在Juha的回答Serverfault回答的帮助下).


更新:

经过反思,我认为这项echo业务太复杂了.事实证明,您可以使用osascriptshebang行制作可执行的AppleScript文件:

#!/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
Run Code Online (Sandbox Code Playgroud)

保存为term.scpt,使其可执行chmod +x,并以与下面相同的方式使用它,例如term.scpt "emacs -nw" "Red Sands".


原始答案:

假设我们将下面的脚本保存为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
Run Code Online (Sandbox Code Playgroud)

...可以按如下方式调用:

  • term.sh
    • 打开一个新的终端窗口,没什么特别的
  • term.sh COMMAND
    • 打开一个新的终端窗口,执行指定的命令.带参数的命令可以用引号括起来,例如term.sh "emacs -nw"打开一个新终端并运行(非窗口化)emacs
  • term.sh COMMAND PROFILE
    • 打开一个新的终端窗口,执行指定的命令,并将其设置为指定的配置文件.其名称中带有空格的配置文件可以用引号括起来,例如term.sh "emacs -nw" "Red Sands"打开一个新终端并使用Red Sands配置文件运行(非窗口化)emacs.

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

如果您使用错误的配置文件名称调用它,该窗口仍将打开,命令仍将执行,但窗口将保持默认配置文件,您将收到一条错误消息(对于stderr,无论您何时启动它)沿着

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

如果我花时间学习getopt(例如,term.sh -p profile -e command更好的东西,例如,允许您在不调用命令的情况下轻松地在指定的配置文件中打开新终端),则调用稍微有点hacky,并且可能会有所改进.如果有办法搞砸复杂的引用,我也不会感到惊讶.但它适用于大多数用途.