name ROOT submenu name Column macros submenu content --Applescript -- NEW TABULAR -- This script inserts a new tabular environment with a user-specified number of rows and columns. You can choose to have a plain table or a booktabs table - for the latter case you need to be using either the Memoir class or the booktabs package. Finally you may choose whether to have the table floating with a caption or simply centred. -- CUSTOMISE TO YOUR LIKING: property indent : " " -- edit this if you want other than 2 spaces for your indent property cell_text : "• " -- edit this if you want a different text blob property default_tabular_type: "Booktabs" -- or "Plain" property default_table_type: "Floating" -- or "Centred" -- THE SCRIPT: property texapp : "TeXShop" tell application texapp set q1 to display dialog "New tabular" & return & return & "Enter the number of columns you'd like and then select the type of tabular you want." default answer "3" buttons {"Cancel", "Plain", "Booktabs"} default button default_tabular_type set NCOLS to the text returned of q1 set tabtype to the button returned of q1 set q2 to display dialog "Enter the number of rows you'd like (not including a header row) and select whether you'd like a floating or simply centred table." default answer "3" buttons {"Cancel", "Centred", "Floating"} default button default_table_type set NROWS to the text returned of q2 set floattype to the button returned of q2 if the tabtype = "Booktabs" then set the toprule to "\\toprule" set the midrule to "\\midrule" set the bottomrule to "\\bottomrule" else set the toprule to "\\hline" set the midrule to "\\hline" set the bottomrule to "\\hline" end if if the floattype = "Floating" then set the beforetext to ¬ "\\begin{table}[htbp]" & return & ¬ indent & "\\centering" & return & ¬ indent & "\\begin{tabular}" -- note no carriage return to allow space for the column preamble set the aftertext to ¬ indent & indent & bottomrule & return & ¬ indent & "\\end{tabular}" & return & ¬ indent & "\\caption{TableCaption}" & return & ¬ indent & "\\label{tab:label}" & return & ¬ "\\end{table}" else set the beforetext to ¬ "\\begin{center}" & return & ¬ indent & "\\begin{tabular}" set the aftertext to ¬ indent & indent & bottomrule & return & ¬ indent & "\\end{tabular}" & return & ¬ "\\end{center}" end if set the column_preamble to "{@{} c" set the one_row_text to indent & indent & cell_text if NCOLS > 1 then repeat with c from 2 to NCOLS set the column_preamble to column_preamble & "c" set the one_row_text to one_row_text & "& " & cell_text end repeat end if set the column_preamble to column_preamble & " @{}}" & return set the one_row_text to one_row_text & "\\\\ " -- start off with a header row between two rules: set the midtext to ¬ indent & indent & toprule & return & ¬ one_row_text & return & ¬ indent & indent & midrule & return repeat with r from 1 to NROWS set the midtext to midtext & one_row_text & return end repeat set alltext to beforetext & column_preamble & midtext & aftertext if texapp = "TeXShop" then tell application "TeXShop" to set the content of the selection of the front document to alltext else if texapp = "iTeXMac" then --tell application "iTeXMac" to insert alltext in the text of the front document end if end tell -- 2004 Will Robertson -- (You may do what you like with this script) key T+ControlKey name New tabular content --Applescript -- NEW TABULAR -- This script inserts a new array environment with a user-specified number of rows and columns. You may choose to indent the array, since you'll be inserting it into a maths environment. -- CUSTOMISE TO YOUR LIKING: property indent : " " -- edit this if you want other than 2 spaces for your indent property cell_text : "• " -- edit this if you want a different text blob property default_array_location: "Indent" -- or "Flush left" -- THE SCRIPT: property texapp : "TeXShop" tell application texapp set q1 to display dialog "New maths array." & return & return & "Enter the number of rows you'd like." default answer "3" set NROWS to the text returned of q1 set q2 to display dialog "How many columns would you like? Would you like me to indent the array?" default answer "3" buttons {"Cancel", "Flush left", "Indent"} default button default_array_location set NCOLS to the text returned of q2 set indentyesno to the button returned of q2 if the indentyesno = "Indent" then set the preindent to indent else set the preindent to "" end if set the column_preamble to "{c" set the one_row_text to preindent & indent & cell_text if NCOLS > 1 then repeat with c from 2 to NCOLS set the column_preamble to column_preamble & "c" set the one_row_text to one_row_text & "& " & cell_text end repeat end if set the column_preamble to column_preamble & "}" & return set the one_row_text to one_row_text & "\\\\ " & return set midtext to "" repeat with r from 1 to NROWS set the midtext to midtext & one_row_text end repeat set the beforetext to preindent & "\\begin{array}" set the aftertext to preindent & "\\end{array}" set alltext to beforetext & column_preamble & midtext & aftertext if texapp = "TeXShop" then tell application "TeXShop" to set the content of the selection of the front document to alltext else if texapp = "iTeXMac" then --tell application "iTeXMac" to insert alltext in the text of the front document end if end tell -- 2004 Will Robertson -- (You may do what you like with this script) key A+ControlKey name New array content --Applescript -- ADD COLUMN --Select the tabular/array rows you wish to edit and run the script. The script will insert a new column at the specified location in each row. You'll have to adjust multicolumn rows and the column preamble (eg, the {ccc} part) manually. -- CUSTOMISE TO YOUR LIKING: property indent : " " property default_button : "After" -- or "Before" property cell_blob : "•" --THE SCRIPT: property texapp : "TeXShop" tell application texapp set q to display dialog "Add column." & return & return & "Please enter the column number before or after which you would like to add a new column." default answer "1" buttons {"Before", "After"} default button default_button set add_where to the button returned of q set col_num to the text returned of q if texapp = "TeXShop" then tell application "TeXShop" to set tabular to the content of the selection of the front document else if texapp = "iTeXMac" then --tell application "iTeXMac" to set tabular to (the selection of the text of the front document) end if set new_tabular to "" repeat with ii from 1 to the count of the paragraphs of tabular set this_line to paragraph ii of tabular if this_line contains "\\\\" and this_line does not contain "\\multicolumn" then --Transform " a & b & c \\" to "a & b & c": set trim_line to do shell script ¬ "echo " & the quoted form of this_line & ¬ " | sed 's/^[ ^t]*//' | sed 's/\\\\\\\\//'" -- "\\\\\\\\" is a TeX newline escaped in both Applescript and the shell set new_col to "& " & cell_blob & " &" if add_where = "After" then set line_start to do shell script "echo " & the quoted form of trim_line & " | cut -f -" & col_num & " -d '&'" set line_end to do shell script "echo " & the quoted form of trim_line & " | cut -f " & col_num + 1 & "- -d '&'" if line_end = "" then set new_col to "& " & cell_blob & " " end if else if add_where = "Before" then if col_num = "1" then set line_start to "" set new_col to cell_blob & " & " set line_end to trim_line else set line_start to do shell script "echo " & the quoted form of trim_line & " | cut -f -" & col_num - 1 & " -d '&'" set line_end to do shell script "echo " & the quoted form of trim_line & " | cut -f " & col_num & "- -d '&'" if line_end = "" then set new_col to "& " & cell_blob & " " end if end if end if set new_line to indent & indent & line_start & new_col & line_end & "\\\\" if new_tabular = "" then set new_tabular to new_line else set new_tabular to new_tabular & return & new_line end if else if new_tabular = "" then set new_tabular to this_line else set new_tabular to new_tabular & return & this_line end if end if end repeat if tabular contains "\\multicolumn" then display dialog "I cannot parse lines with “\\multicolumn” in them. You'll have to do them by hand. Sorry." buttons {"Cancel", "Proceed"} default button "Proceed" with icon caution end if if texapp = "TeXShop" then tell application "TeXShop" to set the selection of the front document to new_tabular else if texapp = "iTeXMac" then --tell application "iTeXMac" to insert new_tabular in the text of the front document end if display dialog "Don't forget to add a column to the column header!" buttons {"•"} giving up after 2 end tell -- 2004 Will Robertson -- (You may do what you like with this script) name Add column content --Applescript -- DELETE COLUMN --Select the tabular/array rows you wish to edit and run the script. The script will delete the specified column from each row (after a confirmation). You'll have to adjust multicolumn rows and the column preamble (eg, the {ccc} part) manually. -- CUSTOMISE TO YOUR LIKING: property indent : " " property warn_before_delete: true -- THE SCRIPT: property texapp : "TeXShop" tell application texapp set q to display dialog "Delete column." & return & return & "Please enter the number of the column you would like to delete." default answer "1" buttons {"Cancel", "Delete"} default button "Delete" set col_num to the text returned of q if texapp = "TeXShop" then tell application "TeXShop" to set tabular to the content of the selection of the front document else if texapp = "iTeXMac" then --tell application "iTeXMac" to set tabular to (the selection of the text of the front document) end if if tabular = "" then display dialog "Nothing selected. Please select the tabular/array rows you wish to edit." buttons "Cancel" default button 1 end if set sample_value to "" set new_tabular to "" repeat with ii from 1 to the count of the paragraphs of tabular set this_line to paragraph ii of tabular if this_line contains "\\\\" and this_line does not contain "\\multicolumn" then -- note "\\\\" means \\ because the backslashes are escaped --Transform " a & b & c \\" to "a & b & c": set trim_line to do shell script ¬ "echo " & the quoted form of this_line & ¬ " | sed 's/^[ ^t]*//' | sed 's/\\\\\\\\//'" -- "\\\\\\\\" is a TeX newline escaped in both Applescript and the shell set new_col to "&" if sample_value = "" then set sample_value to do shell script "echo " & the quoted form of trim_line & " | cut -f " & col_num & "-" & col_num & " -d '&'" end if if col_num = "1" then set line_start to "" set line_end to do shell script "echo " & the quoted form of trim_line & " | cut -f " & col_num + 1 & "- -d '&'" set new_col to "" else set line_start to do shell script "echo " & the quoted form of trim_line & " | cut -f -" & col_num - 1 & " -d '&'" set line_end to do shell script "echo " & the quoted form of trim_line & " | cut -f " & col_num + 1 & "- -d '&'" if line_end = "" then set new_col to "" end if end if set new_line to indent & indent & line_start & new_col & line_end & "\\\\" if new_tabular = "" then set new_tabular to new_line else set new_tabular to new_tabular & return & new_line end if else if new_tabular = "" then set new_tabular to this_line else set new_tabular to new_tabular & return & this_line end if end if end repeat if warn_before_delete then display dialog "Are you sure you want to delete the column that begins with “" & sample_value & "”?" buttons {"Cancel", "Delete"} default button "Delete" end if if tabular contains "\\multicolumn" then display dialog "I cannot parse lines with “\\multicolumn” in them. You'll have to do them by hand. Sorry." buttons {"Cancel", "Proceed"} default button "Proceed" with icon caution end if if texapp = "TeXShop" then tell application "TeXShop" to set the selection of the front document to new_tabular else if texapp = "iTeXMac" then --tell application "iTeXMac" to insert new_tabular in the text of the front document end if display dialog "Don't forget to remove a column from the column header!" buttons {"•"} giving up after 2 end tell -- 2004 Will Robertson -- (You may do what you like with this script) name Delete column content --Applescript -- "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 -- 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: 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 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" else set sort to "" end if -- glue 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) 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) 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 "\\" & ref_command & "{" & label_insert & "}" end if set the selection of the front document to ref_insert as string end if end tell key R+ShiftKey name Insert reference content -- Applescript -- 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 & this_filename) tell application "TeXShop" to open file_to_open end repeat end if key O+ShiftKey name Open quickly… content --Applescript -- CLEAN UP TeX FILES -- This script will delete all those extraneous files that you don't need after you've totally finished your document. -- It will look at every file in the current directory, and if it has one of the offending extensions, the file will be moved to the trash. -- Alternatively, you can choose only to look at files with the same base name as the front TeX document. -- Note that the files are only moved to the trash, so you can still retrieve them if you change your mind! -- CUSTOMISE TO YOUR LIKING: property extensions_to_delete : {"aux", "bbl", "blg", "brf", "glo", "idx", "ilg", "ind", "loa", "lof", "log", "lot", "mtc", "mlf", "out", "pdfsync", "toc"} property delete_files_only_with_same_name : true -- If your file is document.tex, setting this true will delete only document.aux, document.log, etc., but NOT otherdoc.aux, otherdoc.log, etc. -- Otherwise, ALL files with the extensions_to_delete will be deleted. property warn_before_delete : true -- Setting this true will bring up a prompt confirming which files you are about to delete. -- THE SCRIPT: tell application "TeXShop" to set texpath to the path of the front document -- the current tex file: "/path/to/docu.tex" set texloc to alias POSIX file texpath -- the applescript path: "path:to:docu.tex" tell application "TeXShop" tell application "Finder" set texfolder to the container of (texloc as alias) -- "path:to:" if delete_files_only_with_same_name then set texName to the name of (texloc as alias) -- "docu.tex" -- snip off the extension: (will fail if a containing folder contains a period!) set texName to do shell script "echo " & the quoted form of texName & " | cut -f 1 -d '.'" set texfiles to every file in texfolder whose name contains (texName & ".") -- "docu.aux", "docu.log", etc. else set texfiles to every file in texfolder end if end tell set files_to_delete to {} set filenames_to_delete to {} repeat with this_file in texfiles if (the name extension of this_file is in extensions_to_delete) then -- add the file to the list for deletion: set the end of files_to_delete to (this_file) -- add the NAME of the file to the list for -confirmation- of deletion: set the end of filenames_to_delete to " " & (the name of this_file as string) & return end if end repeat if warn_before_delete then display dialog "Are you sure you want to move the following files to the trash?" & return & filenames_to_delete as string buttons {"Keep", "Trash"} default button "Trash" with icon caution end if if the button returned of the result is "Trash" then tell application "Finder" to move every item of files_to_delete to the trash end if end tell -- 2004 Will Robertson -- (You may do what you like with this script) name Clean up TeX files