Mac automator shell script output

Making shell scripts more user friendly

One of the most compelling features of Automator is support for building services—headless applications that are pervasively accessible throughout the operating system. Many services are context-sensitive and designed to process or operate on user input.


Adding an Ask for Confirmation action after the Run Shell Script one and using $1 in the This will look like this: Automator with text output. To get your shell (bash) script to output to the screen continually so that you can see the progress, you need to launch it from AppleScript.

A brief and unscientific poll of Mac enthusiasts revealed that few actually use the Services menu. Despite its relative obscurity among users, I've learned to appreciate its value.

TechPwn Quicktime script

Apple offers a number of its own services—like one that supports dictionary lookup on a selected word—that work with the platform's standard applications. Third-party developers can also create services to deploy with their applications. Automator makes it really easy for regular end users to create their own services with specialized behaviors.

As a Linux refugee, one of the features that makes Automator particularly compelling is that it allows me to integrate command-line operations, commands, and pipelines into my Automator workflows. In this tutorial, I'm going to show you two of the ways that I use shell scripting in Automator services in order to simplify my work. I write virtually all of my articles in the Vim text editor and use Markdown syntax for formatting. On Linux, I used a Markdown processing tool from the command-line to convert my articles to HTML and then piped the output into the xclip command so that I could just paste the finished article directly into Ars Technica's content management system.

I had a simple shell script that I could call directly from within Vim itself to perform those steps. This same approach is still viable on Mac OS X, but I wanted to explore a more Mac-native solution to the same problem. More importantly, I wanted a solution that wasn't tied solely to Vim. That's where Automator comes into play. I built a trivial service that wraps a command-line Markdown processor. I can select a block of editable text with Markdown formatting in any Mac application and use the Markdown service to convert it to HTML in place.

To create a service, you start by selecting New from Automator's File menu. Automator will show you a list of available templates and prompt you to choose one for your new project. You should select the Service option, which is accompanied by a gear icon.

In your new service, you will see a bar at the top of the Automator flow pane.

It has combo boxes that allow you to set filters that establish the conditions in which your service should be made accessible. You want to make a service that receives selected text and will operate in any application. Below those combo boxes is a checkbox that you can toggle to specify whether you want the output of your service to replace the selected text block. We definitely want that to be checked for our Markdown service, because we are replacing the Markdown-formatted input text with the HTML output provided by the Markdown processing engine.

The behavior of our Markdown workflow is really simple, so it will only require one action. From the left-hand action library pane, drag the Run Shell Script action out into the workflow pane. Beyond that, you can even automate actions for photo manipulation. For example, you can add filters, like posterize and vignette.

If you need to use a database within your app, the most likely format is SQLite. Using Automator can make managing the database more efficient. In Automator, select File from the menu bar and then New. Note that one is included in the sample files for this tutorial. Drag a Execute SQL action onto your workflow. This can run any SQL you want on the database. When this action runs, its output will be the result of the query.

Click the Run button at the top right to see what happens. 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:.

All replies

The output of the Get Specified Finder Items action will be the filepaths of all files included in the action. 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.

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:. The idea is your app will be a names database for smart people, so you need a table to store names. This query will create your table with the name people and will contain two fields: first name and last name.

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. 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:.

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 :. 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. Enter the following SQL on each:. If you were to run this action on the sample names. Next, add a View Results action to your workflow.

Output Results from AppleScript

Having a View Results action seems to mitigate the bug. The workflow should now look like:. 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. Name the new variable SQLite Database. Save the file somewhere on your Desktop for easy access. To test it out, drag and drop your names. It will then ask you to supply an SQLite database :. 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! Combine that with the power of AppleScript and you can do some pretty nifty stuff. The input at the end of the Run Applescript action is actually an array of all data collected prior to this action in the workflow. To use these inputs, you need to assign them to a variable in your AppleScript. You can assign variables with either set or copy, followed by the name of the variable.

You can assign it a value straight away, above where you assign the value of the first item in the array of inputs. If you have multiple variables, they are available based on their position in the workflow:.

You may also like

Select Variables in your library and search for Documents and drag it onto your workflow. Go back to your Actions library and drag a Run AppleScript action onto the workflow:.