Safely quit apps


Today I felt like procrastinating, so I wrote this script for safely quitting apps. The reason for writing this script is the fact that Dropbox (or the other cloud solution I also use SurgarSync) is currently not supporting a merging functionality. If you have the application open in two devices at the same time this could lead to data inconsistency while switching.

In my particular case this translates to the fact that I sometimes forget to close several apps in my Mac at work, so when I get home on occasion I’d like to continue to work or access some files. In the past I’ve had issues with this, and I’d loose some data. For instance, once I was working on a latex document I wrote some new lines and when I went to my other computer, assuming the new lines had been incorporated I found that some were but others weren’t. The worse part was that I tried to restore previous versions of the file via Dropbox but the new lines were nowhere to be found.

So, here I am with the script I wrote to address this issue. I’ve scheduled it with iCal so it executes at 7:45 pm (I normally leave around 7:00). Here’s the script, there are also other apps that don’t necessarily need safe quitting but that I’d also like to quit. The best part of the script is that it sends me an email telling me which apps are still running and which apps were successfully quit. Feel free to copy, modify and redistribute this script any way you like.

-- This script is for safe quitting Applications

-- Applications for safe quitting 
set AppList to {"TaskPaper", "TextMate", "OmniOutliner Professional"}

-- Applications that are ok to just quit without further instructions
set otherAppList to {"Adium", "nvALT"}

set notRunningApps to {"These Apps were safely quit:"}
set RunningApps to {"These Apps are still running:"}

-- The safely quit App procedure
repeat with theApp in AppList
  try
        if ApplicationIsRunning(theApp) then
           tell application theApp
                save documents
             close windows
              quit
           end tell
       end if
     set notRunningApps to notRunningApps & theApp
  on error
       set RunningApps to RunningApps & theApp
    end try
end repeat

-- Quitting the "other" Apps
repeat with theApp in otherAppList
    try
        tell application theApp to quit
        set notRunningApps to notRunningApps & theApp
  on error
       set RunningApps to RunningApps & theApp
    end try
end repeat

-- Just a bit of formatting
set RunningApps to RunningApps & " "

-- Appending both lists for the message to be sent
set theMessage to RunningApps & notRunningApps

-- Convert lists to text
set old_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set theMessage to theMessage as text
set AppleScript's text item delimiters to old_delim


-- Send the email with the message
sendEmail(theMessage)

-- Send email function
on sendEmail(MessageContent)
    tell application "Mail"
     set theNewMessage to make new outgoing message with properties {subject:"Quit Apps Message", content:MessageContent, visible:true}
     tell theNewMessage
         make new to recipient at end of to recipients with properties {address:"your@email.com"}
           send
       end tell
   end tell
end sendEmail

-- To know if an App is running
on ApplicationIsRunning(appName)
   tell application "System Events" to set appNameIsRunning to exists (processes where name is appName)
   return appNameIsRunning
end ApplicationIsRunning

References

Some references I used for writing the script. I’m no Applescript expert, I just look what’s available out there and use it to fit my needs. Here’s something in return.

  1. Schedule your scripts
  2. Save and restore safari urls
  3. Applescript list examples
  4. Advanced Applescript
  5. Send mail using AppleScript
  6. Script function handers
  7. Safely quit TaskPaper
Advertisements

If you liked this post please leave a comment or consider subscribing.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s