[OS X TeX] TeXShop macro "Select class options"

Will Robertson will at guerilla.net.au
Mon Aug 30 05:57:47 CEST 2004


Hi all

Apologies to those iTeXMac users for my consistent bias against writing 
scripts for their program - I hope to rectify the situation soon-ish.

In the meantime, I have a new script for testing. Please try it out and 
let me know how it goes. It is run on the main tex file, and it will 
bring up a list of all possible class options available with the 
current document class. You can then select which ones you'd like and 
your document is edited to reflect your choice.

In theory, it can be adapted to work with any package, but that's a 
hurdle I'll cross in the future. The script is appended to the end of 
this post. When it's tested I'll sort out a better system for 
distributing them.

The script should be commented well enough to work out what's going on 
in the source, for those who're curious.

Applescripters, I have a question for you: I am trying to catch all the 
errors that can occur in various stages in the script. At the moment I 
have nested if-then-else statements, that become a little convoluted:

get such_and_such
if not false then
	get such_and_such2
	if not false then
		get such_and_such3
		...
	end if
end if

Is there an easier way to catch errors and then simply jump out of the 
script, like a "exit repeat" but for the whole script?

Good luck!

Will

<===CUT FROM HERE===>

--Applescript direct

--SELECT CLASS OPTIONS
-- This script determines the class of the front document and 
determines which options are available for it. The user is then 
prompted with a list of every possible option from which to make their 
choice with the currently selected options highlighted.
-- This script will only work if the \documentclass[...]{...} 
declaration exists on a single line of its own.
-- It will overwrite previously set options.

property save_before_run : true
-- If you don't want this script to save your document before it runs 
for whatever reason, set this to false.

--THE SCRIPT:
on run
	if save_before_run then
		tell application "TeXShop" to save the front document
	end if
	tell application "TeXShop" to set texpath to the path of the front 
document
	
	set {texclass, classline, currentoptions} to get_class(texpath)
	-- TEXCLASS is the class name begin used (article, memoir, etc.)
	-- CLASSLINE is the line number that the \documentclass is defined
	-- CURRENTOPTIONS are the class options already selected
	
	-- Error catch if the document doesn't contain \documentclass :
	if texclass ­ false then
		
		set classoptions to my get_class_options(texclass)
		-- CLASSOPTIONS is the list of all possible class options
		
		set docClassString to my choose_options(texclass, classoptions, 
currentoptions)
		-- This is the complete string to be inserted into the tex document.
		-- E.g. "\documentclass[letterpaper,12pt]{article}"
		
		-- Error catch if the user cancels:
		if docClassString ­ false then
			tell application "TeXShop"
				tell the front document to goto line (classline as number)
				set the content of the selection of the front document to 
docClassString
			end tell
		end if
	end if
end run

on get_class(texpath)
	--Returns:
	-- TEXCLASS : the class name begin used (article, memoir, etc.)
	-- CLASSLINE : the line number that the \documentclass is defined
	-- CURRENTOPTIONS : the class options already selected
	
	set cat_file to "cat " & quoted form of texpath -- cmd 2 return the 
text of the file
	set grep_class to " | grep '\\documentclass'" -- cmd 2 search for line 
containing \documentclass...
	set get_class to " | sed 's/.*{//' | sed 's/}.*//'" -- cmd 2 cut 
everything before { and everything after }
	set texclass to do shell script cat_file & grep_class & get_class -- 
pipe & run the commands to get the texclass
	
	set get_line_number to "| grep -n '\\documentclass' | cut -f 1 -d ':'" 
-- cmd 2 get the linenumber of the document class declaration
	set classline to do shell script cat_file & get_line_number -- pipe & 
run the commands to get the classline
	
	set get_options to " | sed 's/.*\\[//' | sed 's/\\].*//'" -- cmd 2 cut 
everything before [ and everything after ]
	set currentoptions to every word of (do shell script cat_file & 
grep_class & get_options) -- pipe & run to get the currentoptions
	
	if texclass = "" then
		-- Shell script has returned "" which implies there is no 
\documentclass in the current document
		display dialog "There appears to be no \\documentclass. This script 
may only be run on the main file of a LaTeX document." buttons {"OK"} 
default button 1
		return {false, false, false}
	else
		-- Proceed correctly
		return {texclass, classline, currentoptions}
	end if
	
end get_class


on get_class_options(texclass)
	-- Returns:
	-- CLASSOPTIONS : every possible option for the current class
	
	set classname to texclass & ".cls" -- append extension
	set classpath to do shell script 
"/usr/local/teTeX/bin/powerpc-apple-darwin-current/kpsewhich " & 
classname -- get the path to the class
	
	set cat_file to "cat " & quoted form of classpath -- cmd 2 return the 
text of the file
	set grep_options to " | grep '\\DeclareOption'" -- cmd 2 find the line 
containing "\DeclareOption"
	set get_options to " | sed 's/.*\\DeclareOption{//' | sed 's/}.*//'" 
-- cmd 2 get the name of the option
	
	set classoptions to every paragraph of (do shell script cat_file & 
grep_options & get_options) -- pipe & run the commands
	return classoptions
	
end get_class_options


on choose_options(texclass, classoptions, currentoptions)
	
	tell application "TeXShop" to set chooseoptions to choose from list 
classoptions with prompt "Select which class options you would like. 
-click for multiple selections." default items currentoptions with 
multiple selections allowed and empty selection allowed
	
	-- catch error if the user cancels:
	if chooseoptions ­ false then
		set optionsstring to ""
		repeat with thisoption in chooseoptions
			if optionsstring = "" then
				set optionsstring to optionsstring & thisoption
			else
				set optionsstring to optionsstring & "," & thisoption
			end if
		end repeat
		if optionsstring ­ "" then
			set optionsstring to "[" & optionsstring & "]"
		end if
		set classstring to "{" & texclass & "}"
		set docClassString to "\\documentclass" & optionsstring & classstring 
& "
" -- anyone know a better way to insert a newline?
		return docClassString
	else
		return false
	end if
end choose_options


-- Will Robertson 2004
-- You may do anything you wish with this script.
-- No liability for lost data etc. etc.

--------------------- Info ---------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
           & FAQ: http://latex.yauh.de/faq/
TeX FAQ: http://www.tex.ac.uk/faq
List Post: <mailto:MacOSX-TeX at email.esm.psu.edu>





More information about the macostex-archives mailing list