<?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>name</key>
			<string>Column macros</string>
			<key>submenu</key>
			<array>
				<dict>
					<key>content</key>
					<string>--Applescript direct

-- 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" &amp; return &amp; return &amp; "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 theNCOLS 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 theNROWS 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]" &amp; return &amp; ¬
			indent &amp; "\\centering" &amp; return &amp; ¬
			indent &amp; "\\begin{tabular}" -- note no carriage return to allow space for the column preamble
		set the aftertext to ¬
			indent &amp; indent &amp; bottomrule &amp; return &amp; ¬
			indent &amp; "\\end{tabular}" &amp; return &amp; ¬
			indent &amp; "\\caption{TableCaption}" &amp; return &amp; ¬
			indent &amp; "\\label{tab:label}" &amp; return &amp; ¬
			"\\end{table}"
	else
		set the beforetext to ¬
			"\\begin{center}" &amp; return &amp; ¬
			indent &amp; "\\begin{tabular}"
		set the aftertext to ¬
			indent &amp; indent &amp; bottomrule &amp; return &amp; ¬
			indent &amp; "\\end{tabular}" &amp; return &amp; ¬
			"\\end{center}"
	end if
	
	set the column_preamble to "{@{} c"
	set the one_row_text to indent &amp; indent &amp; cell_text
	
	if theNCOLS &gt; 1 then
		repeat with c from 2 to theNCOLS
			set the column_preamble to column_preamble &amp; "c"
			set the one_row_text to one_row_text &amp; "&amp; " &amp; cell_text
		end repeat
	end if
	
	set the column_preamble to column_preamble &amp; " @{}}" &amp; return
	set the one_row_text to one_row_text &amp; "\\\\ "
	
	-- start off with a header row between two rules:
	set the midtext to ¬
		indent &amp; indent &amp; toprule &amp; return &amp; ¬
		one_row_text &amp; return &amp; ¬
		indent &amp; indent &amp; midrule &amp; return
	
	repeat with r from 1 to theNROWS
		set the midtext to midtext &amp; one_row_text &amp; return
	end repeat
	
	set alltext to beforetext &amp; column_preamble &amp; midtext &amp; 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)</string>
					<key>key</key>
					<string>T+ControlKey</string>
					<key>name</key>
					<string>New tabular</string>
				</dict>
				<dict>
					<key>content</key>
					<string>--Applescript direct

-- 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." &amp; return &amp; return &amp; "Enter the number of rows you'd like." default answer "3"
	set theNROWS 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 theNCOLS 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 &amp; indent &amp; cell_text
	
	if theNCOLS &gt; 1 then
		repeat with c from 2 to theNCOLS
			set the column_preamble to column_preamble &amp; "c"
			set the one_row_text to one_row_text &amp; "&amp; " &amp; cell_text
		end repeat
	end if
	
	set the column_preamble to column_preamble &amp; "}" &amp; return
	set the one_row_text to one_row_text &amp; "\\\\ " &amp; return
	
	set midtext to ""
	repeat with r from 1 to theNROWS
		set the midtext to midtext &amp; one_row_text
	end repeat
	
	set the beforetext to preindent &amp; "\\begin{array}"
	set the aftertext to preindent &amp; "\\end{array}"
	
	set alltext to beforetext &amp; column_preamble &amp; midtext &amp; 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)</string>
					<key>key</key>
					<string>A+ControlKey</string>
					<key>name</key>
					<string>New array</string>
				</dict>
				<dict>
					<key>content</key>
					<string>--Applescript direct

-- 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." &amp; return &amp; return &amp; "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 &amp; b &amp; c \\" to "a &amp; b &amp; c":
			set trim_line to do shell script ¬
				"echo " &amp; the quoted form of this_line &amp; ¬
				" | sed 's/^[ ^t]*//' | sed 's/\\\\\\\\//'"
			-- "\\\\\\\\" is a TeX newline escaped in both Applescript and the shell
			
			
			set new_col to "&amp; " &amp; cell_blob &amp; " &amp;"
			if add_where = "After" then
				set line_start to do shell script "echo " &amp; the quoted form of trim_line &amp; " | cut -f -" &amp; col_num &amp; " -d '&amp;'"
				set line_end to do shell script "echo " &amp; the quoted form of trim_line &amp; " | cut -f " &amp; col_num + 1 &amp; "- -d '&amp;'"
				if line_end = "" then
					set new_col to "&amp; " &amp; cell_blob &amp; " "
				end if
			else if add_where = "Before" then
				if col_num = "1" then
					set line_start to ""
					set new_col to cell_blob &amp; " &amp; "
					set line_end to trim_line
				else
					set line_start to do shell script "echo " &amp; the quoted form of trim_line &amp; " | cut -f -" &amp; col_num - 1 &amp; " -d '&amp;'"
					set line_end to do shell script "echo " &amp; the quoted form of trim_line &amp; " | cut -f " &amp; col_num &amp; "- -d '&amp;'"
					if line_end = "" then
						set new_col to "&amp; " &amp; cell_blob &amp; " "
					end if
				end if
			end if
			
			set new_line to indent &amp; indent &amp; line_start &amp; new_col &amp; line_end &amp; "\\\\"
			if new_tabular = "" then
				set new_tabular to new_line
			else
				set new_tabular to new_tabular &amp; return &amp; new_line
			end if
		else
			if new_tabular = "" then
				set new_tabular to this_line
			else
				set new_tabular to new_tabular &amp; return &amp; 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)</string>
					<key>name</key>
					<string>Add column</string>
				</dict>
				<dict>
					<key>content</key>
					<string>--Applescript direct

-- 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." &amp; return &amp; return &amp; "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 &amp; b &amp; c \\" to "a &amp; b &amp; c":
			set trim_line to do shell script ¬
				"echo " &amp; the quoted form of this_line &amp; ¬
				" | sed 's/^[ ^t]*//' | sed 's/\\\\\\\\//'"
			-- "\\\\\\\\" is a TeX newline escaped in both Applescript and the shell
			
			set new_col to "&amp;"
			if sample_value = "" then
				set sample_value to do shell script "echo " &amp; the quoted form of trim_line &amp; " | cut -f " &amp; col_num &amp; "-" &amp; col_num &amp; " -d '&amp;'"
			end if
			if col_num = "1" then
				set line_start to ""
				set line_end to do shell script "echo " &amp; the quoted form of trim_line &amp; " | cut -f " &amp; col_num + 1 &amp; "- -d '&amp;'"
				set new_col to ""
			else
				set line_start to do shell script "echo " &amp; the quoted form of trim_line &amp; " | cut -f -" &amp; col_num - 1 &amp; " -d '&amp;'"
				set line_end to do shell script "echo " &amp; the quoted form of trim_line &amp; " | cut -f " &amp; col_num + 1 &amp; "- -d '&amp;'"
				if line_end = "" then
					set new_col to ""
				end if
			end if
			
			
			set new_line to indent &amp; indent &amp; line_start &amp; new_col &amp; line_end &amp; "\\\\"
			if new_tabular = "" then
				set new_tabular to new_line
			else
				set new_tabular to new_tabular &amp; return &amp; new_line
			end if
		else
			if new_tabular = "" then
				set new_tabular to this_line
			else
				set new_tabular to new_tabular &amp; return &amp; 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 “" &amp; sample_value &amp; "”?" 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)</string>
					<key>name</key>
					<string>Delete column</string>
				</dict>
			</array>
		</dict>
	</array>
</dict>
</plist>
