2

I have a folder, Books, where I add all of the .pdf files that need to be uploaded to a Kindle. I have a Python script that does that, but instead of manually running it, I want to check whenever files are added to Books, run that Python script. Is there any way to do this using Automator, or even without Automator?

1

Launch Automator, and choose 'Folder Action' as the document type. Then set the target folder in the drop-down menu at the top of the window.

Add a "Run Shell Script" action. You have to choose one of the 'shell' environments in the drop-down menu, which include either the system python /usr/bin/python (which is v2, and EOLed); or a Unix shell, where you can call the python script externally, with something like:

/usr/local/bin/python3 <path to python script> "$1"

Alternatively to using the Run Shell Script action, you could use the AppleScript action with a do shell script command.

Save your workflow. The workflow is saved to ~/Library/Scripts/Folder Action Scripts

You can configure Folder Actions for any folder by right-clicking in the Finder and selecting "Folder Actions Setup".

(I have had intermittent success hacking the saved workflow in a text editor, and altering the 'shell' path to another environment. /usr/bin/swift works, but /usr/local/bin/python doesn't. More work required.)

5
  • I have done everything you said, and I am running it from the zsh shell. When running the Python script from the Automator though, (pressing the run button at the top-right), a subprocess in the Python script fails to work. It says FileNotFoundError: [Errno 2] No such file or directory: 'ebook-convert': 'ebook-convert' ebook-convert is the command I am running in the subprocess. Is there any way to fix this? – Vishnu S. Mar 21 '20 at 15:26
  • Are you quoting the full path for this subprocess? From my testing, I wouldn't rely on any assumptions about relative paths. – benwiggy Mar 21 '20 at 16:02
  • I am quoting relative paths, but in my shell Automator script, I change directory to the required directory, I also just tried it with full paths and it didn't work. – Vishnu S. Mar 21 '20 at 16:09
  • Also, I changed all of the relative paths of absolute paths, and it continued to work when I executed it in the Terminal python3 kindle.py but now, in Automator, it gives error 127 when running the subprocess. – Vishnu S. Mar 21 '20 at 16:22
  • Hello! Thanks for the help! So I understood what was happening. For some reason, ebook-convert wouldn't work for the shell script, so I had to use the full path to the command, which was /Applications/calibre.app/Contents/MacOS/ebook-convert, then the arguments, which worked! – Vishnu S. Mar 21 '20 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .