While Apple makes opening applications and documents in OS X relatively straightforward, there may be times when you'd like to automate the process, opening those apps and docs when you wish without lifting a finger. Perhaps, for example, you want Mail to open every time you log in. Or maybe you want a specific set of apps to open every Friday at 9:00 am. Or maybe you want to get fancy and have a particular program open whenever a certain disk is mounted.
All of these things are possible using tools built into OS X. In fact, there are four distinct ways to schedule or automate the opening of applications and documents on your Mac, without the intervention of third-party tools.
The most common way to automate the opening of particular documents and applications is to tell OS X to open them whenever you log into your account. There are a couple of ways to do so. First, you can right-click a program on the Dock and choose Open At Login from the Options submenu. Or you can go to the Users & Groups pane in System Preferences, choose the Login Items tab for your user account, and add or remove items from the list, using the plus (+) and minus (-) buttons at the bottom of the window.
That Login Items list is often used by developers to open background applications and system menu extras for third-party programs you have installed. But you can add practically any item you would like--including documents, which (if added to the list) will open in their default applications.
The second way to automatically launch apps and documents uses OS X's built-in Folder Actions, which allow you to trigger a script when the contents of a specified folder is changed.
To set this up, open the AppleScript Editor utility, and then enable the Show Script Menu in Menu Bar in the General tab of its preferences. That done, open this menu and choose Configure Folder Actions from the Folder Actions submenu. In the panel that pops up, check the box to enable folder actions, and then click the plus below the left-hand column to add a folder. Now select the folder and click the plus in the right-hand column to bind a script to this folder.
There's one convenient built-in script--add--new item alert--that monitors the contents of the folder and, when something new is added to it, alerts you. You could make a copy of this script and have it launch the new item(s) instead. Or you could build your own custom folder action--using AppleScript Editor or Automator (select Folder Action when creating a new workflow)--to launch folder contents.
For example, you could set up an Automator workflow so that any PDF you add to a given folder automatically opens in Preview:
- Create a new workflow in Automator, and select Folder Action as the type of workflow.
- At the top of the window, specify the folder you would like this workflow to act on when new items are added.
- Add the Filter Finder Items action to the workflow, and then set it for All criteria to be true, with Kind Is PDF as the only rule.
- Add the Open Finder Items action next, and set the application to be Preview.
Once you save this workflow, and now when you add new PDF files to the specified folder, they will open in Preview. Using this approach, you can similarly specify other file types and applications, or have automator move items, delete them, or perform any other supported action on the items added to the folder.
While you might only think of the built-in Calendar apps as a way to organize your schedule, you can also use it to launch documents and applications at specific times. To do so:
- Create a new event.
- Set an alarm for this event.
- Choose Custom in the Alert menu.
- Select Open File as the type of alert.
- Choose the application, document, or Automator or AppleScript application you want to open.
- Set a day and time for the alert to trigger, and save it.
After doing this, when the alert triggers, the specified file will open or run. The convenience of using Calendar is you can quickly move events around and duplicate them. Also, if you add them to a special Automated Tasks calendar, you can uncheck the calendar to hide these events from view.
You can get a fair amount done by using the Login Items, Folder Actions, and Calendar as launching platforms. But sometimes you might need finer control over how programs are run, especially if they are background tasks that need to execute at specific time intervals or under specific conditions.
In these cases, you might benefit from creating scripts called Launch Agents that configure the system launcher to run tasks at specified times and under a number of optional conditions. Launch Agents can be quite complicated to configure; however, simple ones can be relatively easy to set up. The only hard part is knowing the syntax for the script.
At its most basic, a Launch Agent script is a standard property list (plist) XML file that has two main key-value requirements: first is a label so it can be listed and identified in the system launcher, and second is a program argument that points to an executable file on your Mac. Beyond this, you will need to indicate when to open the program.
While you can create Launch Agent files with text editors, another way is to use online tools such as Launchd Plist Generator. At this site, you can supply the label you'd like to use, followed by specifying the full path to a program in the Program Arguments field, and then setting conditions for launching the program before saving the generated plist in your username > Library > LaunchAgents folder. (You'll need to create this folder if it does not exist.)
Note that program files that end in .app in OS X are actually folders, so to launch these apps you will need to specify the executable file within their folders. For instance, the following is the full path to the Calculator application at its default location in OS X, and how it should look as the Program Argument value in a launch agent file: /Applications/Calculator.app/Contents/MacOS/calculator.
Granted this approach might seem complicated, but you can use it for running scripts and programs in the background, making it exceptionally useful for backups, maintenance, and other custom routines.
Direct and Indirect launching
The above approaches can be used to open applications directly, but you might consider having them open a script that will in turn launch the desired applications. Even though using a script adds complexity, by doing so you can set and forget the scheduling routine, and then simply update the script to change what is opened and how it is opened.
Scripting can sound intimidating, and for shell scripts and even AppleScript it can be daunting to get right. However, Apple's Automator program makes it relatively simple to create basic workflows. You can then save these workflows as small applications, and then use the above methods to open those on the schedule or condition you set.
This story, "Four ways to open applications and documents automatically in OS X" was originally published by Macworld.