Top  | Previous | Next

Advanced / runScript

runScript(scriptFunction, [pollRate])

Runs a single line of Python code as an expression. If a poll rate is specified, the function will be run repeatedly at the poll rate. This is a very powerful way for you to add extensions to the expression language.

 

For example, one could write a project script module function called app.weather.getTempAt(zip) that queried a web service for the current temperature at a given zipcode, and then bind the value of a label to the return value of that function.

 

You could implement app.weather.getTempAt(zip) with this Python script:

# This function would query Yahoo Weather for the temperature at
# the given zipcode and find the temperature using a regular expression
def getTempAt(zipCode):
   import system
   import re #Regular Expression library 
   

   yahooURL = "http://xml.weather.yahoo.com/forecastrss?p="
   response = system.net.httpGet(yahooURL + str(zipCode))
   
   # NOTE - if you've never seen regular expressions before, don't worry, they look
   # confusing even to people who use them frequently.
   pattern = re.compile('.*?<yweather:condition (.*?)/>', re.DOTALL)
   match = pattern.match(response)
   if match:
      subText = match.group(1)
      temp = re.compile('.*?temp="(.*?)"').match(subText).group(1)
      return int(temp)
   else:
      system.gui.errorBox("Yahoo weather service changed")
      return -1

 

And then you could use this expression to bind a property value to the weather:

runScript("app.weather.getTempAt('95818')", 15000)

... This would bind a property to the temperature in sunny Sacramento, CA, and would refresh itself every 15 seconds.

 

See also:

About Python