Alert.png

Notice: This page contains information for the legacy Phidget21 Library.

Phidget21 is out of support. Bugfixes may be considered on a case by case basis.

Phidget21 does not support VINT Phidgets, or new USB Phidgets released after 2020. We maintain a selection of legacy devices for sale that are supported in Phidget21.

We recommend that new projects be developed against the Phidget22 Library.


Click on the 2phidget22.jpg button in the menu bar to go to the Phidget22 version of this page.

Alert.png

Language - Applescript: Difference between revisions

From Phidgets Legacy Support
No edit summary
Line 33: Line 33:
*Coding with Phidgets is made possible by the interaction of Apple Events between AppleScript and • the PhidgetsOSA application. Type the following to interact with PhidgetsOSA:
*Coding with Phidgets is made possible by the interaction of Apple Events between AppleScript and • the PhidgetsOSA application. Type the following to interact with PhidgetsOSA:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   tell application "PhidgetsOSA"
   tell application "PhidgetsOSA"
   end tell
   end tell


</source>
</syntaxhighlight></div>
</font>
</div>


The project now has access to Phidgets and we are ready to begin coding. For the rest of this document, unless otherwise stated, it will be assumed that all code will be typed inside this tell block.
The project now has access to Phidgets and we are ready to begin coding. For the rest of this document, unless otherwise stated, it will be assumed that all code will be typed inside this tell block.
Line 49: Line 45:
A Phidget object will need to be declared. For example, we can declare a PhidgetInterfaceKit:
A Phidget object will need to be declared. For example, we can declare a PhidgetInterfaceKit:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   set ifkit to make new phidget interfacekit
   set ifkit to make new phidget interfacekit


</source>
</syntaxhighlight></div>
</font>
 
</div>
The object name for any type of Phidget is listed in the PhidgetsOSA dictionary. Every type of Phidget also inherits functionality from the Phidget base class.
The object name for any type of Phidget is listed in the PhidgetsOSA dictionary. Every type of Phidget also inherits functionality from the Phidget base class.


Line 68: Line 61:
The wait parameter will block for a certain amount of time until a connection is made to the Phidget. For example, we can connect to a PhidgetInterfaceKit with:
The wait parameter will block for a certain amount of time until a connection is made to the Phidget. For example, we can connect to a PhidgetInterfaceKit with:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   tell ifkit to open
   tell ifkit to open


</source>
</syntaxhighlight></div>
</font>
</div>


The different types of open can be used with parameters to try and get the first device it can find, open based on its serial number, or even open across the network.  
The different types of open can be used with parameters to try and get the first device it can find, open based on its serial number, or even open across the network.  
Line 85: Line 74:
We can tell the ifkit to block for 5000ms until a connection has been made to the PhidgetInterfaceKit with:
We can tell the ifkit to block for 5000ms until a connection has been made to the PhidgetInterfaceKit with:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   tell ifkit to open wait 1000 --wait for 5000ms
   tell ifkit to open wait 1000 --wait for 5000ms


</source>
</syntaxhighlight></div>
</font>
</div>


We can also connect to a PhidgetInterfaceKit over the WebService with a serial number of 99999 on a server with an IP Address of 192.168.3.180 and a port that is opened on 5001 with:
We can also connect to a PhidgetInterfaceKit over the WebService with a serial number of 99999 on a server with an IP Address of 192.168.3.180 and a port that is opened on 5001 with:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   tell ifkit to open serial number 99999 server address "192.168.3.180" server port 5001
   tell ifkit to open serial number 99999 server address "192.168.3.180" server port 5001


</source>
</syntaxhighlight></div>
</font>
</div>
 
It is also possible to connect using the server id. For example, we can connect to a PhidgetInterfaceKit on a password protected server with:
It is also possible to connect using the server id. For example, we can connect to a PhidgetInterfaceKit on a password protected server with:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   tell ifkit to open server id "TestMac" password "pw"
   tell ifkit to open server id "TestMac" password "pw"


</source>
</syntaxhighlight></div>
</font>
</div>


At the end of your script, don’t forget to call close to free any locks on the Phidget.
At the end of your script, don’t forget to call close to free any locks on the Phidget.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   tell ifkit to close
   tell ifkit to close
   delete ifkit
   delete ifkit


</source>
</syntaxhighlight></div>
</font>
</div>


Phidgets can also be freed from the [[#PhidgetsOSA Menu Bar|PhidgetsOSA Menu Bar]].
Phidgets can also be freed from the [[#PhidgetsOSA Menu Bar|PhidgetsOSA Menu Bar]].
Line 138: Line 110:
We can hook an event handler at loading with the following code:
We can hook an event handler at loading with the following code:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   tell ifkit to make new interfacekit sensor change handler with properties {script file:thisScript}
   tell ifkit to make new interfacekit sensor change handler with properties {script file:thisScript}


</source>
</syntaxhighlight></div>
</font>
</div>


And after the tell block at end of the script, the callback method is defined as follows:
And after the tell block at end of the script, the callback method is defined as follows:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   using terms from application "PhidgetsOSA"
   using terms from application "PhidgetsOSA"
Line 160: Line 126:
   end using terms from
   end using terms from


</source>
</syntaxhighlight></div>
</font>
</div>


With this function, the code inside the interfacekit sensor changed handler will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.  
With this function, the code inside the interfacekit sensor changed handler will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.  
Line 174: Line 138:
For example, sensor values from the PhidgetInterfaceKit can be read with:
For example, sensor values from the PhidgetInterfaceKit can be read with:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   log "The first sensor has a value of: " & first interfacekit sensor's value
   log "The first sensor has a value of: " & first interfacekit sensor's value


</source>
</syntaxhighlight></div>
</font>
</div>


These functions can be used inside a polling loop as an alternative to event driven programming.
These functions can be used inside a polling loop as an alternative to event driven programming.
Line 192: Line 152:
For example,
For example,


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=applescript>
<font size="3">
<source lang=applescript>


   if first phidget interfacekit exists then
   if first phidget interfacekit exists then
Line 203: Line 161:
   end if
   end if


</source>
</syntaxhighlight></div>
</font>
</div>





Revision as of 21:39, 27 March 2012

Icon-Applescript.png Applescript is a scripting language for Mac OSX designed primarily for interfacing between applications.

Support

Applescript has a complete API and sample code for all Phidgets devices.

For a complete list of our supported languages and their support status, click here.

  • Our honest opinion on how well this language is suited to controlling Phidgets. If it is a poor choice, suggest and link similar (better) languages.
  • In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.

Development Environments and Compilers

Describe each major compiler and notable differences or important information. (eg. framework versions) If there are known issues/workarounds mention them and link to the corresponding issue at the bottom of the page.

Quick Downloads

Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website. Visit the drivers section at www.phidgets.com and get the latest:

You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:

  • API Manual (Link to API download)
  • API Reference (Link to online API reference (if applicable))
  • Example Programs written in this language
  • You can find a high level discussion about programming with Phidgets in general on the General API page.
  • The Device Functionality page explains the general operational information for your device.

You may want to have these pages open while working through these instructions.

Getting Started

The Phidget examples were written using AppleScript 2.1.2 under AppleScript Editor 2.3, and this tutorial assumes their use. Other versions and development environments should work as well and would be set up in a similar manner.

  • First, open the AppleScript editor to create a new script.
  • Coding with Phidgets is made possible by the interaction of Apple Events between AppleScript and • the PhidgetsOSA application. Type the following to interact with PhidgetsOSA:
  tell application "PhidgetsOSA"
  end tell

The project now has access to Phidgets and we are ready to begin coding. For the rest of this document, unless otherwise stated, it will be assumed that all code will be typed inside this tell block.

Coding For Your Phidget

A Phidget object will need to be declared. For example, we can declare a PhidgetInterfaceKit:

  set ifkit to make new phidget interfacekit

The object name for any type of Phidget is listed in the PhidgetsOSA dictionary. Every type of Phidget also inherits functionality from the Phidget base class.

Connecting to the Phidget

Next, the Phidget object needs to be initialized and the program needs to try and connect to the Phidget through a call to open. Open will tell the program to continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected. This means that simply calling open does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and tracking the Attach Events and Detach Events, or by specifying the wait parameter. The wait parameter will block for a certain amount of time until a connection is made to the Phidget. For example, we can connect to a PhidgetInterfaceKit with:

  tell ifkit to open

The different types of open can be used with parameters to try and get the first device it can find, open based on its serial number, or even open across the network. For more information on connecting across a network, please see the "Working with Phidget WebService" guide on the programming section at www.phidgets.com. The PhidgetsOSA dictionary lists all of the available modes that open provides. Examples of the usage of different types of open are listed below:

We can tell the ifkit to block for 5000ms until a connection has been made to the PhidgetInterfaceKit with:

  tell ifkit to open wait 1000 --wait for 5000ms

We can also connect to a PhidgetInterfaceKit over the WebService with a serial number of 99999 on a server with an IP Address of 192.168.3.180 and a port that is opened on 5001 with:

  tell ifkit to open serial number 99999 server address "192.168.3.180" server port 5001

It is also possible to connect using the server id. For example, we can connect to a PhidgetInterfaceKit on a password protected server with:

  tell ifkit to open server id "TestMac" password "pw"

At the end of your script, don’t forget to call close to free any locks on the Phidget.

  tell ifkit to close
  delete ifkit

Phidgets can also be freed from the PhidgetsOSA Menu Bar.

Event Driven Programming

We recommend the use of event driven programming when working with Phidgets. We can hook an event handler at loading with the following code:

  tell ifkit to make new interfacekit sensor change handler with properties {script file:thisScript}

And after the tell block at end of the script, the callback method is defined as follows:

  using terms from application "PhidgetsOSA"
    on interfacekit sensor changed ind to val on ifkit
      log "Sensor Index: " & ind & ", Sensor Value: " & val
    end interfacekit sensor changed
  end using terms from

With this function, the code inside the interfacekit sensor changed handler will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs. Some events such as Attach and Detach belong to the base Phidget object and thus are common to all types of Phidgets. Please refer to the PhidgetsOSA dictionary and the AppleScript examples for a list of events and their usage.

Please keep in mind that events are triggered from the PhidgetsOSA application and not from AppleScript. Thus, the AppleScript editor won't be able to receive replies when the script runs. In addition, any changes you make to the event code must be saved in order for the changes to take effect.

Working directly with the Phidget

Some values can be read and sent directly to the Phidget. For example, sensor values from the PhidgetInterfaceKit can be read with:

  log "The first sensor has a value of: " & first interfacekit sensor's value

These functions can be used inside a polling loop as an alternative to event driven programming.

Using the same Phidget in more than one Application

One important thing to remember is that when working with Phidgets, a call to open will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. If your goal is to use the same Phidget among multiple AppleScripts and/or other applications, there are two approaches.

1. Implement logic in the script that will use the same Phidget object if the script detects that a Phidget of the same type has already been initialized.

For example,

  if first phidget interfacekit exists then
    set ifkit to the first phidget interfacekit
  else
    set ifkit to make the phidget interfacekit
    open ifkit
  end if


2. The one connection per device limit does not apply when exclusively using the open Phidget Webservice. For more information, please see the "Connecting to the Phidgets" section of this document.

Working with multiple Phidgets

Multiple Phidgets of the same type can easily be run inside the same program. In our case, it requires another InterfaceKit instance to be defined and initialized. The new instance can then be set up, opened and used in the same process as the previous one. If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.

Other Phidgets

The design given in this document can also be followed for almost all Phidgets. For example, if you were using a PhidgetRFID instead of an PhidgetInterfacekit, you would declare an RFID object instead of an InterfaceKit. The methods and events available would change but they can be accessed in a similar manner.

PhidgetsOSA Menu Bar

Applescript bar.jpg

The PhidgetsOSA menu bar appears whenever AppleScript accesses the PhidgetsOSA application. The menu bar is used to monitor the status of any accessed Phidgets.

Additionally, it can be used to free the lock on a Phidget or quit the PhidgetsOSA application.

Building your Project

Describe the different ways a project could be built using this language.

Common Problems and Solutions/Workarounds

Here you can put various frequent problems and our recommended solutions.