Can AppleScript execute a Python script

mail.apppdfpython

As a math grad student I often have undergrads emailing me homework to be graded. Some undergrads do send me homework in PDF format. On the other hand many don't and it is very tiresome to convert their homework in image files into PDFs manually either online or using my simple Python script using img2pdf.

I would like to be able to

  1. automatically download attachments that look like homework (i.e. the email has "HW", "Homework", "hw" or "Hw",

  2. and there exists images or PDFs as attachments)

  3. and then if images are obtained combine them into a single PDF using the Python script that I already have.

  4. Adding a new option in the Mail app that can convert attachments to PDF using my Python script will also be great.

I think AppleScript can handle the issue of saving attachments. Can it also execute a Python script?

Best Answer

To answer the question actually in your post (as opposed to the current question title), yes it's possible to execute a python script from within AppleScript.

However, you need to ensure you're not trying to run the script directly. Your AppleScript needs to use Python to run your script.

Example

Assuming your Python script is called img2pdf.py, you need to pass the path of your script as an argument from AppleScript to Python. Below is example code to show how you would do this if your script was saved in your Documents folder:

do shell script "python /Users/yourusername/Documents/img2pdf.py"

But, as I mentioned in my earlier comment, I would just make your requirements clear in advance (and include them in any assignment/essay/homework criteria) so your students know what they need to submit and how. My kids have to do this type of thing for their assessments, so I don’t think it’s too much for you to ask. Plus that’d make your job a lot easier! :)