Advertisement

AppleScripting Dates > Elapsed Time Calculator

Image

Since Valentine's Day is this week, I thought I would take this opportunity to share an AppleScript I wrote to calculate how long my wife and I have been married. With this baby, I'll never be accused of forgetting how long it's been when our anniversary rolls around. The script determines the elapsed time between now (the current date and time), and a prior date and time (such as our anniversary). It then displays the result in seconds, minutes, hours, days, weeks, months and years.

Note: If you're into the whole "traditional anniversary gift" thing (I'm not), you can find a list of gift categories on Wikipedia.

Creating the Script

Note: If you have any trouble following along, you can download the complete script here.

1. Launch AppleScript Editor in /Applications/Utilities and create a new script document.

2. Insert the following code:

Some notes about this script...

* I used some of the number calculation handlers in the Essential Subroutines section of macosxautomation.com, although, I made some slight modifications to them for the script. If you're interested in learning more about AppleScript, there are some other useful handlers there, as well, and I encourage you to check them out.

* The final display dialog sets as its icon the FavoriteItemsIcon icon file, which is embedded in the CoreTypes bundle in /System/Library/CoreServices. It is, coincidentally enough, a heart.

Using the Script

To use, just run the script within AppleScript Editor, or save it as an application to be launched and run whenever you wish. First, the script asks you to enter a date and, optionally, a time.

Image

Next, the script does its calculations and displays the result. Just take a screenshot of the dialog and send it along to your significant other.

Image

Until next time, Happy Scripting and Happy Valentine's Day Week!