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

Ramón Figueroa-Centeno ramonf at hawaii.edu
Tue Apr 6 12:23:49 CEST 2010


Aloha,

Here are some minor revisions to the example (error checking and cleaning
the code slightly):

--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
that
-- we must escape '\' by writing '\\' and '"' by writing '\"' (the single
quotations 
-- are not part of the command).
property UNIXfilter : "sed 's/^[ \\t]*//' | sed 's/[ \\t]*$//' | sort -f | 
uniq -i"

tell application "TeXShop"
	
	try
		set the front_document to the front document
	on error
		beep
		return
	end try
	
	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
	else
		return
	end if
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
filter
set command to "cat " & texpath & " | " & UNIXfilter

tell me to set filtered to do shell script command

tell application "TeXShop"
	set the content of the selection of the front_document to filtered
end tell

try -- to delete the temporary file
	tell me to do shell script "rm -f " & POSIX path of TMP
end try

-----------------------------------------

Enjoy!

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



More information about the macostex-archives mailing list