Run a script based on sunset and sunrise info?

What is the best way to run an Applescript when my current location's status changes from night to day and day to night?

Thanks!


Never used this, but it looks like Power Manager should be able to do it. You can make sun based actions similar to automator.

It doesn't seem like it has a feature built in to figure out your location automatically though. So if you're like flying from country to another and want it to automatically figure it out, you probably can't do that.

They have a tutorial: How to Power On Your Mac at Sunrise .

enter image description here


First of all, to get your current location on the command line, download "LocateMe" from here. Unpack the zip file and find the executable LocateMe inside, which you can either move to e.g. /usr/local/bin/ to make it globally accessible, or put it anywhere else.

To get your local sunrise/sunset times, you can use the free api from sunrise-sunset.org/api together with the LocateMe format option -f to generate your api url:

/path/to/LocateMe -f "http://api.sunrise-sunset.org/json?lat={LAT}&lng={LON}"

which should return something like http://api.sunrise-sunset.org/json?lat=52.068545&lng=12.126213

As the sunrise-sunset api returns json data, you can use python to parse it. Simply create two small shell scripts with the following contents:

#!/bin/bash
curl -s $(/path/to/LocateMe -f "http://api.sunrise-sunset.org/json?lat={LAT}&lng={LON}") | python -c "import sys, json; print json.load(sys.stdin)['results']['sunrise']"

for the sunrise and

#!/bin/bash
curl -s $(/path/to/LocateMe -f "http://api.sunrise-sunset.org/json?lat={LAT}&lng={LON}") | python -c "import sys, json; print json.load(sys.stdin)['results']['sunset']"

for the sunset times.

Now you can simply call them from AppleScript via:

set sunrise to do shell script "/path/to/sunrise_script.sh"
set sunset to do shell script "/path/to/sunset_script.sh"

and you have the sunset time as a variable inside AppleScript, where you can use them, for example, by comparing them with (time string of (current date)).