I am using a script which is basically identical to the first one in https://stackoverflow.com/a/4783182/7238575. It works perfectly when run in the terminal or using the op