Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Better/Working version of TerminalHelper.scpt for Visual Studio Code for Mac
-- This file resides at /Applications/Visual Studio
on run argv
set working_dir to missing value
set runtimeArgs to missing value
set runtimeName to missing value
set programArgs to missing value
set env_vars to ""
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 (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 (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)
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 missing value then
set cmd to cmd & " " & runtimeArgs
end if
set cmd to cmd & " " & program
if programArgs missing value then
set cmd to cmd & " " & programArgs
end if
tell application "Terminal"
set targetWindow to front window -- Default to the front window
set targetTab to selected tab of targetWindow -- Default to the current tab
if targetWindow null then
-- Create a new tab if the current window is busy
if targetTab is busy then
-- Delay required if/when you have to switch to a fullscreen instance since the new tab command will not fire in
delay 0.5
set tabCount to count of every tab in targetWindow
-- Create a new tab
tell application "System Events" to keystroke "t" using command down
-- Wait for the new tab to be created
repeat while (count of every tab in targetWindow) = tabCount
delay 0.1
end repeat
set targetTab to selected tab of targetWindow
end if
do script cmd in targetTab
set targetWindow to (do script cmd)
end if
set dev to tty of targetWindow
if runtimeName missing value then
-- find the "process" that runs under the given tty and remember its process id
set pid to ""
repeat 10 times
set pid to do shell script "ps -c -t " & dev & " | awk '$4==\"" & runtimeName & "\" { print $1 }'"
if pid "" then
exit repeat
end if
delay 0.5
end repeat
if pid "" then
do shell script "echo " & pid
end if
end if
end tell
end run
Copy link

whitlockjc commented May 7, 2015

With the original TerminalHelper.scpt, I would get the following output in the Developer Tools Console when attempting to run any Node.js script:

waiting for v8 protocol on stdout/stderr
setexceptionbreak error: not connected
/Applications/Visual Studio
Terminal got an error: AppleEvent handler failed. (-10000) Input string was not in a correct format.

I start debugging things by looking at Node.js invocation command being generated. It was fine so I moved on. The next thing I noticed was that focus was being given to but nothing was ever executed. After playing around with TerminalHelper.scpt, I found what appeared to be a latency related issue with how the script was locating the window to run the script in. (My is a fullscreen application so there is a delay when activating it, based on testing.) After adding a delay, things still did not work. If I removed the window resolution code and just used the current window, things worked so I rewrote this logic.

So in my version, I have the proper delays in place. I also use tabs instead of windows when it comes to where to run the script. Comments are in place to let you know what I'm doing and all changes I made are between line 59 and line 84.

Copy link

eogas commented May 24, 2015

Major props! I had the same issue and I swapped this out and I am now able to debug. To improve discoverability for this gist, the exact error I was seeing in the app was "could not launch 'node' in debug mode". Hopefully MS gets this fixed for the next release.

EDIT: I should note for any others coming across this page, I had to replace all of the 'not equals' signs with 'is not equal to', as they didn't come over nicely from the gist.

EDIT: Issue logged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment