Skim の displayline をバックグラウンドで

LaTeX 用のPDFプレビュア Skim.app には、行番号を指定して開く displayline というスクリプトがある(Skim.app/Contents/SharedSupport/displayline)。これに、エディタにフォーカスしたまま背景で displayline を実行する -g (-background) という新たなオプションを追加してみた。Apple script の部分の activate という命令を条件分岐しただけ。しばらく使ってみて便利だと思えばメーリングリストなどから提案してみる。

追記:MLで、同じ話題が来たので -g オプションのスクリプトを投稿しておいた。忘れられなければ正式に採用してもらえると思う。

追記:同じ要望が既に一度却下されていた。採用してもらえるかな?

追記:1.3.11 に -g オプションが追加された!でも 1.3.11 は自動更新を毎回聞いてくるというバグがあり、開発者も認識しているので次を待った方がよいかも。1.3.11 からは日本語リソースも追加されている。

それにしても Apple Script の部分の最後のところはコメントかと思った。自分ですらすら書けるような気がしないな。


New option "-g" is added in order not to bring Skim.app to the foreground. I may post this change to the official mailing list.

#!/bin/bash

# displayline (Skim)
#
# Usage: displayline [-r] [-b] [-g] LINE PDFFILE [TEXSOURCEFILE]

if [ $# == 0 -o "$1" == "-h" -o "$1" == "-help" ]; then
    echo "Usage: displayline [-r] [-b] [-g] LINE PDFFILE [TEXSOURCEFILE]"
    exit 0
fi

# get arguments
revert=0
bar=0
activate=1

while [ "${1:0:1}" == "-" ]; do
    if [ "$1" == "-r" -o "$1" == "-revert" ]; then
	revert=1;
    elif [ "$1" == "-b" -o "$1" == "-readingbar" ]; then
	bar=1
    elif [ "$1" == "-g" -o "$1" == "-background" ]; then
	activate=0
    fi
    shift
done
line=$1
file="$2"
[ $# -gt 2 ] && source="$3" || source="${file%.pdf}.tex"

# expand relative paths
[ "${file:0:1}" == "/" ] || file="${PWD}/${file}"
[ "${source:0:1}" == "/" ] || source="${PWD}/${source}"

# pass arguments as NULL-separated string to osascript
# pass through cat to get them as raw bytes to preserve non-ASCII characters
echo -ne "${line}\x00${file}\x00${source}\x00${revert}\x00${bar}\x00${activate}\x00" | \
    /usr/bin/osascript \
    -e "set theArgv to do shell script \"/bin/cat\"" \
    -e "set AppleScript's text item delimiters to ASCII character 0" \
    -e "set {theLine, theFile, theSource, shouldRevert, shouldShowBar, shouldActivate} to text items of theArgv" \
    -e "set theLine to theLine as integer" \
    -e "set theFile to POSIX file theFile" \
    -e "set theSource to POSIX file theSource" \
    -e "set shouldRevert to (shouldRevert as integer) as boolean" \
    -e "set shouldShowBar to (shouldShowBar as integer) as boolean" \
    -e "set shouldActivate to (shouldActivate as integer) as boolean" \
    -e "set thePath to POSIX path of (theFile as alias)" \
    -e "tell application \"Skim\"" \
    -e "  if shouldActivate then activate" \
    -e "  if shouldRevert then" \
    -e "    try" \
    -e "      set theDocs to get documents whose path is thePath" \
    -e "      if (count of theDocs) > 0 then revert theDocs" \
    -e "    end try" \
    -e "  end if" \
    -e "  open theFile" \
    -e "  tell front document to go to TeX line theLine from theSource showing reading bar shouldShowBar" \
    -e "end tell" -