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

Will Robertson will at guerilla.net.au
Fri Aug 20 15:29:59 EDT 2004


Hello again

I seem to be getting caught up in creating scripts for TeXShop at the 
moment.
By popular demand you may be able to convince me to write them for 
iTeXMac as well.

This script is for inserting references. You won't need to ever 
remember a label name again! I suggest the key combo CMD+SHIFT+R. Hit 
the keys and click your reference - that's it. Read the blurb of the 
script for more info.

Please try it out and let me know how you go. It it works, tell me and 
I'll be happy! If it doesn't, I can fix the problems. If all's well, 
I'll put it up on my site.

Ciao!
Will

PS If you know anything about grep/sed/awk and wouldn't mind looking at 
my commands, I'd be very grateful (there's just three one liners all 
piped together). I'm just winging it, so I don't know if I'm doing it 
efficiently or even correctly. Bit of a UNIX newbie - I only know 
enough to do damage, really.


ATTACHED TEXSHOP MACRO:
(COPY/PASTE INTO A NEW ITEM IN THE MACRO EDITOR TO INSTALL)

--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. 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. The 
inserted reference label is wrapped in a (optional) customisable LaTeX 
command.
-- 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.



--THE SCRIPT:

tell application "TeXShop"

if only_scan_current_file
	set searchtexfiles to the quoted form of #TEXPATH# -- the current 
file: /path/to/docu.tex
else
	set texpath to alias posix file #TEXPATH#
	tell application "Finder" to set texfolder to the container of 
(texpath as alias)
	set texdir to the posix path of (texfolder as alias)
	set alltexfiles to the quoted form of texdir & "*.tex"
	set searchtexfiles to alltexfiles
end if

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' " & searchtexfiles) as 
string
set trim_to_raw_labels to " | sed 's/.*\\\\label{//' | sed 's/}.*//'"
set search_restrict to (" | grep " & quoted form of current_selection) 
as string

set get_labels_shell_script to find_label_lines & trim_to_raw_labels & 
search_restrict
set all_labels_str to do shell script get_labels_shell_script
set all_labels to every paragraph of all_labels_str

set label_insert to choose from list all_labels
if label_insert /= false
	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 MacOSX-TeX mailing list