Save Selection and Compare Selected Files scripts for BBEdit

Here are a pair of handy scripts for BBEdit (or TextWrangler). Paste them in to Applescript Editor, save them in your BBEdit Scripts folder, and assign shortcut keys in BBEdit > Preferences > Menus & Shortcuts.

Save Selection as New File

Select some text and invoke this script to create and save a new file containing the selection. (Add close doc to the end of the block if you don’t need to edit the file.)

tell application "BBEdit"
	set txt to contents of selection
	set doc to make new document with properties {contents:txt}
	save doc
end tell

This is pretty similar to choosing File > New > Text Document (with selection) and then choosing Save, but it combines the two into one step, which is helpful if you need to do this repeatedly.

Compare Selected Project Files

Use this script to compare two files selected in the files list of a disk browser or project window.

tell application "BBEdit"
	set selectedProjectItems to selected items of project window 1
	if (count of selectedProjectItems) is not 2 then return
	set comparisonResults to compare first item of selectedProjectItems against second item of selectedProjectItems
	if not differences found of comparisonResults then
		display alert "No differences found:" message reason for no differences of comparisonResults
	end if
end tell

You can accomplish the same thing with Search > Find Differences, but if you’ve already selected some files this is quicker.

Posted on Thursday, March 29th, 2012. Tags: , .