Skip to content

Instantly share code, notes, and snippets.

@uncreative
Created July 22, 2011 19:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save uncreative/1100212 to your computer and use it in GitHub Desktop.
Save uncreative/1100212 to your computer and use it in GitHub Desktop.
externally open a specific file in eclipse, and go to some line
(*
-------- WHAT THIS DOES --------
Enables you to externally open a specific file in eclipse, and go to some line
that is, makes a link with href of
openineclipse://open?url=file:///absolute/path/tofile.ext&line=5
open absolute/path/tofile.ext in eclipse
and go to line 5
-------- HOW TO MAKE THIS WORK --------
Open AppleScript Editor
Paste the contents of this gist
Save as Application OpenInEclipse.app under /Applications
edit /Applications/OpenInEclipse.app/Contents/Info.plist
make sure the following keys are set and save Info.plist
<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>
-------- TEST IT IS WORKING --------
save an html file testopenineclipse.html:
<html><body>
<a href="openineclipse://open?url=file:///etc/hosts&line=3">open this file in eclipse and go to line 5</a>
</body></html>
-------- WHY ISN'T IT WORKING? --------
uncomment:
#display alert "DEBUG: file " & filename & " line " & line_number
click on link again:
- an alert didn't show up: see below on how to force launch services to register a url
- are the filename and line_number parsed correctly?
make sure the href tag is correct or mess with the code below to try and parse the parameters better?
- the alert shows but the file doesn't open
so far this has only been tested when eclipse was already open
change the path to eclipse by modifying the following:
set path_to_eclipse to "/Applications/eclipse/Eclipse.app"
make sure eclipse is able to open a file from terminal:
$ open -a /path/to/your/eclipse.app /etc/hosts
if this doesn't work, maybe your eclipse version is too old?
- the file opens but it doesn't go to the line number
for system events to work with key code, you must "Enable access for assistive devices" in "System Preferences" under "Universal Access"
also, if you change the keyboard shortcut for go to line in eclipse, you'll have to change the following line to send the correct key
tell application "System Events" to key code 37 using command down #send command L
finally, you can try to increase the sleep time before eclipse tries 'go to line' by modifying the following:
set sleep_before_going_to_line to "0.5" # in seconds
-------- INTEGRATE IN XDEBUG --------
in php.ini, in the [xdebug] section:
xdebug.file_link_format="openineclipse://open?url=file://%f&line=%l"
don't forget to restart apache:
$ sudo apachectl restart
*)
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
(* LaunchServices automatically registers URL protocols of applications newly dragged to /Applications
To force OSX to re-register the URL protocol of this app (a restart won't do it):
$ /usr/bin/python
> import LaunchServices
> from Foundation import NSURL
> appURL = NSURL.fileURLWithPath_("/Applications/OpenInEclipse.app")
> status = LaunchServices.LSRegisterURL(appURL, True)
or all applications:
$ /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -f -r /Applications
to see the registered:
$ /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump
*)
##### REFERENCES
# simulate key presses reference
#http://dougscripts.com/itunes/itinfo/keycodes.php
# create custom protocol for launching external app reference:
# https://support.shotgunsoftware.com/entries/127152-launching-external-applications-using-custom-protocols-under-osx
# link example
#openineclipse://open?url=file:///absolute/path/tofile.ext&line=5
@staabm
Copy link

staabm commented Jul 6, 2018

Any way to make this work on windows?

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