Follow us @bensoftware

SecuritySpy

Multi-camera video surveillance software for the Mac

Back to help


AppleScript Examples

AppleScript is the a scripting language that is widely supported by Mac applications and used to automate tasks. SecuritySpy understands many commands to capture images, arm and disarm camera modes, and set various settings. You write scripts using the "AppleScript Editor" application in the /Applications/Utilities/ folder.

AppleScripts can be run from within the AppleScript Editor application, or alternatively saved as applications themselves, so they can be opened directly from the Finder and run independently. To do this, open a script in AppleScript Editor, choose Save As... from the File menu, choose Application as the File Format, make sure the Startup Screen option is disabled, and save the new script application:

To view the AppleScript commands supported by SecuritySpy, open AppleScript Editor, select Open Dictionary from the File menu, and select SecuritySpy. You can also view the SecuritySpy AppleScript commands here.

Camera Numbers

For commands that apply to specific cameras, you can either specify a camera name or a camera number. Camera numbers can be found via the Camera Info window in SecuritySpy, as highlighted in the screenshot below:

If you don't see this column in your Camera Info window, click on the header bar (where the column names are shown) for a menu that allows you to add columns.

For commands where it is appropriate, you can specify the camera number as -1 to mean "all cameras".


Regular Image Capture

This script demonstrates a regular image capture. The script tells SecuritySpy to capture one frame every 10 seconds from camera number 0, and saves it to disk into a "Captures" folder created on the Desktop, with a set file name depending on the current date and time.


Triggering Motion-Detected Recording

This script simulates motion on camera 0. This could be useful to trigger a recording based on an external sensor such as a switch, infra-red sensor or doorbell.


Enabling and disabling a camera, and setting camera settings

This script enables camera 0, and then disables it. This uses the camera settings HTTP POST request in SecuritySpy, so SecuritySpy's web server must be enabled. Make sure to modify the "formURL" address with the correct IP address and port of your SecuritySpy web server. If you have enabled authentication you can additionally add this to the url as follows:

http://username:password@<ip-address>:<port>/++camerasetup

All camera settings can be set in this way using POST requests - to see the parameter names, have a look at the source code for the ++camerasetup HTML page available through the web server.


Send iMessage

This script captures an image file from a camera and sends it as an iMessage via the Messages app. You'll need to edit the script to set the correct camera number and recipient's phone number or email address (which must be in your address book). This script can be set in SecuritySpy as an Action to be performed upon motion detection.


FaceTime Call

This script initiates a FaceTime call.


Pan/Tilt/Zoom Auto-Patrol

This script periodically tells camera 0 to move to PTZ preset 1, then preset 2, then preset 3. You should modify this script to add the presets you want to patrol, and also you can change the delay period (which is currently 10 seconds).


Button Click Image Capture

This script will present a small window with two buttons: "Stop" and "Capture". Click the Capture button repeatedly to capture still images from a particuliar camera; each image will be saved to disk and numbered sequentially, with an additional session number so that you don't have to empty the destination folder before starting a new capture session. Before running this script, in the "capture image" command, set the correct camera number, and set the path to a valid directory within your Documents folder.


Camera Settings Change

The camera settings can be set via SecuritySpy's built-in web server, and so to set the settings from AppleScript, simple scripts can be written that post data to the web server. The example below turn motion detection off and on for camera 0.

In this way, you can have a quick script that changes some key settings for a camera - and if required, this can be scheduled on a regular basis (see Scheduling Scripts). If you have enable password protection for your SecuritySpy web server, you will have to enter the username and password in the "PathToServer" URL. The IP address "127.0.0.1" used in the above script universally refers to "this computer", so should work in all circumstances, no matter what your actual computer's IP address is. Many settings are available, below are some of the most useful ones. Note that you must include the action=save parameter, as demonstrated above, to tell SecuritySpy to save the settings.

camEnabledCheck0 or 1Disables or enables a camera
mdCheck0 or 1Turns motion detection off or on
mdSensitivityText0-100The motion detection sensitivity
mdSoundTriggerCheck0 or 1Whether sound triggers motion detection
mcCaptureMovieCheck0 or 1Turns motion movie capture off or on
ccCaptureMovieCheck0 or 1Turns continuous movie capture off or on

Parse System Info

There is a "System Information" request available via SecuritySpy's web interface, which contains a wealth of information about each camera such as the resolution, mode and connection status. As a demonstration, this script gets and parses this information, and displays a message when a camera is offline. Instead of a message, you might find it useful to send an email to notify you of an offline camera, or link it up to a home automation system that automatically power-cycles the offending camera in an attempt to bring it back online.

For this script to work correctly, you will need to enable SecuritySpy's web server, and modify the "username:password" string in the script to match the authentication of a web server user account with full access privileges (i.e. including general settings).


Scheduling Scripts

The easiest way to schedule a script for timed or repeated execution is via the Calendar application that comes with your Mac. First, create a simple script and save it as an Application The following screenshots demonstrate this with a simple script that sets the first PTZ preset position for camera 0:

After saving the script as an application, the resulting file can be run by simply double-clicking it in the Finder. You can still edit it if you need to, by opening it in Script Editor.

Then, to schedule the script to run at certain times, open the Calendar app, and create a new event. Under the "alert" section, select Custom, then Open File, and select your script application as the file to open. The three arrows in the following screenshot show the settings you need to set:

If you want the script to execute repeatedly at certain times, use the "repeat" option available in the calendar event settings.


ProcessCapturedFile

When installed, this script will be automatically invoked by SecuritySpy for every captured file that is produced. You can use it to apply some additional processing to captured files, such as to move them to a particular folder, send them to another application or upload them to a server. In order for SecuritySpy invoke this script, it must be placed into the SecuritySpy folder within your Home folder (i.e. at ~/SecuritySpy/). The camera number, camera name and file path are passed into the script as parameters - the example script here demonstrates how the parameters work by simply displaying them in a dialog.