Create script that attaches to menu command 'Run' in #!

65 views
Skip to first unread message

Venkat

unread,
Jul 15, 2021, 7:44:53 PM7/15/21
to BBEdit Talk
Hello, I'm relatively new to scripting, and need some help writing up a script that may need extension in the future. I write and compile LaTeX documents in BBEdit, and my workflow is  to create a build.sh (in the same folder) file with the following code that's enough to compile the latex code in the folder:

#!/bin/sh
latexmk -xelatex -verbose -pv

I was wondering if it was possible to create a script (stored in scripts folder) that is called every time I hit the keys Cmd+R together, when the frontmost open text file is a .tex file. In the future, I hope to extend this script for other filetypes. Anyone have any tips or a template script I can work with? 

jj

unread,
Jul 16, 2021, 3:49:35 AM7/16/21
to BBEdit Talk
Hi Venkat,

This applescript might do what you want if I correctly understood your workflow:

```applescript
try
tell application "BBEdit"
tell first window
tell first document
if not (its on disk) then
error "BBEdit's front document is not saved on disk."
end if
set vName to (its name) as string
set vLanguage to (its source language) as string
ignoring case
if not (vName ends with ".tex" or vLanguage is "TeX") then
error "BBEdit's front document is not a TeX document."
end if
end ignoring
set vFile to its file
end tell
end tell
set vFilePath to POSIX path of vFile
set vDirectoryPath to do shell script "dirname" & space & the quoted form of vFilePath
set vScriptPath to vDirectoryPath & "/build.sh"
do shell script the quoted form of vScriptPath
end tell
on error aMessage
display alert aMessage as critical
end try
```

Name it and copy it to BBEdit's Scripts folder and in the menu Window > Palettes > Scripts palette you can assign it a keyboard shortcut.

HTH

Jean Jourdain

Venkat

unread,
Jul 16, 2021, 9:31:55 AM7/16/21
to BBEdit Talk
Thank you! This seems to be what I'm after, however I'm running into a permissions issue, with this being the error message I get: sh: /Users/venkgvi/Desktop/talk/build.sh: Permission denied

I'm not sure why I'm getting this message, since running build.sh directly from the #! menu works fine. Do I need to add permissions somewhere in the AppleScript? Also is it possible to include the contents of build.sh directly in the AppleScript script? So instead of:

set vScriptPath to vDirectoryPath & "/build.sh"

Can I write the contents of build.sh directly in the script? I was wondering if I can skip making an extra shell script file in my project folder.

jj

unread,
Jul 16, 2021, 11:16:27 AM7/16/21
to BBEdit Talk
Either, make sure build.sh has execution permissions.
  • in terminal run:

    sudo chmod ug+x /Users/venkgvi/Desktop/talk/build.sh
    
or 

  • execute the command directly from the script:

    ... 
    set vDirectoryPath to do shell script "dirname" & space & the quoted form of vFilePath
    set vCommand to "cd" & space & (the quoted form of vDirectoryPath) & space & ";"
    set vCommand to vCommand & "PATH='/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin'" & space 
    set vCommand to vCommand & "latexmk -xelatex -verbose -pv"
    do shell script vCommand

  If you get an error about latexmk not being found, check where latexmk is on your setup.
  In terminal run:
    
    which latexmk

  and add the the latexmk directory to the PATH or put the fully qualified path to latexmk in the command.

Patrick Woolsey

unread,
Jul 16, 2021, 12:29:20 PM7/16/21
to bbe...@googlegroups.com
On 7/16/21 at 9:30 AM, gvenka...@gmail.com (Venkat) wrote:

> Thank you! This seems to be what I'm after, however I'm running into a
> permissions issue, with this being the error message I get: *sh:
> /Users/venkgvi/Desktop/talk/build.sh: Permission denied*
>
> I'm not sure why I'm getting this message, since running build.sh directly
> from the #! menu works fine. [...]

Please see:

https://www.barebones.com/support/bbedit/faqs.html#quarantine

and its associated tech note:

https://www.barebones.com/support/bbedit/quarantine.html

:-)


Regards

Patrick Woolsey
==
Bare Bones Software, Inc. <https://www.barebones.com/>

Venkat

unread,
Jul 16, 2021, 12:47:06 PM7/16/21
to BBEdit Talk
Thank you!
Reply all
Reply to author
Forward
0 new messages