Skip to content

Instantly share code, notes, and snippets.

@humblehacker
Last active June 26, 2020 00:11
Show Gist options
  • Save humblehacker/6b8d9cb43419093648aaa06ef46fd2ac to your computer and use it in GitHub Desktop.
Save humblehacker/6b8d9cb43419093648aaa06ef46fd2ac to your computer and use it in GitHub Desktop.
Given an absolute path, line number, and column number, launch Visual Studio for Mac and go there.
on run {filepath, linenumber, columnnumber}
tell application "Visual Studio" to activate
tell application "System Events"
-- ⌘O File → Open...
keystroke "o" using command down
delay 1
-- ⇧⌘G Goto file path
keystroke "g" using {command down, shift down}
delay 1
-- type in filepath
keystroke filepath
delay 1
-- ⏎
key code 36
delay 1
-- ⏎
key code 36
delay 1
-- ⌘L Search → Goto → Line...
keystroke "l" using command down
delay 1
-- type in linenumber:columnnumber e.g. '17:11'
keystroke linenumber
key code 41 using shift down -- :
keystroke columnnumber
-- ⏎
key code 36
end tell
end run
@humblehacker
Copy link
Author

This script mostly works, but is not ideal for obvious and less obvious reasons. Obviously, driving the UI with generated keystrokes is brittle, and will break under many circumstances. Non-obviously, the use of delay throughout doesn't work as expected. None of these delays appear to be lasting for a second. So sometimes, the operation seems to quit half-way through, or you end up typing the line and column numbers into the file that you opened. Even more odd, sometimes the ⌘G operation gets executed twice.

So use this at your own risk. If anyone comes up with a better way, please let me know.

With that said, I use this as an external tool in Jetbrains Rider. Here's how:

Screen Shot 2020-06-25 at 5 05 17 PM

You can do the same thing in Visual Studio:

Screen Shot 2020-06-25 at 5 08 40 PM

Unfortunately, you apparently can't use ~ or environment variables like $HOME in the command field.

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