|
| 1 | +#!/usr/bin/osascript |
| 2 | + |
| 3 | +# Dependency: requires iTerm (https://iterm2.com) |
| 4 | +# Install via Homebrew: `brew install --cask iterm2` |
| 5 | + |
| 6 | +# Required parameters: |
| 7 | +# @raycast.schemaVersion 1 |
| 8 | +# @raycast.title Run Shell Command |
| 9 | +# @raycast.mode silent |
| 10 | +# @raycast.packageName iTerm |
| 11 | + |
| 12 | +# Optional parameters: |
| 13 | +# @raycast.icon images/iterm.png |
| 14 | +# @raycast.argument1 { "type": "text", "placeholder": "shell command" } |
| 15 | + |
| 16 | +# Documentation |
| 17 | +# @raycast.author Andrei Borisov |
| 18 | +# @raycast.authorURL https://github.com/andreiborisov |
| 19 | + |
| 20 | +-- Set this property to true to open in a new window instead of a new tab |
| 21 | +property open_in_new_window : false |
| 22 | + |
| 23 | +-- Handlers |
| 24 | +on new_window() |
| 25 | + tell application "iTerm" to create window with default profile |
| 26 | +end new_window |
| 27 | + |
| 28 | +on new_tab() |
| 29 | + tell application "iTerm" to tell the first window to create tab with default profile |
| 30 | +end new_tab |
| 31 | + |
| 32 | +on call_forward() |
| 33 | + tell application "iTerm" to activate |
| 34 | +end call_forward |
| 35 | + |
| 36 | +on is_running() |
| 37 | + application "iTerm" is running |
| 38 | +end is_running |
| 39 | + |
| 40 | +on is_processing() |
| 41 | + tell application "iTerm" to tell the first window to tell current session to get is processing |
| 42 | +end is_processing |
| 43 | + |
| 44 | +on has_windows() |
| 45 | + if not is_running() then return false |
| 46 | + if windows of application "iTerm" is {} then return false |
| 47 | + true |
| 48 | +end has_windows |
| 49 | + |
| 50 | +on send_text(custom_text) |
| 51 | + tell application "iTerm" to tell the first window to tell current session to write text custom_text |
| 52 | +end send_text |
| 53 | + |
| 54 | +-- Main |
| 55 | +on run argv |
| 56 | + if has_windows() then |
| 57 | + -- Open the command in the current session unless it has a running command, e.g., ssh or top |
| 58 | + if is_processing() then |
| 59 | + if open_in_new_window then |
| 60 | + new_window() |
| 61 | + else |
| 62 | + new_tab() |
| 63 | + end if |
| 64 | + end if |
| 65 | + else |
| 66 | + -- If iTerm is not running and we tell it to create a new window, we get two |
| 67 | + -- One from opening the application, and the other from the command |
| 68 | + if is_running() then |
| 69 | + new_window() |
| 70 | + else |
| 71 | + call_forward() |
| 72 | + end if |
| 73 | + end if |
| 74 | + |
| 75 | + -- Make sure a window exists before we continue, or the write may fail |
| 76 | + repeat until has_windows() |
| 77 | + delay 0.01 |
| 78 | + end repeat |
| 79 | + |
| 80 | + send_text(argv) |
| 81 | + call_forward() |
| 82 | +end run |
0 commit comments