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
No edit summary
 
(26 intermediate revisions by 3 users not shown)
Line 5: Line 5:
==Introduction==
==Introduction==


{{LanguageSupport|AppleScript|the complete Phidget API, including events|all Phidget devices.|AppleScript Editor on OS X.|}}
{{LanguageSupport|AppleScript|the complete Phidget API, including events|all Phidget devices.|AppleScript Editor on OS X|}}
 
'''Note: Applescript is only supported with Phidgets on OS X 10.6 "Snow Leopard" and newer.'''


==Quick Downloads==
==Quick Downloads==
{{QuickDownloads|AppleScript|
{{QuickDownloads|AppleScript|
Find the API under AppleScript Editor → File → Dictionaries → PhidgetOSA|
Find the API under AppleScript Editor → File → Open Dictionary → PhidgetsOSA|
{{ExampleQuickDownloads|http://www.phidgets.com/downloads/examples/CSharp.zip|}}|
{{ExampleQuickDownloads|{{SERVER}}/downloads/phidget21/examples/applescript/AppleScript.zip|}}|
{{ExtraLibraryQuickDownloads|http://www.phidgets.com/downloads/libraries/phidget21-x86.zip|.NET Framework Files|}}
{{MacQuickDownloads}}}}
{{MacQuickDownloads}}}}


Line 25: Line 26:
===Description of Files===
===Description of Files===


Preference Pane integration, etc
The [{{SERVER}}/downloads/phidget21/libraries/macos/Phidget.dmg Mac OS X installer for Phidgets] puts all of the files and the Phidget OSA dictionary into the correct locations.  It also installs the PhidgetsOSA menu bar, described below.
 
====PhidgetsOSA Menu Bar====
 
[[Image: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.


===Use Our Examples===
Additionally, it can be used to free the lock on a Phidget or quit the PhidgetsOSA application.
 
The menu bar can be disabled by clicking 'Disable Menu', after which it will not appear again, even after restarting PhidgetsOSA. To re-enable the menu bar, run the following command in Terminal:
 
<div class="source">
<syntaxhighlight lang=bash>
sudo defaults write com.phidgets.PhidgetsOSA showmenu -bool YES
</syntaxhighlight>
</div>
 
===AppleScript Editor===


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 (e.g. XCode, Smile) should work as well and would be set up in a similar manner.
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 (e.g. XCode, Smile) should work as well and would be set up in a similar manner.


Insert many screenshots
====Use Our Examples====


===Write Your Own Code===
The first step to using our examples is to [{{SERVER}}/downloads/phidget21/examples/applescript/AppleScript.zip download them].  Once that is done, running the examples is as simple as opening them with the AppleScript Editor and hitting "Run".  Note that there are 2 examples you can use.  Either the HelloWorld example which will work with any Phidget device.  Or the example specific to your Phidget.  If you use the HelloWorld example, be sure to turn on your speakers as it has an auditory component.  While running, any messages generated by the code will appear in the "Events" tab of the pane.  For example the analog sensor readings from an interface kit appear on a regular basis:


Again, here we use AppleScript Editor
[[image:Applescriptrun.png|link=|550px]]


*First, open the AppleScript editor to create a new script.
====Write Your Own Code====
*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:
 
To start writing your own code from scratch, first, open the AppleScript editor to create a new script.
 
Then, 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 class="source"><syntaxhighlight lang=applescript>
<div class="source"><syntaxhighlight lang=applescript>
Line 47: Line 68:
</syntaxhighlight></div>
</syntaxhighlight></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. All code is then typed inside this tell block.


==Follow The Examples==
==Follow The Examples==


===Code Snippets===
By following the instructions above, you probably now have a working example and want to understand it better so you can change it to do what you want.  This teaching section has resources for you to learn from the examples and write your own.


Your main reference for writing AppleScript code will be our API information, with syntax for all of our functions:


*First, open the AppleScript editor to create a new script.
{{UsingAPhidgetInCodeGeneral|both of which are available in AppleScript|'''AppleScript API''' - Find the API under AppleScript Editor &rarr; File &rarr; Open Dictionary &rarr; PhidgetOSA}}
*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 class="source"><syntaxhighlight lang=applescript>
===Code Snippets===


  tell application "PhidgetsOSA"
Specific calls in AppleScript will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same. 
  end tell


</syntaxhighlight></div>
It may help to have the [[General Phidget Programming]] page and this section open at the same time, because they parallel each other and you can refer to the AppleScript syntax.  However, many additional concepts are covered on the General Phidget Programming page on a high level, such as using multiple Phidgets, handling errors, and different styles of programming.


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.
====Step One: Initialize and Open====


===Coding For Your Phidget===
A Phidget object will need to be declared and then opened. For example, we can declare a Phidget Interface Kit and then open it:
A Phidget object will need to be declared. For example, we can declare a PhidgetInterfaceKit:


<div class="source"><syntaxhighlight lang=applescript>
<div class="source"><syntaxhighlight lang=applescript>
   set ifkit to make new phidget interfacekit
   set ifkit to make new phidget interfacekit
 
  tell ifkit to open
</syntaxhighlight></div>
</syntaxhighlight></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.  You would use a different object name for a Temperature Sensor, Spatial, etc, if you were not using an Interface Kit.


===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.  
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:
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:
Line 91: Line 104:
</syntaxhighlight></div>
</syntaxhighlight></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 using the [[Phidget WebService]]. Other types of open include:
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:


<div class="source"><syntaxhighlight lang=applescript>
<div class="source"><syntaxhighlight lang=applescript>
  tell ifkit to open serial number 99999 server address "192.168.3.180" server port 5001
</syntaxhighlight></div>


  tell ifkit to open wait 1000 --wait for 5000ms
This connects to a Phidget Interface Kit 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.


</syntaxhighlight></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:
 
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 class="source"><syntaxhighlight lang=applescript>
<div class="source"><syntaxhighlight lang=applescript>


   tell ifkit to open serial number 99999 server address "192.168.3.180" server port 5001
   tell ifkit to open server id "TestMac" password "pw"


</syntaxhighlight></div>
</syntaxhighlight></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:


<div class="source"><syntaxhighlight lang=applescript>
The PhidgetsOSA dictionary lists all of the available modes that open provides.


  tell ifkit to open server id "TestMac" password "pw"
====Step Two: Wait for Attachment (plugging in) of the Phidget====


</syntaxhighlight></div>
Open does not necessarily allow you to use the Phidget immediately.  For example, the Phidget may not even be plugged in, and open will return just fine.  The actual plugging in of the physical Phidget is an occurrence known as attachment. 


At the end of your script, don’t forget to call close to free any locks on the Phidget.
We can handle attachment 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 modify our above open call to be more robust with the Phidget Interface Kit being possibly unplugged with:


<div class="source"><syntaxhighlight lang=applescript>
<div class="source"><syntaxhighlight lang=applescript>


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


</syntaxhighlight></div>
</syntaxhighlight></div>


Phidgets can also be freed from the [[#PhidgetsOSA Menu Bar|PhidgetsOSA Menu Bar]].
====Step Three: Do Things with the Phidget====


===Event Driven Programming===
We recommend the use of event driven programming when working with Phidgets.  
We recommend the use of event driven programming when working with Phidgets.  
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 class="source"><syntaxhighlight lang=applescript>
<div class="source"><syntaxhighlight 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}
</syntaxhighlight></div>
</syntaxhighlight></div>


Line 143: Line 146:


<div class="source"><syntaxhighlight lang=applescript>
<div class="source"><syntaxhighlight lang=applescript>
   using terms from application "PhidgetsOSA"
   using terms from application "PhidgetsOSA"
     on interfacekit sensor changed ind to val on ifkit
     on interfacekit sensor changed ind to val on ifkit
Line 149: Line 151:
     end interfacekit sensor changed
     end interfacekit sensor changed
   end using terms from
   end using terms from
</syntaxhighlight></div>
</syntaxhighlight></div>


Line 158: Line 159:
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.
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.  
Some values can be read and sent directly to the Phidget.  
For example, sensor values from the PhidgetInterfaceKit can be read with:
For example, sensor values from the PhidgetInterfaceKit can be read with:
<div class="source"><syntaxhighlight lang=applescript>
  log "The first sensor has a value of: " & first interfacekit sensor's value
</syntaxhighlight></div>
These functions can be used inside a polling loop as an alternative to event driven programming.
====Step Four: Close and Delete====
At the end of your script, don’t forget to call close to free any locks on the Phidget.


<div class="source"><syntaxhighlight lang=applescript>
<div class="source"><syntaxhighlight lang=applescript>


   log "The first sensor has a value of: " & first interfacekit sensor's value
   tell ifkit to close
  delete ifkit


</syntaxhighlight></div>
</syntaxhighlight></div>


These functions can be used inside a polling loop as an alternative to event driven programming.
Phidgets can also be freed from the [[#PhidgetsOSA Menu Bar|PhidgetsOSA Menu Bar]].
 
====Special Case: Using the same Phidget in more than one Application====


===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.  
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.
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,
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,


<div class="source"><syntaxhighlight lang=applescript>
<div class="source"><syntaxhighlight lang=applescript>
   if first phidget interfacekit exists then
   if first phidget interfacekit exists then
     set ifkit to the first phidget interfacekit
     set ifkit to the first phidget interfacekit
Line 184: Line 196:
     open ifkit
     open ifkit
   end if
   end if
</syntaxhighlight></div>
</syntaxhighlight></div>


2. The one connection per device limit does not apply when exclusively using the open [[Phidget WebService]].  Please see the [[#Step One: Initialize and Open|Step One: Initialize and Open]] section for example webservice calls.


: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.
{{MoreHowTos}}
 
===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===
[[Image: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.


==Common Problems and Solutions/Workarounds==
==Common Problems and Solutions/Workarounds==


None at this time.
None at this time.

Latest revision as of 16:56, 7 June 2017

Icon-Applescript.png Applescript is a scripting language for OS X designed primarily for interfacing between applications.


Introduction

If this is your first time working with a Phidget, we suggest starting with the Getting Started page for your specific device. This can be found in the user guide for your device. That page will walk you through installing drivers and libraries for your operating system, and will then bring you back here to use AppleScript specifically.

AppleScript is capable of using the complete Phidget API, including events. We also provide example code in AppleScript for all Phidget devices.

AppleScript can be developed with AppleScript Editor on OS X.

You can compare AppleScript with our other supported languages.

Note: Applescript is only supported with Phidgets on OS X 10.6 "Snow Leopard" and newer.

Quick Downloads

Just need the AppleScript documentation, drivers, libraries, and examples? Here they are:

Documentation

Find the API under AppleScript Editor → File → Open Dictionary → PhidgetsOSA

Example Code

Libraries and Drivers

Getting started with AppleScript

If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:

  • Make sure your libraries are properly linked
  • Go from source code to a test application as quickly as possible
  • Ensure your Phidget is hooked up properly

We offer support for developing AppleScript on OS X.

OS X

Description of Files

The Mac OS X installer for Phidgets puts all of the files and the Phidget OSA dictionary into the correct locations. It also installs the PhidgetsOSA menu bar, described below.

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.

The menu bar can be disabled by clicking 'Disable Menu', after which it will not appear again, even after restarting PhidgetsOSA. To re-enable the menu bar, run the following command in Terminal:

sudo defaults write com.phidgets.PhidgetsOSA showmenu -bool YES

AppleScript Editor

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 (e.g. XCode, Smile) should work as well and would be set up in a similar manner.

Use Our Examples

The first step to using our examples is to download them. Once that is done, running the examples is as simple as opening them with the AppleScript Editor and hitting "Run". Note that there are 2 examples you can use. Either the HelloWorld example which will work with any Phidget device. Or the example specific to your Phidget. If you use the HelloWorld example, be sure to turn on your speakers as it has an auditory component. While running, any messages generated by the code will appear in the "Events" tab of the pane. For example the analog sensor readings from an interface kit appear on a regular basis:

Applescriptrun.png

Write Your Own Code

To start writing your own code from scratch, first, open the AppleScript editor to create a new script.

Then, 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. All code is then typed inside this tell block.

Follow The Examples

By following the instructions above, you probably now have a working example and want to understand it better so you can change it to do what you want. This teaching section has resources for you to learn from the examples and write your own.

Your main reference for writing AppleScript code will be our API information, with syntax for all of our functions:

  • AppleScript API - Find the API under AppleScript Editor → File → Open Dictionary → PhidgetOSA (This is the complete set of functions you have available for all Phidgets)
  • Device Specific APIs - The one for your Phidget can be found in its user guide.

To learn the details behind opening, configuring, using, and closing your Phidget, try the General Phidget Programming page. That page also describes using the Phidget in an event-driven manner and in a traditional manner, both of which are available in AppleScript.

Code Snippets

Specific calls in AppleScript will differ in syntax from those on the General Phidget Programming page, but the concepts stay the same.

It may help to have the General Phidget Programming page and this section open at the same time, because they parallel each other and you can refer to the AppleScript syntax. However, many additional concepts are covered on the General Phidget Programming page on a high level, such as using multiple Phidgets, handling errors, and different styles of programming.

Step One: Initialize and Open

A Phidget object will need to be declared and then opened. For example, we can declare a Phidget Interface Kit and then open it:

  set ifkit to make new phidget interfacekit
  tell ifkit to open

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. You would use a different object name for a Temperature Sensor, Spatial, etc, if you were not using an Interface Kit.

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 using the Phidget WebService. Other types of open include:

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

This connects to a Phidget Interface Kit 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.

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"

The PhidgetsOSA dictionary lists all of the available modes that open provides.

Step Two: Wait for Attachment (plugging in) of the Phidget

Open does not necessarily allow you to use the Phidget immediately. For example, the Phidget may not even be plugged in, and open will return just fine. The actual plugging in of the physical Phidget is an occurrence known as attachment.

We can handle attachment 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 modify our above open call to be more robust with the Phidget Interface Kit being possibly unplugged with:

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

Step Three: Do Things with the Phidget

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.

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.

Step Four: Close and Delete

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.

Special Case: 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. Please see the Step One: Initialize and Open section for example webservice calls.

More How-To's

The General Phidget Programming page gives more information about:

Common Problems and Solutions/Workarounds

None at this time.