1

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?

  • 1
    If I were you, I would set up automatic reply in Mail, saying they Homework will not be accepted if not in PDF format. – Ruskes Dec 16 '18 at 2:45
  • 1
    I have to admit my first thought was very similar to the suggestion by @Buscar웃 In my case I would just make your requirements clear in advance (and include them in any assignment/essay/homework criteria) so 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! :) – Monomeeth Dec 16 '18 at 3:06
  • Have to agree with Buscar and would add that it should be one pdf per email - they should do the joining together and provide a suitable name: I use student number as that is unique per student... – Solar Mike Dec 16 '18 at 6:31
1

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! :)

  • Really thanks! For the purpose of learning AppleScript for lolz I decide to write a script any way. – Ying Zhou Dec 17 '18 at 19:33
1

This is what Automator Services are for. Launch Automator, and create a Service (aka Quick Actions in Mojave).

  1. Set the options so that it received Image files in Mail.app.
  2. Add the "Run Shell Script" option.
  3. Set the shell drop-down to "usr/bin/python".
  4. Set "pass input" drop-down to "as arguments". It will help you out with a tiny bit of python, which includes the arguments (filenames of the attachments).

Add your python code, which will need to save the file to a known folder (or, for extra credit, use pyObjC to bring up a file dialog.)

You can then right-click on these attachments in Mail, and select the Service.

You must log in to answer this question.

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