0

ご入力いただきありがとうございます。次のAppleScriptについて質問があります。同じコマンドを2回実行し、2つの異なる応答を取得しています。最初はunixコマンド(grep)で、2番目は純粋なAppleScriptです。teXドキュメントで文字列、つまり\begin{document}を見つけようとしています。次に、AppleScriptでこの文字列の前に\usepackage{whatever}を追加します。アクティブなウィンドウのファイルの場所をTeXShopからpythonに渡すことができず、AppleScriptのみを渡すことができないことを除いて、私が望むことを実行するpythonスクリプトがあります。

質問:UNIXバージョンが純粋なAppleScriptバージョンと異なるのはなぜですか?\ begin {document}は、私がチェックしているドキュメントに確実に含まれていることに注意してください。純粋なバージョンは正しく機能します。

tell application "TeXShop"
    -- get the front document and save
    set thisDoc to the front document
    -- get the filename and cursor position of the document
    get path of thisDoc
    set filePath to result
    --set insPoint to offset of selection of thisDoc
end tell
set searchResult to do shell script "grep -q \\begin{document}" & filePath & "; echo $?" --echo 0 on match found/success and 1 on not found
if searchResult = "0" then
    display dialog "string found"
else
    display dialog "string not found"
end if
set findThis to "\\begin{document}"
set theFileContent to read filePath
if result contains findThis then
    display dialog "string found"
else
    display dialog "string not found"
end if
4

3 に答える 3

1

シェルは特殊文字、この場合は円記号と中括弧の両方を解釈します。またgrep、AppleScript自体も円記号を解釈します。

set searchResult to do shell script "grep -q '\\\\begin{document}'" & filePath & "; echo $?"

一重引用符は中括弧を保護し、シェルが円記号を解析するのを防ぎます。AppleScriptはそれぞれ1つを食べ(同じ理由で、純粋なAppleScriptバージョンでは2つのバックスラッシュが必要)、もう1つを食べるためgrep(バックスラッシュは「次の文字をリテラルとして扱う」を意味します。ただし、GNUgrepでは「扱う」を意味する場合があります)。次のキャラクターは特別です」が、それはここでは起こりません)。

于 2012-04-11T19:21:53.543 に答える
0

実行している場合

grep \begin{document} <file>

バックスラッシュはシェルの特殊文字であるため、機能しません。試す:

grep \\begin{document} <file>
于 2012-04-11T19:16:03.797 に答える
0

アクティブなウィンドウのファイルの場所をTeXShopからpythonに渡すことができず、AppleScriptのみを渡すことができないことを除いて、私が望むことを実行するpythonスクリプトがあります。

ただし、 AppleScript使用して現在開いている.texファイルのパスを取得し、を使用do shell scriptしてそのパスを引数としてPythonスクリプトに渡すことができます。このようなもの(テスト済み、Pythonスクリプトがコマンドライン引数としてファイルパスを受け入れる場合はスムーズに動作します):

property thePythonScript : "/Users/user/path/to/script.py "
tell application "TeXShop"
    set thisDoc to the front document
    set filePath to (path of thisDoc)
    do shell script ("'" & thePythonScript & "' " & quoted form of filePath)    
end tell
于 2012-04-12T13:56:00.970 に答える