Adding Command Line Tools to BBEdit

BBEditWe use BBEdit for a good portion of our html/css/javascript production work. Like many OS X applications BBEdit allows you to execute Applescript from its script menu. This makes it very easy to add command line tool support (e.g., linters, minimizers, compilers) to the application.

In the example below the Applescript will run the Google Closure Compiler (see TechNote #36) on the saved version, of a javascript file, open in the front most document window. If there are errors the Applescript try block will direct the output to a new BBEdit document window.

To make this script available to BBEdit simply place your .scpt file in BBEdit’s Scripts Folder. For more info about scripting BBEdit see the application documentation. To get started with Applescript see TechNote #4.

tell application "BBEdit"
	set jsfile to file of text document 1
end tell


on process_item(this_item)
  set theFile to quoted form of the POSIX path of (this_item as text)
  set the item_info to info for this_item
  set ext to the name extension of the item_info
  set i to length of ext
  set nam to the name of the item_info
  set j to length of nam
  set nam to text 1 thru (j - i - 1) of nam
  set dir to the POSIX path of (this_item as text)
  set k to length of dir
  set dir to text 1 thru (k - j) of dir
  set outFile to quoted form of the POSIX path of (dir & nam & "_compiled." & ext)
  set theFile to quoted form of the POSIX path of (this_item as text)
  set AppleScript's text item delimiters to " "
    do shell script "java -jar /Applications/Utilities/closure/compiler.jar  --js_output_file=" & outFile & " " & theFile
  on error errorMessage
    tell application "BBEdit"
      set dest to make new text document
      set text of dest to errorMessage
      set name of dest to nam & "." & ext & " -- " & "Closure compiler errors"
    end tell
  end try
end process_item

TechNote #41