1

Background: texdoc is a command installed by the LaTeX distribution MacTeX and which texdoc in the Terminal gives "/Library/TeX/texbin/texdoc".

In the Terminal, texdoc --version gives:

Texdoc 3.4.1 (2022-03-19).

But in AppleScript, via do shell script, this didn't work.

do shell script "/Library/TeX/texbin/texdoc --version" gives the error:

error "env: texlua: No such file or directory" number 127.

I don't understand why AppleScript speak about "texlua" not found and "env" (by the way which texlua gives "/Library/TeX/texbin/texlua").

How to call texdoc with do shell script?

Edit

In fact, not only I can not call directly texdoc --version, but after this is resolved by append /Library/TeX/texbin: to the path, a real usage case is still not working, eg do shell script "texdoc url", even if I append /Library/TeX/texbin: to the path. Applescript complaints that it does not find the open command (open is called by texdoc to display the pdf documentation. Very strange, because when called directly, eg do shell script "open /usr/local/texlive/2022/texmf-dist/doc/latex/url/url.pdf", this works.

So, after adding export PATH='/Library/TeX/texbin:$PATH';, the AppleScript code :

do shell script "export PATH='/Library/TeX/texbin:$PATH'; texdoc url"

outputs this error:

error "sh: open: command not found\rtexdoc error: Failed to execute: open "/usr/local/texlive/2022/texmf-dist/doc/latex/url/url.pdf"" number 1

So what is the complete method to call a binary like texdoc with AppleScript?

10
  • 2
    FWIW, the shell used here has a different environment than that of the Terminal. Try running env in the terminal and then run it in Script Editor with do shell script "env". Note especially the difference in PATH. Then try do shell script "which tex" and compare. For some explanation by apple, take a look at their tech note on the subject: TN2065. You can do most stuff, you just need to take this into account.
    – Mockman
    Commented Jan 28, 2023 at 9:04
  • 2
    /Library/TeX/texbin/texdoc is probably a shell script that depends on other scripts/programs in /Library/TeX/texbin. That directory is in your PATH in an interactive shell, but not in the shell that do shell script creates. Try do shell script "PATH="/Library/TeX/texbin:$PATH"; texdoc --version" Commented Jan 28, 2023 at 11:03
  • @Mockman Among differences, in the Terminal, env outputs PATH=/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin and by AppleScript, I obtain only PATH=/usr/bin:/bin:/usr/sbin:/sbin. For which tex I obtain /Library/TeX/texbin/tex in the Terminal and the error "The command exited with a non-zero status." number 1 in AppleScript. But why my command doesn't work when I append the complete path to the CLI command?
    – quark67
    Commented Jan 28, 2023 at 13:36
  • @GordonDavisson Yes your command command works (by replacing the inners "with '), thanks, but why doesn't my version works despite I have append the complete path to the command?
    – quark67
    Commented Jan 28, 2023 at 13:39
  • 1
    What do you get with this? do shell script "export PATH=\"/Library/TeX/texbin:$PATH\" ; echo $PATH" Your command doesn't include the export. I think the same path will be returned either way but I'm not sure that the path will be available to any subsequent commands without the export.
    – Mockman
    Commented Jan 28, 2023 at 17:42

1 Answer 1

3

The very first line of /Library/TeX/texbin/texdoc

$ head -1 /Library/TeX/texbin/texdoc
#!/usr/bin/env texlua

calls texlua and relies on env finding the command within PATH. As AppleScripts run with the default path (which doesn't contain /Library/TeX/texbin) this will fail with the error you see.

To solve, use

do shell script "PATH=/Library/TeX/texbin:$PATH texdoc --version"

instead which makes sure PATH includes your texbin directory.

6
  • 1
    Applescript doesn't understand single-quotes; you need to use double-quotes around the entire command, and escape the inner double-quotes with backslashes. Commented Jan 28, 2023 at 18:12
  • @GordonDavisson In fact, we can alsot invert ' and ". do shell script "PATH='/Library/TeX/texbin:$PATH'; texdoc --version" works. So we don't have to use \". But I have discovered that if with this trick, texdoc --version works, in real usage case, eg texdoc url, as this call the command open to display the PDF of the documentation, AppleScrip says "sh: open: command not found" (but used directly, open is found, eg do shell script "/usr/local/texlive/2022/texmf-dist/doc/latex/url/url.pdf" works. Why AppleScript don't find the path of "open" when called by "texdoc"?
    – quark67
    Commented Jan 28, 2023 at 19:19
  • @quark67 The version with single-quotes won't work because the shell won't expand $PATH when it's in single-quotes. The original PATH didn't include /Library/TeX/texbin, so it couldn't find programs there (other than the one you specifically pointed at); with single-quotes, PATH effectively only contains /Library/TeX/texbin, so it can't find programs anywhere else. It may or may not need to find programs in the usual places, but if it ever does it'll fail. Basically, you haven't noticed a problem with single-quotes, but that doesn't mean they actually work. Commented Jan 28, 2023 at 23:12
  • @GordonDavisson You are right, and yet it works. I wasn't aware of the difference between ' and ". So I accept the answer from nohillside who has provided a working solution (but a have not seen it before, because I believed that \" can be replaced with ').
    – quark67
    Commented Jan 28, 2023 at 23:25
  • 1
    @quark67 unless a path entry contains a space character, you need neither.
    – nohillside
    Commented Jan 29, 2023 at 6:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .