#!/bin/bash # # texshop: Script for opening a file in TeXShop in OS X and positioning # the cursor at a particular line. If the file is already open # in an existing Vim, the cursor will be repositioned # re-opening the file. # # The script is meant to extend the inverse searching (c.f. # PDFSync compatible PDF viewers, like Skim or TeXniscope) to # MacVim. # # Version: 1.01 # # Author: Ted Pavlic # ted@tedpavlic.com # http://www.tedpavlic.com/ # # Usage: texshop [-h] [-a] [-o] [-r] "%file" [%line] # # where %file is a TeX file name and %line is a line number. The # -a option prevents activating TeXShop. The -o option prevents # opening the file (i.e., only refreshing and repositioning). The # -r option prevents refreshing the text. The -h option shows # help text. # # For more information (and history), see: # # http://phaseportrait.blogspot.com/2007/07/using-skim-pdfsync-and-texshop.html # # Version history: # # 1.0 : 07/17/2007 - Initial release (Ted Pavlic) # 1.01 : 07/20/2007 - Added -aorh options and help text. Also added file name guessing. (Ted Pavlic) # function do_usage { echo "Usage: texshop [-help] [-a] [-o] [-r] FILE [LINE]" >&2 } function do_help { do_usage echo "Open/Refresh TeX file in TeXShop. Optionally, position at LINE." >&2 echo "" >&2 echo -e " -a,\t\t Do NOT activate TeXShop." >&2 echo -e " -o,\t\t Do NOT open the file (i.e., only refresh/reposition)." >&2 echo -e " -r,\t\t Do NOT refresh the file." >&2 echo -e " -h,\t\t Show this help text." >&2 echo "" >&2 } # Usage if nothing passed if [ "$1" == "" ]; then do_usage exit 1 fi # Process options. Give usage if necessary activateString="1" openString="1" refreshString="1" while getopts aorh o; do case "$o" in a) activateString="";; # Disable "activate" o) openString="";; # Disable "open" r) refreshString="";; # Disable "refreshtext" h) do_help exit 1;; [?]) do_usage exit 1;; esac done shift $(( ${OPTIND}-1 )) fileName="$1" lineNumber="$2" gotoString="" [ "${fileName:0:1}" == "/" ] || fileName="${PWD}/${fileName}" [ "${lineNumber}" == "" ] || gotoString="goto line ${lineNumber}" # If filename doesn't exist, try adding extensions; exit if we can't # figure it out if [ ! -f "${fileName}" ]; then if [ -f "${fileName}.tex" ]; then fileName="${fileName}.tex"; elif [ -f "${fileName}tex" ]; then fileName="${fileName}tex"; else echo "File not found ($1)." >&2 exit 1 fi fi [ "${activateString}" != "" ] && activateString="activate" [ "${openString}" != "" ] && openString="open texFile" [ "${refreshString}" != "" ] && refreshString="refreshtext" exec osascript \ -e "set texFile to POSIX file \"${fileName}\"" \ -e "tell application \"TeXShop\"" \ -e "${activateString}" \ -e "${openString}" \ -e "tell front document" \ -e "${refreshString}" \ -e "${gotoString}" \ -e "end tell" \ -e "end tell"