Skip to content

Instantly share code, notes, and snippets.

@richie5um
Last active November 18, 2015 21:35
Show Gist options
  • Save richie5um/8c580fe23f22a6d55b73 to your computer and use it in GitHub Desktop.
Save richie5um/8c580fe23f22a6d55b73 to your computer and use it in GitHub Desktop.
If you are using VSC v10.0+: Copy to '/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node-debug/out/node'. If you are using VSC v0.9: Copy to '/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node-debug/debugAdapter/out/node'
---------------------------------------------------------------
-- Copyright (C) Microsoft Corporation. All rights reserved.
-- Updated by R. Somerfield (hopefully without causing issues for Microsoft's rights).
---------------------------------------------------------------
-- the following two properties are persisted across different runs of this TerminalHelper script
-- they are used to reuse the same terminal across invocations
property lastTty : missing value
property lastWindowID : missing value
on run argv
set window_title to missing value
set working_dir to missing value
set runtime to ""
set runtimeArgs to ""
set runtimeName to missing value
set program to ""
set programArgs to ""
set env_vars to ""
set activate_console to false
repeat with i from 1 to (count of argv)
set a to (item i of argv)
if a = "-w" then
set i to i + 1
set working_dir to quoted form of (item i of argv)
else if a = "-r" then
set i to i + 1
set runtime to quoted form of (item i of argv)
else if a = "-ra" then
set i to i + 1
set runtimeArgs to runtimeArgs & " " & quoted form of (item i of argv)
else if a = "-rn" then
set i to i + 1
set runtimeName to (item i of argv)
else if a = "-p" then
set i to i + 1
set program to quoted form of (item i of argv)
else if a = "-pa" then
set i to i + 1
set programArgs to programArgs & " " & quoted form of (item i of argv)
else if a = "-e" then
set i to i + 1
set env_vars to env_vars & " " & quoted form of (item i of argv)
else if a = "-t" then
set i to i + 1
set window_title to (item i of argv)
else if a = "-a" then
set i to i + 1
set activate_console to true
end if
end repeat
set cmd to ""
if working_dir ≠ missing value then
set cmd to "cd " & working_dir & "; "
end if
if env_vars ≠ "" then
set cmd to cmd & "env" & env_vars
end if
set cmd to cmd & " " & runtime
if runtimeArgs ≠ "" then
set cmd to cmd & runtimeArgs
end if
set cmd to cmd & " " & program
if programArgs ≠ "" then
set cmd to cmd & programArgs
end if
tell application "iTerm"
activate
tell the first terminal
launch session "Default"
tell the current session
set lastTty to tty
write text cmd
end tell
if runtimeName ≠ missing value then
-- find the process with name 'runtimeName' that runs under the given tty and return its process id
repeat 10 times
delay 0.5
set pid to exec command "ps -c -t " & lastTty & " | awk '$4==\"" & runtimeName & "\" { print $1 }'"
if pid ≠ "" then
return pid
end if
end repeat
end if
end tell
end tell
return 0
end run
on findNonBusyTtyTab(aWindow, aTty)
tell application "iTerm"
repeat with currentWindow in windows
set theWindowId to id of currentWindow
if theWindowId = aWindow then
repeat with currentTab in tabs of currentWindow
set theTty to tty of currentTab
if (theTty = aTty) and (currentTab is not busy) then
return currentTab
end if
end repeat
end if
end repeat
end tell
return null
end findNonBusyTtyTab
on window_of_tab(aTty)
tell application "iTerm"
repeat with currentWindow in windows
repeat with currentTab in tabs of currentWindow
set theTty to tty of currentTab
if (theTty = aTty) then
return currentWindow
end if
end repeat
end repeat
end tell
return null
end window_of_tab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment