Launch NetBeans when opening files in the MacOS X Finder

I recently switched from Open Komodo to NetBeans 7. What drove me absolutely nuts was the fact that I couldn’t drag and drop files onto the NetBeans application to have them open. Further complicating the issue, NetBeans does not use a standard file browser dialog so you cannot drag and drop onto that either.

Here is my work around. It allows you to drag and drop a single file, or group of files and have them open in NetBeans. I created an application with AppleScript that takes the dropped files and passes them to a shell command that opens netbeans. To use this you must first have NetBeans already launched. I keep this application sitting in my Dock right next the NetBeans icon.

You may need to edit the path to NetBeans if you are using a different version or location

The AppleScript

-- This AppleScript created by John Kramlich of http://www.johnkramlich.com
-- Modify it in anyway you see fit.
return -- not needed, but shows that the script stops here when "run"

on open of finderObjects -- "open" handler triggered by drag'n'drop launches
	repeat with i in (finderObjects) -- in case multiple objects dropped on applet
		
		set mypath to posix_path(i)
		
		do shell script "/Applications/NetBeans/NetBeans\\ 7.0.app/Contents/MacOS/netbeans --open " & mypath
		
	end repeat
	
	tell application "NetBeans 7.0"
		activate
	end tell
	
end open

on posix_path(mac_path)
	set mac_path to (mac_path as text)
	set root to (offset of ":" in mac_path)
	set rootdisk to (characters 1 thru (root - 1) of mac_path)
	tell application "Finder"
		if (disk (rootdisk as string) is the startup disk) then
			set unixpath to "/" & (characters (root + 1) thru end of mac_path)
		else
			set unixpath to "/Volumes:" & mac_path
		end if
	end tell
	set chars to every character of unixpath
	repeat with i from 2 to length of chars
		if item i of chars as text is equal to "/" then
			set item i of chars to ":"
		else if item i of chars as text is equal to ":" then
			set item i of chars to "/"
		else if item i of chars as text is equal to "'" then
			set item i of chars to "\\'"
		else if item i of chars as text is equal to "\"" then
			set item i of chars to "\\" & "\""
		else if item i of chars as text is equal to "*" then
			set item i of chars to "\\*"
		else if item i of chars as text is equal to "?" then
			set item i of chars to "\\?"
		else if item i of chars as text is equal to " " then
			set item i of chars to "\\ "
		else if item i of chars as text is equal to "\\" then
			set item i of chars to "\\\\"
		end if
	end repeat
	return every item of chars as string
end posix_path

The Icon

Download the Files

Posted June 1st, 2011 in Tools. Tagged: , , .

4 comments:

  1. Dedoko:

    Fantastic – that has been driving me nuts too! I had even drifted back to Komodo IDE because of that issue.

    Thanks.

  2. Kevin:

    Awesome, thank you very much for this script! The only problem seems that i can’t assign this as default application to all my .php files (or any other default). It always discards my change in the open with menu.

  3. omar:

    Yes, thank you thank you for this. Makes the netbeans solution complete for me!

  4. Marcin:

    I made little update for auto quit :

    on open of finderObjects — “open” handler triggered by drag’n’drop launches
    repeat with i in (finderObjects) — in case multiple objects dropped on applet

    set mypath to posix_path(i)

    do shell script “/Applications/NetBeans/NetBeans\\ 8.0.2.app/Contents/MacOS/netbeans –open ” & mypath

    end repeat

    tell me to quit
    –tell application “NetBeans 8.0.2″
    — activate
    –end tell

    end open

    on posix_path(mac_path)
    set mac_path to (mac_path as text)
    set root to (offset of “:” in mac_path)
    set rootdisk to (characters 1 thru (root – 1) of mac_path)
    tell application “Finder”
    if (disk (rootdisk as string) is the startup disk) then
    set unixpath to “/” & (characters (root + 1) thru end of mac_path)
    else
    set unixpath to “/Volumes:” & mac_path
    end if
    end tell
    set chars to every character of unixpath
    repeat with i from 2 to length of chars
    if item i of chars as text is equal to “/” then
    set item i of chars to “:”
    else if item i of chars as text is equal to “:” then
    set item i of chars to “/”
    else if item i of chars as text is equal to “‘” then
    set item i of chars to “\\'”
    else if item i of chars as text is equal to “\”” then
    set item i of chars to “\\” & “\””
    else if item i of chars as text is equal to “*” then
    set item i of chars to “\\*”
    else if item i of chars as text is equal to “?” then
    set item i of chars to “\\?”
    else if item i of chars as text is equal to ” ” then
    set item i of chars to “\\ ”
    else if item i of chars as text is equal to “\\” then
    set item i of chars to “\\\\”
    end if
    end repeat
    return every item of chars as string
    end posix_path

Leave a response:

*