Last active
November 6, 2022 20:18
-
-
Save ciiqr/41b049aede12c82b5a3c645c6b0927b6 to your computer and use it in GitHub Desktop.
(WIP) hack to run some command in Terminal.app and get it's output (relevant in contexts where Terminal has permissions to run a script but bash doesn't, ie. inside github actions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run_script_in_terminal() { | |
osascript - "$@" <<'EOF' | |
on FileExists(theFile) -- (String) as Boolean | |
try | |
do shell script "ls " & quoted form of theFile & " && ! lsof " & quoted form of theFile | |
return true | |
on error errMsg number errNum | |
return false | |
end try | |
end FileExists | |
on run argv | |
set outputPath to item 1 of argv | |
set command to rest of argv | |
-- quote command | |
repeat with x from 1 to (count command) | |
try -- skip errors | |
set item x of command to quoted form of (item x of command) | |
end try | |
end repeat | |
-- concat command | |
set saveTID to text item delimiters | |
set text item delimiters to " " | |
set commandString to command as text | |
set text item delimiters to saveTID | |
tell application "Terminal" | |
-- execute script in terminal | |
set T to do script commandString & " > " & quoted form of outputPath & " ; exit \"$?\"" | |
-- wait till output written | |
repeat until my FileExists(outputPath) | |
delay 0.1 | |
end repeat | |
repeat with winn in every window | |
try | |
set W to first tab of winn | |
if T is equal to W then | |
close winn | |
exit repeat | |
end if | |
end try | |
end repeat | |
end tell | |
end run | |
EOF | |
} | |
declare temp="$(mktemp)" | |
run_script_in_terminal "$temp" 'command' 'arg' 'arg2' | |
cat "$temp" | |
rm "$temp" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment