-- Script to transfer the current BBEdit document to TeXShop
-- Ramon Figueroa-Centeno (March 10, 2009)
tell application "BBEdit"
	try
		if not (on disk of active document of text window 1) then
			display dialog "You need to save your document!" buttons {"OK"} default button 1 with icon stop
			return
		end if
		if (modified of active document of text window 1) then
			display dialog "You need to save changes to your document!" buttons {"Cancel", "Save"} default button 2 with icon caution
		end if
		save active document of text window 1
		set texFile to file of active document of text window 1
		set FILEPATH to POSIX path of texFile as string
		set selection_offset to characterOffset of selection
		set selection_length to length of the selection
		set selection_line_number to (startDisplayLine of selection as integer)
		tell application "TeXShop"
			activate
			open FILEPATH
			tell front document to goto line selection_line_number
			set the offset of the selection of the front document to selection_offset - 1
			set the length of the selection of the front document to selection_length
		end tell
		close active document of text window 1
	on error
		beep
	end try
end tell