<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>name</key>
	<string>ROOT</string>
	<key>submenu</key>
	<array>
		<dict>
			<key>content</key>
			<string>--Applescript direct

-- "INSERT REFERENCE"
-- 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. 

-- v2004-08-24
--Modified to work with Lion/TeXShop 3.06 by Michael Sharpe 2012-01-18

-- 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 : true
-- 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 : false
-- 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:


set docname to #DOCUMENTNAME#
if docname is equal to "" then
	display dialog "No document---bailing out."
	return
end if

if save_before_run then
	tell application "TeXShop" to save docname
end if

--tell application "TeXShop" to set texpath to the path of the front document -- the current tex file: "/path/to/docu.tex"
set texpath to #FILEPATH#--full path

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 &amp; "*.tex" -- search all .tex files: "/path/to/*.tex"
end if

tell application "TeXShop" to set current_selection to the content of selection of document docname

-- DO SHELL SCRIPT COMPONENTS:
--set find_label_lines to ("grep -h '\\\\label' " &amp; texfiles_tosearch) as string
set find_label_lines to ("cat " &amp; texfiles_tosearch &amp; " | tr '\\r' '\\n'  | grep '\\\\label{' " &amp; texfiles_tosearch) as string
-- (Better commmand suggested by Matthew Hills which also converts the line-endings to UNIX form - thanks!)
set trim_to_raw_labels to " | sed 's/.*\\\\label{//' | sed 's/}.*//'"
set filter_selection to " | grep " &amp; quoted form of the current_selection
if sort_output then
	set sort to " | sort -u"
else
	set sort to ""
end if
	
-- glue the shell scripts together:
set get_labels_shell_script to find_label_lines &amp; trim_to_raw_labels &amp; sort
	
try
	-- filter choices with the current selection:
	set choose_labels to every paragraph of (do shell script get_labels_shell_script &amp; filter_selection)
on error
	-- If it fails (e.g., nothing found) ask to diplay all labels:
	activate
	display dialog "No labels found that contain '" &amp; the current_selection &amp; "'. Would you like to view the whole list?" buttons {"Cancel", "View all"} default button "View all"
	if the button returned of the result is "View all" then
		set choose_labels to every paragraph of (do shell script get_labels_shell_script)
	else
		return -- exit script
	end if
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 "\\" &amp; ref_command &amp; "{" &amp; label_insert &amp; "}"
	end if
	tell application "TeXShop" to set the selection of document docname to ref_insert as string
end if
	
</string>
			<key>key</key>
			<string>R+ShiftKey</string>
			<key>name</key>
			<string>Insert reference</string>
		</dict>
		<dict>
			<key>content</key>
			<string>-- Applescript direct

-- OPEN QUICKLY...
-- The idea for this script was stolen from iTeXMac.
-- This script pops up a dialog box with a list of  files in the current directory.
-- The selection (multiple allowed) is opened.

-- CUSTOMISE TO YOUR LIKING:
property extensions_to_open : {"tex", "sty", "pdf"}


-- THE SCRIPT:

--get the path to the current tex file - "/path/to/docu.tex":
tell application "TeXShop" to set texpath to the path of the front document

-- Convert to applescript format - "path:to:docu.tex"
set texloc to POSIX file texpath
-- Get the folder it's in - "path:to:"
tell application "Finder" to set texfolder to the container of (texloc as alias)
-- Get the path to the folder - "/path/to/"
set texdir to the POSIX path of (texfolder as alias)
-- Get a list of all the filenames to open:
set texfiles to the name of every file of texfolder whose name extension is in extensions_to_open

tell application "TeXShop" to set openfiles to choose from list texfiles with prompt "Which file(s) do you wish to open?" with multiple selections allowed
if openfiles is not false then
	repeat with this_filename in openfiles
		set file_to_open to POSIX file (texdir &amp; this_filename)
                 set cdate to (current date) + 30 -- allow 30 seconds for timeout
                 tell application "TeXShop"
                    try 
                        open  file_to_open
                   on error errmsg number errnum
                        if (errnum = -1712) and ((current date) &lt; cdate) then
                          --applescript direct often provokes error number -1712
                          else
                              display dialog errmsg
                        end if
                   end try
                 end tell		
	end repeat
end if</string>
			<key>key</key>
			<string>O+ShiftKey</string>
			<key>name</key>
			<string>Open quickly…</string>
		</dict>
	</array>
</dict>
</plist>
