Skip to content

Instantly share code, notes, and snippets.

@uncreative
Created August 19, 2011 09:36
Show Gist options
  • Save uncreative/1156457 to your computer and use it in GitHub Desktop.
Save uncreative/1156457 to your computer and use it in GitHub Desktop.
externally open a specific file in eclipse, and go to some line
import LaunchServices
from Foundation import NSURL
appURL = NSURL.fileURLWithPath_("/Applications/OpenInEclipse.app")
status = LaunchServices.LSRegisterURL(appURL, True)
<key>CFBundleIdentifier</key>
<string>com.unpeelingthemagic.AppleScript.Eclipse</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>OpenInEclipse</string>
<key>CFBundleURLSchemes</key>
<array>
<string>openineclipse</string>
</array>
</dict>
</array>
</dict>
</plist>
on open location this_URL
### constants
set path_to_eclipse to "/Applications/eclipse/Eclipse.app"
set sleep_before_going_to_line to "0.5" # in seconds
### parse url
set line_number_starts to offset of "&" in this_URL
set filename to text ((length of "openineclipse://open?url=file://") + 1) thru (line_number_starts - 1) of this_URL
set line_number to text ((length of "line=") + line_number_starts + 1) thru -1 of this_URL
#display alert "DEBUG: file " & filename & " line " & line_number
### open the file in eclipse
set openCmd to "open -a " & path_to_eclipse & " '" & filename & "'"
do shell script openCmd
### go to line via keyboard shortcuts
# because of https://bugs.eclipse.org/bugs/show_bug.cgi?id=305336
do shell script "sleep " & sleep_before_going_to_line # wait half a second - eclipse is slow
tell application "System Events" to key code 37 using command down #send command L
tell application "System Events" to keystroke line_number
tell application "System Events" to keystroke return
end open location
<html><body>
<a href="openineclipse://open?url=file:///etc/hosts&line=3">open /etc/hosts file in eclipse and go to line 3</a>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment