Jak*_*ter 5 macos terminal automator
我想要做的是运行一个自动化脚本。发生的情况是它打开带有两个选项卡的终端,每个选项卡和 ssh 到 root@192.168.0.1 和 root@ssh@192.168.0.2;你会怎么做?
您可以使用运行 AppleScript操作来运行如下脚本:
on run {input, parameters}
tell application "Terminal"
activate
do script "ssh root@192.168.0.1"
do script "ssh root@192.168.0.2"
end tell
return input
end run
Run Code Online (Sandbox Code Playgroud)
终端的do script命令创建一个新的终端窗口并将给定的命令字符串发送到 shell。请注意,如果您想向同一个终端发送其他命令,请将do script命令的结果存储在一个变量中——它将是对创建的终端的引用,您可以将它与命令的in参数一起使用do script以发送更多命令到那个终端。
为了补充克里斯·佩奇的有用答案:
\n\n如果您希望两个终端选项卡位于同一个窗口中,事情就会变得棘手:终端 AppleScript API 的一个长期限制是无法以编程方式在现有窗口中创建新选项卡。
\n\n您可以使用 GUI 脚本来解决该问题;虽然以下处理程序makeNewTab()相当强大,但它需要事先进行一次性授权才能进行辅助访问 - 请参阅下面对处理程序的评论。
请注意,授权通用执行环境(例如 Terminal.app 和 Automator.app)进行辅助访问意味着它们运行的任何脚本都将拥有这些权限。
\n\n如果您想要对选项卡创建过程进行更多控制,例如分配特定配置文件(外观和行为设置)的能力,请参阅我的答案,以及该答案底部的应用程序。
\n\n(*\n Creates a new tab in Terminal\'s front window and optionally executes a shell command,\n if <shellCmdToRun> is a nonempty string.\n\n Note:\n * This handler effectively clicks the menu item that creates a new tab and\n therefore requires assistive access:\n The application running this handler - e.g., Terminal.app, Script Editor.app, \n or Automator.app - must be added to the list at\n System Preferences > Security & Privacy > Privacy > Accessibility,\n using admin credentials.\n * This handler activates Terminal first, which is required for it to work.\n\n Caveat: \n If there\'s no front window or if all windows are currently minimized, the\n tab is created in a *new* window.\n\n Examples:\n my makeNewTab("") # open new tab (without executing a command)\n my makeNewTab("ls") # open new tab and execute shell command `ls`\n*)\non makeNewTab(shellCmdToRun)\n tell application "Terminal"\n\n # Note: If Terminal is not frontmost, clicking the new-tab menu item invariably \n # creates the tab in a *new* window.\n activate\n\n # Find the File menu by position and click the menu item whose keyboard shortcut is\n # \xe2\x8c\x98T - this should work with any display language.\n tell application "System Events" to \xc2\xac\n tell menu 1 of menu item 2 of menu 1 of menu bar item 3 of menu bar 1 \xc2\xac\n of application process "Terminal" to click (the first menu item \xc2\xac\n whose value of attribute "AXMenuItemCmdChar" is "T" and \xc2\xac\n value of attribute "AXMenuItemCmdModifiers" is 0)\n\n # If specified, run a shell command in the new tab.\n if shellCmdToRun \xe2\x89\xa0 missing value and shellCmdToRun \xe2\x89\xa0 "" then\n do script shellCmdToRun as text in selected tab of front window\n end if\n\n end tell\nend makeNewTab\nRun Code Online (Sandbox Code Playgroud)\n\n如果您愿意安装我的ttabCLI,您可以完全不用 AppleScript,而是从Run Shell ScriptAutomator 操作中运行以下命令:
# Create tab in new window (-w) and run specified command.\nttab -w ssh root@192.168.0.1 \n\n# Create additional tab in same window, with specific settings.\nttab -s Grass ssh root@192.168.0.2 \nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
13903 次 |
| 最近记录: |