为 运行 一个 bash 命令创建新标签的 Apple 脚本

Apple Script to create a new tab to run a bash command

我正在尝试使用 Apple Script 编写函数来创建选项卡和 运行 命令。


我试过了

tell application "Terminal"
    activate
    
    my makeTab("desktop", "ls")
    
end tell

on makeTab(name, command)
    
    do shell script command
    
    tell application "System Events"
        keystroke "t" using {command down}
        delay 0.2
        keystroke "i" using {shift down, command down}
        keystroke tab
        keystroke name
        key code 53
        
    end tell
end makeTab

结果

我不断得到

sh: eCmd: command no found

关于如何让它发挥作用的任何提示?

终端冲突,namecommand是AppleScript的保留术语。

您不能将这些术语用作处理程序的参数。


检查这些变量的颜色。

  • 变量的颜色必须是绿色
  • 紫色 : 它是一个 属性 或一个常量
  • Blue : 这是一个命令或 class

所以,更改这些变量的名称。

如果你想运行新标签页中的命令,使用do script命令,而不是do shell script,像这样:

tell application "Terminal"
    activate
    my makeTab("desktop", "ls")
end tell

on makeTab(tName, tCommand)
    tell application "System Events"
        keystroke "t" using {command down}
        delay 0.2
        keystroke "i" using {shift down, command down}
        keystroke tab
        keystroke tName
        key code 53
    end tell
    tell application "Terminal"
        do script tCommand in front window -- run the command in the new tab
    end tell
end makeTab