Executing Automation Workflows with Claude Code
Claude Code is one of the best AI agents available. One of the reasons for this is its ability to plan and dynamically adapt. This ability is a generic skill that can be applied in other areas as well. One such area is automation, but instead of writing automation scripts or describing it in plain English, we can use workflow diagrams. This idea was inspired by the "The Dynamic Context Problem" post from Carta.
A workflow diagram is an ideal tool for communication, particularly among individuals from diverse domains. They combine visual and algorithmic elements, which makes them easy to change and verifiable. Additionally, workflow diagrams can be represented as text, which we need to process using LLMs. The plan is to create a workflow diagram in UI-Builder and have Claude Code execute it using text representation. I'll use Mermaid, a text-based diagramming tool that allows editing diagrams both in the UI editor and in text.
flowchart TD
A(["Start"]) --> n1["Ask user to enter a number"]
n1 --> E{"if the input number is >1?"}
E -- No --> H@{ label: "Show to user message box with text 'Small number'" }
H --> n2(["End"])
n3@{ label: "Show to user message box with text 'Big number'" } --> n4(["End"])
E -- Yes --> n3
The first automation is a UI screenshot analyser. This workflow reads all screenshots in the folder. If it finds a UI bug, it moves it to ui-bugs folder. If it finds non-English texts, it moves a screenshot into the translation-bugs folder. In each case, it creates a text file with a bug description.
I implemented this as a Claude Code command - analyse-screenshots. This command is executed like this: claude -p "/analyze-screenshots".
With all of this, we've managed to achieve a new approach to automation. The automation workflow is defined as Mermaid diagram, converted to text, which simplifies its understanding and future changes. The execution is initiated in a single command and does not require human intervention. But what if we need a human intervention? What if the script requires user input?
Adding a user input is simple. We add a new node in the workflow - ask the user. The trick is to provide UI for this input. Here is where osascript command tool on Mac helps. This tool allows you to execute AppleScript. Here is an example of a confirmation window:
osascript -e 'display dialog "Continue?" buttons {"Yes", "No"} default button "Yes"'.
Now we need to update our prompt to instruct Claude Code on how to use osascript in different situations, such as confirmation boxes, input requests, and multi-select choices. This is implemented in /analyze-any-folder command. This Claude Code command lists all folders in the parent and asks users which one to process via an osascript UI dialogue. claude -p "/analyze-any-folder"
The last experiment is /play-game command. This is a simple game where the user inputs a number and the script shows different messages. The point of this experiment is to understand how many if blocks Claude Code can support. 10 if blocks are not a problem for Claude Code. However, in this case, Claude Code generates a Bash script based on the provided workflow.
Resources
Blog: https://www.firstround.com/ai/ - The Dynamic Context Problem
Tool: https://www.mermaidchart.com/ - Diagramming tool
Github: https://github.com/shchahrykovich/cc-automation - Automation examples