[OS X TeX] New TeXShop macro: "Insert reference"

Will Robertson will at guerilla.net.au
Sat Aug 21 08:20:31 CEST 2004


Hello again!

I have a new version based on feedback:
¥ It now saves the front document when it begins in order to pick up 
labels you may have added since the last save.
¥ If you are trying to filter for something and nothing is found, 
instead of producing an error, it will simply display every label.
¥ You can now choose to sort the output alphabetically or in the order 
found in the document.

I'll be adding it to <http://www.mecheng.adelaide.edu.au/~will/tex> in 
a short while.

I hope I haven't made any silly mistakes like I always do!

Will

PS I'm adding more comments to my scripts so they make more sense if 
you try and read the source code. It's pretty amazing to me how short 
these things are compared to their convenience, and it's only possible 
through both applescript and shell scripts. A very powerful 
combination.

COPY/PASTE INTO A NEW ITEM IN THE MACRO EDITOR OF TEXSHOP:
------------------------------------

--Applescript

-- "INSERT REFERENCE", for TeXShop
-- This script searches through the current file (or within every file 
in the current folder) for \label{...} commands, then pops up a list 
for you to choose from which you may insert one.
--The inserted reference label is wrapped in a (optional) customisable 
LaTeX command.
-- It will cut down the list to include only those entries that contain 
the current selection. So if you type "sec" and select it, then run 
this script, you will get a choice of only, e.g., 
"sec:intro","sec:conc","fig:secunia", but not other labels in your 
document e.g., "cha:monkey","equ:maxwell",etc. 

-- 2004 Will Robertson


-- CUSTOMISE TO YOUR LIKING:

property ref_command : "ref" -- or "vref" or "fref" etc... N.B. no 
backslash!
-- Inserts e.g. "\ref{fig:ladle}" or "\vref{fig:ladle}" or ... based on 
this property
-- Put "" to insert the plain label: e.g. "fig:ladle" 

property only_scan_current_file : false
-- If false, the script will search through every .tex file in the 
folder containing the current file.
-- This is useful for projects with other \include{} -ed files in the 
same folder.
-- Otherwise, it will only look in the current file.

property sort_output : true
-- If false, displays labels in the order they're found in the document.
-- Otherwise, displays them in alphabetical order.

property save_before_run : true
-- The script only finds labels created in saved documents. If you 
don't want this script to save your document before it runs for 
whatever reason, set this to true.

--THE SCRIPT:


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 -- the current tex file: "/path/to/docu.tex"

if only_scan_current_file then
	set texfiles_tosearch to the quoted form of the texpath -- search the 
current file
else
	set texloc to POSIX file texpath -- Convert to applescript format: 
"path:to:docu.tex"
	tell application "Finder" to set texfolder to the container of (texloc 
as alias) -- Get the folder: "path:to:"
	set texdir to the POSIX path of (texfolder as alias) -- Convert to 
UNIX path: "/path/to"
	set texfiles_tosearch to the quoted form of texdir & "*.tex" -- search 
all .tex files: "/path/to/*.tex"
end if

tell application "TeXShop"
	
	set current_selection to the content of the selection of the front 
document
	
	-- DO SHELL SCRIPT COMPONENTS:
	set find_label_lines to ("grep -h '\\\\label' " & texfiles_tosearch) 
as string
	set trim_to_raw_labels to " | sed 's/.*\\\\label{//' | sed 's/}.*//'"
	set filter_selection to " | grep " & quoted form of the 
current_selection
	if sort_output then
		set sort to " | sort -u"
	else
		set sort to ""
	end if
	
	-- pipe the shell scripts together:
	set get_labels_shell_script to find_label_lines & trim_to_raw_labels & 
sort
	
	try
		-- filter choices with the current selection:
		set choose_labels to every paragraph of (do shell script 
get_labels_shell_script & filter_selection)
	on error
		-- If it fails (e.g. nothing found) display the whole list:
		set choose_labels to every paragraph of (do shell script 
get_labels_shell_script)
	end try
	
	-- In case the document doesn't contain any labels:
	if choose_labels = {""} then
		display dialog "Sorry, no \\label{} items have been found." buttons 
{"No worries"} default button "No worries"
		return
	end if
	
	set label_insert to choose from list choose_labels with prompt "Please 
choose the label of the reference to insert:"
	if label_insert ­ false then
		if ref_command = "" then
			set ref_insert to label_insert
		else
			set ref_insert to "\\" & ref_command & "{" & label_insert & "}"
		end if
		set the selection of the front document to ref_insert as string
	end if
	
end tell
--------------------- 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