[OS X TeX] question on texcount applescript (selected text only)

Daniel Becker daniel.becker at uni-rostock.de
Sat Jun 20 16:26:49 EDT 2009


Am 20.06.2009 um 17:40 schrieb Daniel Becker:

> http://www2.hawaii.edu/~ramonf/TeXShop/TeXCount.html
> Works very well and already uses temporary files but I don't know how
> add something like
>
> * if text is selected, dump only that part to TMP that is selected

I tried myself and have something that seems to work. Charcater- 
Counting based on texcount is maybe nonsense, so I used detex and wc.  
Works both for selected text and the whole document if nothing is  
selected.

Comments welcome.....

Daniel







--Applescript
-- Script to invoke TeXCount, detex etc. based on  <http://folk.uio.no/einarro/Comp/texwordcount.html 
 >

tell application "TeXShop"
	activate
	-- empty the clipboard and fill it with selected text
	set the clipboard to ""
	set the front_document to the front document
	tell application "System Events" to keystroke "c" using command down
	set textSelection to the clipboard
          	-- test whether the clipboard is non-empty
	if textSelection is "" then
set countmodeused to "Count of WHOLE DOCUMENT:"
		-- in this case, the whole document should be counted
		-- If the front document is not saved dump its
		-- content to a temporary file and use that
		if the front_document is modified then
			-- The whole text of the document
			set whole_document to (the text of the front_document) as string
			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 whole_document to TMP
				close access TMP
			end tell
		else
			-- If the front document is saved then make a symbolic
			-- link to it in the /tmp directory. This way we avoid
			-- the "bug" (feature) in texcount that takes a file with
			-- spaces in its name name and takes it as a list of files
			-- even in quoted form or with escaped spaces.
			set texpath to the path of the front_document
			set symlinkpath to do shell script "mktemp /tmp/XXXXXXXX"
			do shell script "rm " & symlinkpath
			do shell script "ln -s " & (quoted form of texpath) & " " &  
symlinkpath
			set texpath to symlinkpath
			set TMP to texpath
		end if
	else
set countmodeused to "Count of SELECTED TEXT"
		-- in this case, work with selection
		set whole_document to textSelection as string
			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 whole_document to TMP
				close access TMP
			end tell
	end if
	
	
	
	
	
	set charcommand to "/usr/texbin/detex " & texpath & "| wc -c "
	
	tell me to set CharCount to do shell script charcommand
	
	set command to "/usr/texbin/texcount " & texpath & " | tail -n +2"
	
	tell me to set TeXcount to do shell script command
	
-- outputs the text used by texcount (with 20 extra chars)

set texcountcharcountcommand to "/usr/texbin/texcount -v1 -nocodes - 
nocol " & texpath & " | tail -c +20 | tail -r | tail -n +9 | tail -r   
| tail -c +2 | wc -c"
	
tell me to set texcountcharcount to do shell script  
texcountcharcountcommand
set the clipboard to texcountcharcount
set texcountcharcountcorr to (texcountcharcount - 21)
	
	-- If a temporary file or symbolic link was created delete it.
	try
		-- do shell script "rm " & POSIX path of TMP
	end try
	
	
	display dialog countmodeused & "
=======================

1) Characters (detex + wc): " & CharCount & "

2) Characters (texcount + wc): " & texcountcharcountcorr & "
    (doesn't work very well,
     detex is better for characters...)

3) Wordcount (texcount):

" & TeXcount buttons {"OK, thanks"} default button "OK, thanks"
	
end tell



More information about the MacOSX-TeX mailing list