Run a Gradle build when you save in BBEdit

My blog is a static website which I generate with a Gradle build. Gradle builds the website if I run:

cd ~/github/sdelamo/;./gradlew build

My posts are Markdown files under ~/github/sdelamo/

I want BBEdit to run my Gradle build when I save a Markdown file in my blog's folder. Thanks to BBEdit's AppleScript integration it is possible.

I created an AppleScript in the Attachment Scripts folder:

The Attachment Scripts folder contains AppleScripts which are run at specific points: when BBEdit starts or quits; and when documents are open, saved, and closed.

I saved the following AppleScript script at ~/Library/Application Support/BBEdit/Attachment Scripts/Document.documentDidSave.scpt.

It runs when I save a blog post's Markdown file:

on documentDidSave(doc)
    set p to file of doc
    if (p as text) ends with ".md" and (p as text) contains "" then
        set buildScript to "cd ~/github/sdelamo/;./gradlew build"
        tell application "Terminal"
            if (exists window 1) and not busy of window 1 then
                do script buildScript in window 1
                do script buildScript
            end if
        end tell
    end if
end documentDidSave

This script allows me to focus on writing.

Tags: #automation #bbedit #gradle #applescript