User Tools

Site Tools


applescript

Using AppleScript

AppleScript is a powerful scripting language that can be used to control and interrogate applications, as well as automate tasks. WeatherSnoop 3 has been built from the ground up to support AppleScript, allowing you to perform some very sophisticated and elegant operations.

If you would like to learn more about AppleScript, we suggest you view this tutorial.

To begin using the scripts below, open the AppleScript Editor which is located in your Mac's Applications > Utilities folder Utilities folder. There you can copy and paste in the following scripts.

Getting the List of Site Documents

The following code snippet will return an array of open Site Documents.

tell application "WeatherSnoop 3"
	get documents
end tell

Here's a screenshot of the AppleScript Editor running the above code segment, along with the result shown in the Event Log portion of the window.

In the above example, there is a single Site Document open: KLFT.wsssite. We'll use that document to work through the sample scripts ahead. Feel free to substitute your document name in place of it.

Once you have a list of open Site Documents, you can work with a specific document. The agent object of each document is the central point of interaction with WeatherSnoop 3. In the following code, we'll use the running property to tell us if the agent has started.

tell application "WeatherSnoop 3"
	tell agent of document "KLFT.wssite"
		get running
	end tell
end tell

The result is a boolean value: true (the agent is running) or false (the agent is stopped).

To make the following scripts easier to run, we'll use the first document property, which just picks the first document in the list of open Site Documents as the target for subsequent commands.

Starting and Stopping the Agent

The agent object responds to the start and stop commands which start or stop the agent, respectively.

This script will start the agent if it is not running, or stop the agent if it is running.

tell application "WeatherSnoop 3"
	tell agent of first document
		if running then
			stop
		else
			start
		end if
	end tell
end tell

Getting Site Information

You can interrogate the agent object for the current site information. Here, we are creating a sentence using the various properties (site name, location, longitude, latitude and elevation) available from the agent.

tell application "WeatherSnoop 3"
	tell agent of first document
		get site name & " is located at " & location & ". The coordinates are " & longitude & "," & latitude & " and the elevation is " & elevation
	end tell
end tell

Monitoring Communications

If you're interested in seeing how well the agent is communicating with the weather source, you can obtain the number of successful transactions and failed transactions during the agent's communication to the weather source. The following script returns the total number of transactions by adding the two properties together:

tell application "WeatherSnoop 3"
	tell agent of first document
		get successful transactions + failed transactions
	end tell
end tell

You can also get the last successful transaction and last failed transaction date/times:

tell application "WeatherSnoop 3"
	tell agent of first document
		get "Last good one: " & last successful transaction & ", last bad one: " & last failed transaction
	end tell
end tell

Viewing the Current Conditions

The agent object's current conditions property returns a nice summary of the current weather conditions. This script will get the current conditions, then tell the Mail application to compose and send an email to me with those conditions.

tell application "WeatherSnoop 3"
	tell agent of first document
		set body to current conditions
	end tell
	if body is missing value then
		set body to "No current conditions"
	end if
	tell application "Mail"
		set theNewMessage to make new outgoing message with properties {subject:"Current Conditions", content:body, visible:true}
		tell theNewMessage
			make new to recipient at end of to recipients with properties {address:"boisy@tee-boy.com"}
			send
		end tell
	end tell
end tell

Working with Weather Properties

Weather properties are measurable entities that carry the basic weather information that WeatherSnoop 3 gathers and shares. A weather property can be anything from an indoor temperature to an outdoor humidity. All weather properties carry within themselves a set of properties that can be accessed from AppleScript.

Every weather property has a name property that indicates what it is measuring. The weather properties and their names will differ, depending on the type of weather source.

In the following code snippet, we will pull a weather property named Temperature, available in the Weather Underground Agent, from its available list of weather properties (your agent may have a different name for its temperature property). We will then determine if the weather property's value is over 85 degrees (assuming Fahrenheit) and if so, report the actual value and its unit in a dialog.

tell application "WeatherSnoop 3"
	tell weather property "Temperature" of agent of first document
		if value > 85 then
			display dialog "It's getting hot at " & value & " " & unit & "!"
		end if
	end tell
end tell

Obtaining Archived Data

Starting with WeatherSnoop 3.0.7, you can access the archived (historical) data for a weather property. In order to do this, you must interrogate the weather property in question for its identifier which corresponds to the table name in the database.

With that identifier, you can then use the retrieve archive command to obtain the archived data for that weather property, given a start and end date to specify the range.

tell application "WeatherSnoop 3"
	tell agent of first document
		set tableName to identifier of weather property "Indoor Temperature"
		retrieve archive for tableName from date "Wednesday, January 1, 2014 at 12:00:00 AM" to date "Friday, January 1, 2016 at 12:00:00 AM"
	end tell
end tell

The result is a list of archive records. A single archive record consists of the archive date/time and the value.

Note that the values stored in the database are in English units; therefore, the values of numeric weather properties retrieved from the archive will also be in English units.

Next Steps

You've reached the end of the WeatherSnoop 3 User Guide! Feel free to return to the WeatherSnoop 3 Documentation Portal.

applescript.txt · Last modified: 2015/08/01 11:06 (external edit)