Thanks everyone for contributing. I ended up solving the problem using the custom action from Execute a Script in Terminal - #5 by Tom
Here is the working script if anyone else struggled:
Thanks everyone for contributing. I ended up solving the problem using the custom action from Execute a Script in Terminal - #5 by Tom
Here is the working script if anyone else struggled: