Calculating Sunrise and Sunset

You can use Power Manager to calculate sun observation times with AppleScript or JavaScript. Like everything else in Power Manager, the internal astronomical calculator is available through the scripting interface.

AppleScript calculating sunset tomorrow

We are keen supporters of being able to script and augment software. For Power Manager that translates into providing first class scripting support. When we recently added a sun observation trigger and condition, we took the opportunity to expose the internal calculator for use in your scripts.

Calculating Sunset

Let's jump right in and get Power Manager to tell us when we can observe sunset:

  tell application "Power Manager"
  tell Astronomic

    set myDate to sun observation moment sunset

  end tell
end tell

The value returned in myDate is a time and date. But where in the world is sunset happening at this time? The sun observation command defaults to the Royal Observatory in Greenwich, London; a suitable nod to the history of time keeping.

Calculate Sunset Today in Sydney, Australia

I assume you will want to provide your own location. To do this we need a latitude and longitude. These are two numbers that pinpoint a specific location on Earth.

There are many ways to find a latitude and longitude. Let's use OpenStreetMap, a wonderful free mapping resource on the Internet.

Visit OpenStreetMap and search for your desired location. After picking your location, the results should include a line like:

  Location: -33.8548157, 151.2164539

In this case, I searched for Sydney, Australia. The results tell me that the latitude is -33.8548157 and the longitude is 151.2164539. These are very specific values and would place you in the middle of the harbour – and in the path of the ferry service. Not ideal but for our example these values will do:

  tell application "Power Manager"
  tell Astronomic

    set myDate to sun observation moment sunset latitude -33.8548157 longitude 151.2164539

  end tell
end tell

This AppleScript snippet includes two new parameters, latitude and longitude followed by the values obtained earlier. With the location provided, the time and date now returned for sunset are calculated relative to Sydney, Australia.

Calculate Sunset Tomorrow in Sydney, Australia

The sun observation command accepts one more parameter, date. The date parameter lets you calculate sunset on any day in the past or future.

By default, the date parameter is today.

Let's add date to our example and find out when sunset will occur in Sydney tomorrow:

  tell application "Power Manager"
  tell Astronomic

    set tomorrow to ((current date) + 86400) as date
    set myDate to sun observation moment sunset latitude -33.8548157 longitude 151.2164539 date tomorrow

  end tell
end tell

In the script above the value 86400 is the number of seconds in a day. Before asking for the sun observation we take the current date, add one day in the form of seconds, and call that tomorrow.

More than Sunset

Power Manager is capable of calculating more than just sunset times. In your script, trying replacing sunset with any of the following:

  • night end
  • ‌nautical dawn
  • ‌dawn
  • ‌sunrise
  • ‌sunrise limit
  • ‌sunset limit
  • ‌sunset
  • ‌dusk
  • ‌nautical dusk
  • ‌night begin

JavaScript Support

With OS X 10.10, Apple introduced support for scripting applications through JavaScript. As with AppleScript, Power Manager is accessible through JavaScript:

  PowerManager = Application('/Applications/Power');
var theDate = PowerManager.astronomic.sunObservation({moment:'sunset',latitude:-33.8548157,longitude:151.2164539});

  App = Application.currentApplication();
  App.includeStandardAdditions = true;
  App.displayDialog("Date: "+theDate);

JavaScript calculating sunset

Beyond Schedules

As you can see, Power Manager's new astronomical calculator is flexible enough to be useful beyond its intended use of triggers and conditions within your schedules.

This article was posted in , , , and and tagged , , , and .

Published by Graham Miln on