15 Automator and AppleScripts You Can’t Live Without

via http://www.maclife.com/article/features/15_automator_and_applescripts_you_can%E2%80%99t_live_without

Computers were designed to do repetitive tasks efficiently and consistently, so why then are we still sitting around and renaming files on our computer by hand? Why are we still relying on our memory to remind us of our friend’s birthdays? There’s got to be a better way, and there is with Automator and AppleScript. Not only can you rename files, but you can also augment your mind by having your computer remind you of events when you start up your machine.

Sure, these things may sound like something out of an episode of The Jetsons, but with today’s Macs, not only is automating repetitive tasks readily available, but it’s also easy to do. That’s why we would like to present you with 15 Automator and AppleScripts that you can’t live without.

Because we have over 15 different Automator workflows and AppleScripts that we will be presenting to you, we have also made the project files available as a download from our website by clicking here. If you get stuck at any point, or simply don’t feel like building out the projects, refer to these fully completed workflow and script files.

Intro to Automator

Automator allows you to automate tasks using a drag and drop interface that makes programming these tasks easier for beginners.

Automator is an application that Apple has distributed with Mac OS X since version 10.4 (Tiger). Automator allows anyone to easily create “workflows” that mimics a repetitive task. To create a workflow, you drag and drop Automator actions from the actions listing (on the left-hand side of the application) to the workflow area (on the right-hand side of the application). These actions can then be stacked on top of one another, like puzzle pieces, until the desired workflow has been completed with all of the necessary steps.

You may be overwhelmed with Automator at first, but you’ll soon be creating new Workflows in no time.

Each Automator action has a certain input and output functionality (e.x. You may feed keyboard input into an action and get a text file on your Desktop as the output). To get the details of the Automator actions, select them in the actions listing and read the information in the description view – a small panel underneath the actions listing. The description view will also tell you the necessary action input(s) and what the action will be outputting.

You can run a workflow from within Automator by clicking on the “Run” button in the top-right corner of the application, or save the workflow out as an Automator file or stand-alone application. To save the workflow as an Automator .workflow file, use the Standard save dialog (File > Save).

A workflow file will allow you to open the workflow again in Automator, while an application format will let you run the workflow as you would any other app on your Mac.

To save the workflow as a stand-alone application that will work just like other Mac OS X apps, click File > Save As. A new save dialog will open, allowing you to type in the name and specify a save location. But before clicking on the Save button, select “Application” from the File Format drop-down menu. This will allow the workflow to run outside of Automator, just like any native app.

These are the basic features and save options behind Automator. In the following workflow tutorials, you will use what you learned here to build 10 Automator actions that can help save you time when using your Mac. Remember, if you get stuck, you can download the project files by clicking here.

Automator Tutorials

1. Encrypt PDFs Created from the Print Dialog

Mac OS X includes a great feature in any print dialog that allows you to print to PDF. We’re going to expand on this functionality to have the PDF automatically get encrypted when doing a print to PDF. To begin, open Automator (located in /Applications), and select the “Print Plugin” template from the template chooser that appears. This will allow us to tie directly into Mac OS X’s print dialog. Next, drag and drop the following actions into the workflow (in the order they appear):

–    Encrypt PDF Documents
–    Move Finder Items

Now, let’s configure these actions. For the “Encrypt PDF Documents” action, click the Options button at the bottom of the action in the workflow and check the option for “Show this action when the workflow runs.” This will allow you to specify a new PDF password each time the action is run.

The save name is what will show up in the Mac OS X print dialog.

After you have configured this new workflow, click File > Save and specify a name for the new print plugin. Automator will do the rest and place the plugin in the appropriate location for it to run on your Mac.

When the workflow is run, a pop up window will appear, allowing you to type in a password for the PDF file about to be created.

To run the workflow, open a print dialog in any application. Click on the PDF button in the lower, left-hand corner of the dialog and select the name of the workflow that you just saved. Type in a password, then type it again for a confirmation. After clicking Continue, a new encrypted PDF will appear on your Desktop.

2. Save Text As Audio

Apple has built in a great synthesized voice in OS X called Alex. This voice has many applications across the operating system, including Apple’s VoiceOver screen reader. However, you can also use this voice to do things in Automator. In this instance, we’ll use it to create an Automator system-wide service let you save any text selected in an application as an audio file to the Desktop. This audio file can then be synced to your iPhone, iPod, or emailed to anyone. This is great for converting PDFs or websites to “audio books.”

To get started, open Automator and select “Service” from the template chooser. Next, drag and drop the following action to the workflow:

– Text to Audio File

In the Text to Audio File action, type the save as name of the audio file that will be created when the workflow is run. We used “AudioText” as the name of the file to be created.

The name you specify in the save field will appear in the Mac OS X Services menu.

When you are done configuring the workflow, click File > Save. Type in a name for the new Mac OS X Service and click Save.

 

The Mac OS X services menu is home to many useful utilities, and your own contributions using Automator’s Service template.

To run the service and generate an audio file, open almost any application on your Mac and select the text you would like to be converted. With the text selected, click on the application name in the menu bar. Select the name of the service that you just saved in Automator from underneath the Services menu listing. After clicking on the service, a new audio file will appear on your Desktop after a few seconds. This file will read aloud the text you selected in the application.

3. Batch Rename Finder Items

No one likes having to rename Finder items. Whether it’s vacation photos, video files, or even articles like this, if you have a bunch of files to rename, it can be time consuming and frustrating. Enter this Automator workflow that allows you to rename files in a blink of an eye.

To get started, open Automator and select the Workflow template from the template chooser; then, drag and drop the following two actions into the workflow:

– Get Specified Finder Items
– Rename Finder Items

These two actions will soon become your best friend for renaming files after you configure the “Rename Finder Items” action. This is a great action for you to play around with. Explore the different renaming options like Make Sequential, Change Case, and Add Text. As for us? We’ll choose the Make Sequential since we have a lot of files we need to rename with unique numbers.


To run this workflow, click the Add button in the “Get Specified Finder Items” action. In the resulting file selection window, select the files that you would like to have renamed and click the Add button. After adding your files, click Run. Once the workflow is run, your files will be renamed using the convention you outlined in the Rename Finder Items action.

You can save this as a workflow file so you will have it for later use, or save it as an application if you will be using the same naming convention on a regular basis.

4. Birthday Reminders

Sometimes we may forget things like bread, milk, and eggs from the grocery store, but let’s hope you don’t forget your wife, brother, or mom’s birthday. In this tutorial, we’ll use an Automator action to automatically remind us of upcoming birthdays by placing a text file containing upcoming birthdays on our Desktop.

To do this, open Automator and select Workflow as the template. Next, drag and drop the following actions to the workflow:

– Find People with Birthdays
– Get Contact Information
– New Text File

In the Find People with Birthdays action, select “This Week” from the drop-down menu. In the Get Contact Information action, check First Name, Last Name, and Birthday from the list on the left-hand side of the action; finally, check the “Combine Names” checkbox. In the New Text File action, type in a memorable name like “Birthdays.txt” and select an appropriate save location.

If there is a an upcoming birthday, it will appear in the text file along with the person’s name and exact birth date as entered in OS X’s Address Book.

To run this workflow, you can either click the Run button in Automator or save the workflow out as an application, which can then be run it at certain times. When the workflow is run, you will get a text file on your Desktop containing a list of people’s names that have a birthday in the currently week.

5. Export iPhone Movies using QuickTime

Automator isn’t all about productivity. It can also be used in conjunction with some QuickTime actions to export iOS-compatible movie files. To do this, open Automator and select the Workflow template. Next, drag and drop the following actions to the workflow:

– Ask for Movies
– Export Movies

The only thing that you will need to configure is within the Export Movies action. Select the appropriate file conversion format from the “Format” drop-down menu. You can select between iPhone, iPhone (Edge), iPod, Apple TV, and QuickTime.

You can preview selected movie files by clicking on the Play button.

When you are ready to run the workflow and convert your files for your Apple-specific device, click on the “Run” button in Automator or save the workflow out as an application. You will first be presented with an iLife media browser, allowing you to select a movie file from iTunes, Photo Booth, iMovie, or your own Movies folder. After selecting the video, click Choose. Your video will then be processed and placed in your Movies folder when complete, ready for importing to iTunes and syncing to your Apple device.

6. Combine Multiple PDFs into One

PDFs have become the go-to file format for many because it gives you printer-friendly files that appear just like originals, no matter where they are viewed. Sometimes, however, you may need to combine (or digitally “staple”) multiple PDFs together to create a larger PDF. While you could do this in multiple steps with Preview on your Mac, Automator can help streamline this process and allow you to combine multiple PDFs in one swoop.

To combine multiple PDFs in Automator, you will need the following actions in your workflow:

– Get Specified Finder Items
– Combine PDF Pages
– Move Finder Items

To run the workflow, drag and drop PDF files to the “Get Specified Finder Items” action, and then click on the Run button. After a few seconds, a single PDF will appear on your Desktop containing the multiple PDF files that were listed in the first workflow action.

After the workflow is run, a single PDF will appear on your Desktop that contains the multiple PDF files digitally “stapled” to each other.

You can save this workflow as an application on your Mac; In order to use it multiple times, however, clear the list of files in the “Get Specified Finder Items” and check the box labeled “Show this action when the workflow runs” in the options section.

7. Download MP3s from Opened Sites In Safari

Occasionally while browsing the web in Safari, you may come across MP3 files that you wish to download from the site, but who wants to go through the page, clicking away one-by-one on all of those download links? Once again, lets have Automator work for us to download all of the MP3 files on any given webpage opened in Safari.

To do this, open Automator and select Workflow as the template. Next, drag and drop the following actions into the workflow:

– Get Current Webpage From Safari
– Get Link URLs from Webpages
– Filter URLs
– Download URLs

Next, we’ll need to configure the Get Link URLs from Webpages action by checking the box labeled “Only return URLs in the same domain as the starting page.” Configure the Filter URLs action so that the first drop-down menu states, “All of the following are true.” While in the Filter URLs action, click the plus sign and use the drop-down menus and text box to state “Path Contains mp3”. Finally, configure the MP3 file save location by using the Download URLs action where menu.

Depending on the size of the MP3 files and how many there are, they may take a while to download to your Desktop.

To run the workflow, navigate to a webpage that contains MP3 files, then click “Run” in Automator to activate the workflow. After the workflow runs, your Desktop will contain the MP3 files linked to on the opened Safari page. You can save this workflow as an application if you plan on using it often.

8. Create Thumbnail Images

In a world where hi-res photography now rules, low-res thumbnail images are still necessary for use as avatars on sites and applications like Twitter, Facebook, iChat, and more. However, you don’t need to open a photo editing application to create thumbnail versions of your images. In this tutorial, we’ll create a Mac OS X service that will allow you to right-click on an image in the Finder and instantly create a thumbnail version of it.

To do this, you’ll need to create a new Automator Service template and drag the following Automator action into your workflow:

– Create Thumbnail Images

Because we selected a Service template, OS X will automatically send our workflow some type of file to handle; however, the default is text. To change the file type this service will receive to an image, use the drop-down menu at the top of the workflow to select “image files” in “any application.” Next, select the thumbnail size in the Create Thumbnail Images action.


Next, click File > Save. Type in the name of the new service – we’ll stick with “Thumbnailer.”

 

The thumbnail image will be output in the same directory as the original image.

To run the workflow, navigate to an image in the Finder and right-click to select Services > Thumbnailer. After clicking on it in the menu, a new thumbnail will be created and will contain a “_tn” in the filename.

9. Quickly Print Multiple Documents

Printing multiple documents from multiple applications can be a pain. Not only do you have to deal with multiple files, but you also have to open and close multiple applications. With a simple Automator workflow, however, you can select and print multiple documents without any fuss.

To do this, create a new Automator Workflow template and use the following actions:

– Get Specified Finder Items
– Print Finder Items

There are no actions to configure with this workflow. To print multiple files, drag and drop them into the “Get Specified Finder Items” action and click on the “Run” button in Automator. Each of the files will then be queued for printing.

10. Import Finder Items into iTunes and Set Options

When importing songs or other items into iTunes, you may want to instantly set things like volume adjustment, track ratings, or other iTunes song options. This is especially useful if you purchase songs from other online retailers like Amazon MP3. Automator can handle this task by combining these actions:

– Get Specified Finder Items
– Import Files into iTunes
– Set Options of iTunes Songs

To run this workflow, drag and drop the files you wish to import into the “Get Specified Finder Items” action, and then set the available iTunes song options in the “Set Options of iTunes Songs” action. When you click run, the workflow will cause iTunes to import the files, then immediately set their options.

This time saving workflow can be saved and run as is (in Automator), or can be saved as an application.

Intro to AppleScript

AppleScript is Apple’s consumer-level programming language that lets you write applications and scripts with English-like words and sentence structure.

AppleScript has been around for almost 20-years, allowing Apple’s customers to create their own programs, extend Mac OS, and create scripts that can do repetitious tasks. Because it is more advanced than Automator’s drag and drop user interface, AppleScript requires more practice and more knowledge of programming. However, AppleScript has a lot more power than Automator, making it still the go-to scripting language on the Mac.

The AppleScript editor is nothing more than a text editor where you can type in your scripts.

To write AppleScripts, you will need to open the AppleScript Editor (located in /Applications/Utilities). This text-based editor is where you can type your AppleScripts as well as test and run them.

AppleScript really shines in its ability to save out the scripts as applications and run them on your Mac as completely stand-alone apps. Automator can do this, but unfortunately relies on the Automator engine in the background to carry out the tasks.

The save name is the name of the application, so name your applications accordingly.

To save an AppleScript as an application, go to File > Save As. In the resulting dialog, type in a save name, select a location, and select “Application” from the File Format drop-down menu. When you’re done, click Save and you will have your very own AppleScript application.

These are the basic features behind the AppleScript Editor that will get you up and running. You will use the things you learned here in building out the 5 AppleScripts below. Remember, if you get stuck, you can download the project files by clicking here. You can also copy and paste the AppleScripts below directly into the AppleScript Editor.

AppleScript Tutorials

1. Resize Images

Today’s hi-resolution images look great on your screen, but when you want to e-mail them to someone, often times you must resize them so the pictures will fit inside of your e-mail send limit. Sure, you could break out a photo editing program, but the following AppleScript can automatically shrink any photo by 50%, saving you time and energy.

Type in the following AppleScript into the AppleScript Editor:

set this_file to choose file without invisibles
try
tell application “Image Events”
— start the Image Events application
launch
— open the image file
set this_image to open this_file
— perform action
scale this_image by factor 0.5
— save the changes
save this_image with icon
— purge the open image data
close this_image
end tell
on error error_message
display dialog error_message
end try

To run this script, click the “Run” button in the AppleScript editor or save it out as a stand-alone application. When run, the application will prompt you to select an image on your Mac. When you do, the original image will automatically be overwritten with its 50% smaller counterpart. All that’s left is to attach the new image to a new e-mail and click the send button.

2. Simple Task Launcher

When we start up our computer in the morning, we generally launch the same applications: Mail, Safari, iCal, and Twitter. So, why should we rely on opening these applications by hand? And we also don’t want them to start up with the computer because we don’t use these work-related applications on the weekend. One solution is to create a simple AppleScript-based application launcher that opens the necessary applications with one-click ease.

To create this AppleScript task launcher, open AppleScript and type in the following line of code:

tell application “Safari” to launch

Now run the script. Did you notice that Safari launched? You can replace the application name in the double-quotes with any application name, just type it exactly as it is listed in the Applications folder on your Mac.

Repeat the line of code on new lines to launch multiple applications. For instance, if we wanted to launch Mail, Safari, iCal, and Twitter, we would type in the following:

tell application “Mail” to launch
tell application “Safari” to launch
tell application “iCal” to launch
tell application “Twitter” to launch

Save this script as an application file, and double-click on it to have the applications automatically launched for you. You can create multiple task launchers for different applications: one for work, one for entertainment, and one for social networking.

3. Mark All Emails As Read

Occasionally, you may wish to mark all of your email messages as read, perhaps because you read them on another computer or on your mobile device. Sure, you could manually mark them all as read, but if you use Apple Mail, you can create a one-click AppleScript application that will do that whole process for you … automatically.

To do this, type the following script into the AppleScript Editor and click “Run”:

tell application “Mail”
activate
get message viewer 1’s selected mailboxes
repeat with thisBox in result
set read status of (messages of thisBox whose read status is false) to true
end repeat
end tell

Yes, we wish we could just have this script run all of the time.

Ahh… that’s better: Inbox at 0.

After running this script, your inbox will return to peace at 0 unread e-mails. If you have a few hundred e-mails, like us, this script could take a few seconds to mark all of your e-mails as read.

4. Print Daily iCal Agenda

Paper may be on its way out, but when we have a busy agenda scheduled, sometimes we like to have a hard copy for reference. However, you can save a few steps when printing an iCal daily agenda with this AppleScript:

tell application “iCal”
view calendar at (my (current date))
switch view to day view
activate
tell application “System Events”
keystroke “p” using command down
delay 1 — (seconds)
keystroke return
delay 2 — (seconds)
keystroke return
end tell
quit
end tell

Add that script to the AppleScript Editor and click the Run button. iCal will open to the current date and will print a daily agenda using your default printer. You can save this as an application and have it run at login so that you always have a print copy of your daily agenda.

5. Auto-Save Pages Documents

As writers, the thing we hate the most is when our word processing application crashes and we lose valuable writing. To combat this, we wrote an AppleScript that will automatically press the “Save” button in Pages every few seconds. Just save the following script as an application and run it whenever you’re in Pages to have your documents automatically saved for you.

repeat
delay 600
tell application “Pages” to save front document
end repeat

You can trade out the word “Pages” in double quotes with any of the other iWork suite of apps, like Numbers or Keynote. To edit the save interval, specify a new number after the delay text in the script. This is the number of seconds the script will wait between pressing the save button. This is measured in seconds, so as it stands at 600 seconds, the script will press the save button ever 10 minutes. Save this script as an application and run it whenever you’re editing in these applications to automatically have your work saved every few seconds.