Everyone loves automation. Automation is conservation of energy; its like the habits we people are into, the habits that seem to be there for all living entities. One great automation option we may enjoy in our everyday work is that provided by the operating systems on our computers to perform tasks for us. One such option is the launching of applications during system startup just after we login. In this post I made up a step-by-step process to optimize the login automation on a Mac OS X machine.
The obvious approach in the case of Mac OS X is for someone to rely on the system and the application settings to “attach” the launching of the application during login. Popular applications such as Dropbox provide a general setting to “Start Dropbox on system startup”. In more “advanced” approaches, one may easily include another application (with no such option) in the login launch list by manually adding it to the Login items section in the System Preferences panel >> Users & Groups section for the current or any other user (might need admin credentials).
This or the other way, the Login items section could end up with a long list of applications, which is great in its own, but there is an issue: although we might expect those applications to be launched in a serial manner, one after the other, so that we have a control on what and when is happening, we get a number of applications competing to launch at almost the same time. This might result in applications taking more time to launch (because the share the same resources of the machine at the same time) and if there are dependencies for one application to specifically launch after another, then we have a serious issue.
In the following paragraphs I propose an alternative way of defining the Login items list, which gives some control over the sequence of actions during login. For my case study let the applications needed to start during login be: Finder (previously open windows), Messages, Skype, Activity monitor, Yellow (an app for post-its on the desktop), Bandwidth+ (an app for the display of network activity), Translate Tab (an app for Google translation functionality on the Status Menu), Safari, Google Drive (nowadays called Backup and Sync) and two sessions of Dropbox (two distinctive instances for different login credentials — this is another issue discussed here). These are apps really time consuming and resource demanding during startup and their simultaneous launching might be problematic.
My approach would be to remove everything from the Login items list in System Preferences and create an app in the Script Editor of Mac OS to do the job in a more controlled way. Let’s get to business: launching an application using a AppleScript is easy like
tell application "Messages" to run
And how about hiding the application (prevent the main app window to show up)?
tell application "Finder" to set visible of application process "Messages" to false
which actually translates to
tell application "System Events" set visible of application process "Messages" to false end tell
Ok, and how might it be possible to create an actual sequence of applications launching one after the other? Well, I did not find an elegant way of doing it, but there is always a “dirty” way; delays. Suppose we need to launch Skype (and hide it also) a little after the Messages app has been launched and got hidden; easy: include a
delay 1command to force AppleScript execution to pause for 1 second.
tell application "Messages" to run tell application "Finder" to set visible of application process "Messages" to false delay 1 tell application "Skype" to run tell application "Finder" to set visible of application process "Skype" to false
Launching previously open Finder windows is a bit more tricky that this, though:
tell application "Finder" quit delay 1 launch delay 1 activate reopen end tell
whereas the case of restoring Safari windows is like accessing the menu:
tell application "System Events" tell process "Safari" click menu item "Reopen All Windows From Last Session" of menu "History" of menu bar 1 end tell end tell
Ok, now that the picture seems to reveal itself, here is my preferred AppleScript for the launch sequence:
# # Planning of the startup launch sequence # delay 5 tell application "Finder" quit delay 1 launch delay 1 activate reopen end tell delay 1 tell application "Messages" to run tell application "Finder" to set visible of application process "Messages" to false delay 1 tell application "Skype" to run tell application "Finder" to set visible of application process "Skype" to false delay 1 tell application "Activity Monitor" to run tell application "Finder" to set visible of application process "Activity Monitor" to false delay 1 tell application "Yellow" to run delay 1 tell application "Bandwidth+" to run delay 1 tell application "Translate Tab" to run delay 1 tell application "Safari" to run tell application "Safari" to activate tell application "System Events" tell process "Safari" click menu item "Reopen All Windows From Last Session" of menu "History" of menu bar 1 end tell end tell delay 5 tell application "Backup and Sync" to run delay 5 tell application "Dropbox" to run delay 10 tell application "dropbox-auxiliary" to run
Compiled and saved as an “app” with a name like “Launch startup items.app” I store it on iCloud to have it available. To use this app, I prefer to copy it in the main Applications folder and include it in the Login items list.
Running this app during system startup (just after the login) is perceived as a secure threat for the system, so it is possible that the first time we reboot to see how this script will perform, we get an error message. To correct this we just need to declare this app in the Accessibility options in the Security & Privacy section in the System Preferences panel.
…and now a bonus: how do we get to have two instances of Dropbox (two different accounts) running? This is a task for another Mac OS X tool, the Automator.
Just after installing Dropbox, open the Automator and create a new Application
Select the action Run Shell Script from the actions library and drag drop it to the right.
In that window just type something like
HOME=$HOME/Dropbox_auxiliary /Applications/Dropbox.app/Contents/MacOS/Dropbox &
HOME=$HOME/Dropbox_auxiliary informs Dropbox to start from a specific folder, which in this case can be any other than the default folder where Dropbox synchronizes files. This way Dropbox is forced to create a new instance and ask for another account to login and start synchronizing. I also save this app on iCloud and copy it in the main Applications folder to have it available.