[OS X TeX] TIP: How to write a Unix filter for TeXShop

Ramón Figueroa-Centeno ramonf at hawaii.edu
Thu Apr 1 04:00:50 EDT 2010


Here is a complete example of how to build a Unix filter for TeXShop via
Applescript for TeXShop's Macro menu (this one sorts the lines in the

--Applescript direct
-- Example script to invoke a Unix filter. 
-- In this example we  sort the lines in the selection, without repeats,
ignoring the case 
-- and leading and trailing blanks.
-- Ramon Figueroa-Centeno (March 31, 2010)

-- We use the command "sort -f | sed 's/[ \t]*$//' | uniq -i"; however, note
-- we must escape '\' by writing '\\' and '"' by writing '\"' (the single
-- are not part of the command).
property UNIXfilter : "sed 's/^[ \\t]*//' | sed 's/[ \\t]*$//' | sort -f | 
uniq -i"

tell application "TeXShop"
	set the front_document to the front document
	set the_selection to (the content of the selection of the front_document)
	-- the Applescript command "do shell script" chokes if given a large amount
of data
	-- the workaround is to dump the data into a temporary file
	if the_selection is not "" then
		tell me
			set texpath to do shell script "mktemp /tmp/XXXXXXXX"
			set TMP to POSIX file texpath
			open for access TMP with write permission
			write the_selection to TMP
			close access TMP
		end tell
		-- Convert the line endings to Unix ones, as is expected by most command
line programs
		-- (we use flip which is part of the standard TeXShop distribution)
		tell me to do shell script "~/Library/TeXShop/bin/flip -u " & texpath
		-- Note: you may need to tweak this line according to the syntax of your
		set command to "cat " & texpath & " | " & UNIXfilter
		tell me to set filtered to do shell script command
		set the content of the selection of the front_document to filtered
		try -- to delete the temporary file
			tell me to do shell script "rm -f " & POSIX path of TMP
		end try
	end if
end tell


Now, bells and whistles could be added:

1. Have the result be selected in TeXShop.
2. Have it present a list of filters to choose from a list of filters in a
filters folder (like in BBEdit).

I probably can add these features, if there is interest. However, it would
not be a short example anymore :)


View this message in context: http://n2.nabble.com/TIP-How-to-write-a-Unix-filter-for-TeXShop-tp4835743p4835743.html
Sent from the MacOSX-TeX mailing list archive at Nabble.com.

More information about the MacOSX-TeX mailing list