Tutorial: How to automatically run your scripts on your computer

Auto

To know how to code can save you a crazy amount of time when you are a reporter, especially for repetitive tasks.

Some scripts are meant to be used once (to sort a database or to extract data from a pdf file for example). But others are made to run every week, every day or even every hour (like a script that monitors a specific webpage).

Online services, like ScraperWiki, can host and run your scripts on a regular basis.

However, it’s also possible to automatically run your script directly from your computer, with a chmod command and Automator!

Note: This tutorial is for Mac users.

For this example, I’ll use a script that my wonderful friend Allie Jaynes (who works for Al Jazeera +) and I coded for fun. This small Python program imitates the Magic 8 Ball, which is supposed to predict the future!

Let’s imagine that I need to ask a question to my Magic 8 Ball each morning. However, since I’m often forgetting to do so, I would like the program to launch on its own.

No problem! We’re going to create an executable file that will be opened by Automator, and triggered by the Calendar app!

Let’s start by creating a new text file on the desktop, with TextEdit, with two lines:

  • The first line is to tell the computer that it’s a shell file that has to be opened in Terminal.
  • The second line is the langage of your script and the path to it.

Now, let’s rename your text file and remove the .txt extension at the end.

Then open Terminal and type the chmod 755 command, followed by the file path.

chmod 755

There we go! You just transformed the file into an executable, that opens in Terminal and runs your script!

It’s a shortcut. Instead of opening Terminal, typing “python”, the path of the script and then pressing Enter, you can now simply double-click on your new file.

Now that this is done, open Automator and choose “Application”.

Automator action

Drag-and-drop your executable you just created. Add the action to open files. It should look like this:

Automator ouvrir fichier

Save what you did. It will give you an .app file.

application sauvegarde

All you have to do now is to open Calendar and to create a new event. Modify it so it will repeat itself as many times as you want.

Click on “Alert” and choose “Custom…”, then “Open a file”.

Click on “Others” and select your executable file. Don’t forget to select “At time of event” as well.

calendar

Voilà! Your script will run automatically, at the time of your choice!

I am sure there’re other ways to do this. But I really like this method because it’s simple, fast, without too much code and it uses softwares that come with OS X.

Moreover, you can check on your calendar the running time of your scripts. You won’t forget them and can easily manage them as well.

But let me know if there’s an easier way to do so!

 

2 thoughts on “Tutorial: How to automatically run your scripts on your computer

  1. Jeroen Meijer

    Hi,

    Very helpful for the general direction. The script did not work for me. After 3 hours of puzzling, I found out that the full path to the python start link is required. Normally, in the interactive shell, it is enough to type e.g.
    python3 test.py
    but in the script the full path is required, e.g.
    /usr/local/bin/python3 test.py

    Thanks!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *