Broken Insert Reference

The official TeXShop Support Forum.

Re: Broken Insert Reference

Postby Herbert Schulz » Wed Jan 18, 2012 7:01 pm


Michael Sharpe, on the Mac OS X TeX e-mail list (see the message near the top of this forum), has fixed up the Insert Reference macro so that it works with TeXShop 3.0x under Mac OS X 10.7 (Lion).

Code: Select all
--Applescript direct

-- 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


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.


set docname to #DOCUMENTNAME#
if docname is equal to "" then
   display dialog "No document---bailing out."
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
   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" to set current_selection to the content of selection of document docname

--set find_label_lines to ("grep -h '\\\\label' " & texfiles_tosearch) as string
set find_label_lines to ("cat " & texfiles_tosearch & " | tr '\\r' '\\n'  | grep '\\\\label{' " & 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 " & quoted form of the current_selection
if sort_output then
   set sort to " | sort -u"
   set sort to ""
end if
-- glue the shell scripts together:
set get_labels_shell_script to find_label_lines & trim_to_raw_labels & sort
   -- 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) ask to diplay all labels:
   display dialog "No labels found that contain '" & the current_selection & "'. 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)
      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"
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
      set ref_insert to "\\" & ref_command & "{" & label_insert & "}"
   end if
   tell application "TeXShop" to set the selection of document docname to ref_insert as string
end if

To use this code press Select All for the Code section above and Copy it (Cmd-C). Open TeXShop and then the Macro Editor (Macros->Open Macro Editor…). Select the Insert Reference macro in the list on the left, click in the Content pane and Select All (Cmd-A) there. Then simply Paste the code you copied from the Code section above over the old macro. Then Save.

If you customized the previous macro to search all files in the same directory you'll have to change that parameter (near the top of the macro) again.
Good Luck,
Herb Schulz
Herbert Schulz
Posts: 1489
Joined: Thu May 12, 2005 2:31 pm


Return to TeXShop (engl.)

Who is online

Users browsing this forum: Williejet and 1 guest