Automator for Mac OS X: Tutorial and Examples

Learn how to use Automator for Mac OS X to automate tedious workflows in this tutorial with five complete examples. By Neil North.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 5 of this article. Click here to view the first page.

The “View Results” Action

So far you’ve made a lot of assumptions based on what you believe the current variables being passed to the output to be. If you’re working on much bigger workflows, or need instant feedback of what the current output is then this action is useful.

Drag the View Results action from the library onto your workflow under Execute SQL. You don’t need to put anything into this box as just a place to display information.

Click the Run button at the top right to see what happens.
Screen Shot 2013-12-13 at 8.42.51 am

You can now see the results of the SQL query in plain text.

Feel free to experiment with the View Results action to fully understand what the output of each action is. For example if you were to drag it above Execute SQL, the result would be something like:

(
"/Users/northy/Desktop/DataStore.sqlite"
)

The output of the Get Specified Finder Items action will be the filepaths of all files included in the action.

Note: Use with care, as it’s not unusual for View Results to change the format of a string before it reaches output. It’s also possible to just use the Results section at the bottom of each action.

Making a standalone application to add data from CSV to Database

There may be occasions where you need to add sample data to a database or multiple databases — like when you’re making a demo or testing a new app. You could use a Folder Action to handle this, but the sample project requires you to work with multiple files of different natures, specifically a CSV file and a SQLite database. This can become confusing when using a Folder Action, so for this lesson you’re going to make an Application.

Applications are just like every other application in your applications folder. They are single files which can take an input by drag and dropping a file or folder onto them:

dragndrop

The application you’re going to make takes a CSV file as its input (found in the sample files provided), and adds the data to the SQLite database you were just working with.

You’ll start by creating a table in the database for your sample data. The idea is your app will be a names database for smart people, so you need a table to store names. You’ll create the table by running a SQL create table query in the workflow you just created.

Replace the SQL in the Execute SQL action with the following:

<span style="color: #993333; font-weight: bold;">create</span> <span style="color: #993333; font-weight: bold;">table</span> people<span style="color: #66cc66;">(</span>firstname varchar<span style="color: #66cc66;">,</span> lastname varchar<span style="color: #66cc66;">)</span>;
<span style="color: #993333; font-weight: bold;">create</span> <span style="color: #993333; font-weight: bold;">table</span> people<span style="color: #66cc66;">(</span>firstname varchar<span style="color: #66cc66;">,</span> lastname varchar<span style="color: #66cc66;">)</span>;

This query will create your table with the name people and will contain two fields: first name and last name.

The feedback you get in your view results action won’t tell you much. The green tick means there wasn’t any errors, so you can assume the table was created correctly:
Screen Shot 2013-12-13 at 4.35.31 pm

Now that you have added the People table to your database, you can create an application to automate adding CSV data.

In Automator, go to File then New from the menu bar. This time, select Application.

The file you’re going to pass into the application is a CSV file (comma delimited data files). They are text files where each line represents a new row of data and a comma separates each field. The one located in the sample file looks something like this:
Screen Shot 2013-12-13 at 4.46.10 pm

The CSV file will pass in as the first output to connect to. Next, you need to add a few actions to format the text, convert it to SQL and store it in a variable.

The first action to add is Combine Text Files:
Screen Shot 2013-12-13 at 4.51.38 pm

This is necessary in case you drag multiple CSV files into the application; it will loop through the contents of each text file and combine everything into one. Also, the file you drag into the application passes in its file path, so you want to make sure you’re getting the file’s contents.

Next, drag onto the workflow a Convert CSV to SQL action. You’ll see a segmented control which allows you to edit the text before, per row and after. Enter the following SQL on each:

Before:

begin transaction;
begin transaction;

Per Row:

<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> people <span style="color: #66cc66;">(</span>firstname<span style="color: #66cc66;">,</span> lastname<span style="color: #66cc66;">)</span> <span style="color: #993333; font-weight: bold;">values</span> <span style="color: #66cc66;">(</span><span style="color: #ff0000;">"%{1}@"</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">"%{2}@"</span><span style="color: #66cc66;">)</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> people <span style="color: #66cc66;">(</span>firstname<span style="color: #66cc66;">,</span> lastname<span style="color: #66cc66;">)</span> <span style="color: #993333; font-weight: bold;">values</span> <span style="color: #66cc66;">(</span><span style="color: #ff0000;">"%{1}@"</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">"%{2}@"</span><span style="color: #66cc66;">)</span>;

After:

end transaction;
end transaction;

You have now written SQL instructions to insert rows into your table for each line in the CSV file, or, combined CSV data.

If you were to run this action on the sample names.CSV file, the resulting SQL will be as follows:

{"begin transaction;
INSERT INTO people (firstname, lastname) values (\"Ray\",\"Wenderlich\");
INSERT INTO people (firstname, lastname) values (\"Adam\",\"Burkepile\");
INSERT INTO people (firstname, lastname) values (\"Charlie\",\"Fulton\");
INSERT INTO people (firstname, lastname) values (\"Matt\",\"Galloway\");
end transaction;
"}

Next, add a View Results action to your workflow. While you don’t need to see the results at this point, you should be aware that at the time of creating this tutorial, there is a bug in the current version of Automator (2.4) where the variables may react strangely to certain text files. Having a View Results action seems to mitigate the bug.

Now add a Set Value of Variable action, create a new variable called SQL Statements.

The workflow should now look like:
Screen Shot 2013-12-13 at 5.10.13 pm

Now what you need to do is set it up so the user (which will be you in a moment) picks a database for the SQL, then apply the SQL to the chosen database.

Add an Ask for Finder Items action to your workflow, this will allow you to select the database where you wish to run the SQL statements.

Change its options to ignore this action’s input, change the prompt to Choose a SQLite Database: and set Start at: to a default location where you save SQLite files or leave it on Desktop.

Screen Shot 2014-01-13 at 6.54.41 pm

Drag a Set Value of Variable action underneath it and don’t bother setting the variable now; you’ll come back to it later.

Add a Get Value of Variable action, set the variable to SQL Statements and options to Ignore this Action’s Inputs.

Add an Apply SQL action, select New Variable from the Database dropdown list. Name the new variable SQLite Database.

Screen Shot 2013-12-15 at 11.12.48 pm

Now you can go back to the Set Value of Variable action (third action from the bottom of the workflow) and change it to SQLite Database.
Screen Shot 2013-12-13 at 5.41.53 pm

Congratulations, you’ve now made an application! Save the file somewhere on your Desktop for easy access.

To recap the workflow you just created:

  • You first bring in all CSV files dropped onto the application.
  • You then combine them into a single text string, and convert it into SQL statements, and save it in a variable.
  • Next, you ask the user to supply a database file and save the path to a variable.
  • Finally, you pass your SQL statements into the Apply SQL action, which runs on the provided SQLite file.

To test it out, drag and drop your names.csv file on top of the application you just saved.

dragndrop

It will then ask you to supply an SQLite database:
Screen Shot 2013-12-13 at 5.51.16 pm

That’s it! The process is done. How can you check that it has worked?

Go back to the previous workflow (the one you used to create the table) and change the SQL to:

<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> people;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> people;

Then run the workflow:

Screen Shot 2013-12-13 at 5.53.55 pm

Look at you! You now have experience with all of the available SQL actions. Is your mind overflowing with ideas on how to use them to automate your database related projects? It should be!

So far you’ve only scratched the surface of what you can do with Automator actions. In the next and final practical example you’ll have another look at AppleScript, and how you can use outputs from your other actions.

Neil North

Contributors

Neil North

Author

Over 300 content creators. Join our team.