MacOS – Git pre-commit hook that checks for open application

applescriptgitmacos

I am using git to manage a project that has a few binary files that need to be closed before being committed. So I need a git hook, that checks to see if the application that has these files open is running.

Here is the script I am using

#!/usr/bin/env osascript

tell application "System Events"
    set apps to the name of every process whose background only is false
end tell

if "Some App" is in apps then
    error "Some App is running. Can't commit until Some App is quit" number 5
end if

But I am getting this error when I commit or execute the pre-commit. Even when "Some App" is not running.

.git/hooks/pre-commit:50:106: execution error: An error of type -10810 has occurred. (-10810)

Permissions on the file are

-rwxr-xr-x@ 1 toddgeist  staff   263 Dec  7 07:33 pre-commit

Best Answer

You don't say what problem you're running into, so let me guess.

If you're having trouble with how to phrase the "-- show a message and exit with non zero status" part, use the error command:

#!/usr/bin/env osascript

tell application "System Events"
    set apps to the name of every process whose background only is false
end tell

if "Some Application" is in apps then
    error "Some Application is running" number 5
end if

The number (5 in the example) doesn't matter; osascript will exit with status 1 no matter what number you choose. You don't have control over the formatting of the message, but the non-zero exit status will block the commit.

If you're having trouble getting the pre-commit hook to even run, remember to save it as .git/hooks/pre-commit and make it executable.

(update) if you get the -10810 error try doing it with out System Events. Like this

#!/usr/bin/env osascript

set someApp to "Some App"
set appIsRunning to false
if application someApp is running then
    set appIsRunning to true
end if
if appIsRunning then
    error "Can't commit. Please quit Some App" number 5
end if