Thanks in advance for your input. I have a question regarding the following AppleScript. I am running the same command twice and getting two different responses.First a unix command (grep) and second pure AppleScript. I am trying to find a string, namely \begin{document} in a teX document. I want to then add a \usepackage{whatever} before this string with AppleScript. I have a python script which does what I want except that I can’t pass the file location of the active window from TeXShop to python, only AppleScript.
Question: Why does the unix version differ from the pure AppleScript version? Keep in mind the \begin{document} is most certainly in the document I am checking. The pure version works correctly.
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
The shell interprets special characters, in this case both backslash and braces; and
grepand AppleScript itself also interpret backslashes.The single quotes protect the braces and prevent the shell from parsing the backslashes; you need 4 backslashes because AppleScript eats one of each (for the same reason you need two backslashes in the pure AppleScript version) and
grepeats the other (backslash means “treat the next character as literal”, except in GNUgrepwhere it sometimes means “treat the next character as special” but that doesn’t happen here).