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/sergiodelamo.com/;./gradlew build

My posts are Markdown files under ~/github/sdelamo/sergiodelamo.com/posts

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 "github:sdelamo:sergiodelamo.com" then
        set buildScript to "cd ~/github/sdelamo/sergiodelamo.com/;./gradlew build"
        tell application "Terminal"
            if (exists window 1) and not busy of window 1 then
                do script buildScript in window 1
            else
                do script buildScript
            end if
        end tell
    end if
end documentDidSave

This script allows me to focus on writing.

Tags: #automation #bbedit #gradle #applescript
Jul 2021, 28.

 

My next events:
🗓 Jul 20 09:00 Workshop Security in the Micronaut Framework
🗓 Sep 20 09:00 JCON Online 2022 - Getting Started with the Micronaut Framework
🗓 Sep 23 09:00 JCON Online 2022 - Micronaut® Framework and Microstream Java-native persistence engine