Skip to content

Using git hooks to run Applescripts

Posted on:May 19, 2024

Today I had a strange issue and an even stranger solution.

IntelliJ seems to have recently completely lost the ability to remove git index caching without manual intervention. What this looks like is after I commit my changes, it just sits there with annotated git changes until I close the software and re-open it.

This bothered me, but I was limping on by for a while.

Until today I found out that running the “Refresh” command in IntelliJ would actually fix the issue.

This got me thinking—could I somehow automate running that after committing?

Sure enough, I found a way to do it:

#!/bin/bash
# .git/hooks/post-commit

# This script just refreshes IntelliJ's git indexes after a commit
/usr/bin/osascript <<EOF
tell application "IntelliJ IDEA"
  activate
  tell application "System Events"
    keystroke "r" using {command down, option down, control down}
  end tell
end tell
EOF

And inside IntelliJ, I set my keyboard binding for the “Refresh” command to Command-Option-Control-r

In the end, I ended up not loving the solution because it immediately pulled my focus away from the terminal which was a bit jarring. However, the pattern of using a git hook to trigger another automation is one I definitely may co-opt in the future!


Thanks for reading! If you enjoyed this, you may also enjoy following me on twitter!