Clickable stack traces with Netbeans

I’m clearly in a fickle mood this month, as I’ve changed my PHP editor again, this time to Netbeans (thanks to the tireless nagging of @JJJ)… so far, so good. I particularly love being able to jump to declarations quickly and easily.

One hurdle I overcame today, and I’d be interested to know if there’s a more Netbeans-y way of doing this, is getting my beloved Xdebug clickable stack traces (see my previous post to get these working with Textmate and Sublime Text).

It seems that, by default, Netbeans doesn’t register a URL protocol which can be used to open and pass arguments to the application, but there is a commandline interface to it. After some mucking around with a shellscript, from which I could launch Netbeans and set the line in the editor, but not register the script against a URL protocol, I turned to Applescript.

Working from examples from Shotgun and Nik Friedman TeBockhorst I was able to put a very small Applescript application together to open Netbeans and set the line in the editor.

(Incidentally, an Applescript application which can register URL protocols? That’s pretty cool. Nik TeBockhorst’s example, linked above, is worth a read to see another use of this capability.)

Step 1: Setup Xdebug in PHP

xdebug.file_link_format="nbopen://%f:%l"

You’ll need to restart either PHP directly (if you’re using something like PHP Fast-CGI) or Apache (f you’re using PHP with Apache) to get the change to take effect.

Step 2: Create or install the Applescript application

If you’re using Netbeans 7.2 on OS X Mountain Lion, then you could simply download the NBOpen application and it’ll probably work for you.

If you are on a different version of Netbeans or you want to follow the process through from scratch, then fire up the AppleScript Editor and paste the following into a new script:

-- The URL to open Netbeans should look like this:
-- nbopen:///Users/simon/Projects/Promotone/htdocs/wp-content/plugins/curated-tweets/class-curated-tweets.php:60
-- Note that that URL is passed to the app in the following form, note the two forward
-- slashes after the colon are stripped.:
-- nbopen:/Users/simon/Projects/Promotone/htdocs/wp-content/plugins/curated-tweets/class-curated-tweets.php:60
on open location the_url
	set text item delimiters to ":"
	set the_file to text item 2 of the_url
	set the_line to text item 3 of the_url
	-- log the_file
	do shell script "/Applications/NetBeans/NetBeans\\ 7.2.app/Contents/MacOS/netbeans --open " & the_file & ":" & the_line
end open location

Save the script. Then go to File > Export and export the script as an application. Open the resulting application (which is an OS X package) in your editor of choice, and find Info.plist. Add the following code before the final closing </dict> and </plist>:

<key>CFBundleIdentifier</key>
<string>com.codeforthepeople.AppleScript.NBOpen</string>
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLName</key>
    <string>NBOpen</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>nbopen</string>
    </array>
  </dict>
</array>

Your resultant file should look something like:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleAllowMixedLocalizations</key>
	<true/>
	<key>CFBundleDevelopmentRegion</key>
	<string>English</string>
	<key>CFBundleExecutable</key>
	<string>applet</string>
	<key>CFBundleIconFile</key>
	<string>applet</string>
	<key>CFBundleIdentifier</key>
	<string>com.apple.ScriptEditor.id.nbopen</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleName</key>
	<string>nbopen</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleSignature</key>
	<string>aplt</string>
	<key>LSMinimumSystemVersionByArchitecture</key>
	<dict>
		<key>x86_64</key>
		<string>10.6</string>
	</dict>
	<key>LSRequiresCarbon</key>
	<true/>
	<key>WindowState</key>
	<dict>
		<key>dividerCollapsed</key>
		<false/>
		<key>eventLogLevel</key>
		<integer>-1</integer>
		<key>name</key>
		<string>ScriptWindowState</string>
		<key>positionOfDivider</key>
		<real>426</real>
		<key>savedFrame</key>
		<string>1 136 1388 742 0 0 1440 878 </string>
		<key>selectedTabView</key>
		<string>result</string>
	</dict>
	<key>CFBundleIdentifier</key>
	<string>com.codeforthepeople.AppleScript.NBOpen</string>
	<key>CFBundleURLTypes</key>
	<array>
	  <dict>
	    <key>CFBundleURLName</key>
	    <string>NBOpen</string>
	    <key>CFBundleURLSchemes</key>
	    <array>
	      <string>nbopen</string>
	    </array>
	  </dict>
	</array>
</dict>
</plist>

You can test it’s worked by creating a URL of the form nbopen://[some file path]:[some line number] and pasting it into your browser.

The further tips on Xdebug in the previous article will work as well with Netbeans as with any other editor. In the future, I plan to link up Netbeans with the Remote debugging capabilities of Xdebug, but that’s for another day.

Update: Here’s my Gist of the Applescript, in line with a Version Control All The Things philosophy.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.