Dynamic Properties and Commands
Sometimes it may be desirable to use an object property or command without knowing its name ahead of time. For example, code might want to sometimes deal with selected table rows and other times with columns. Assuming that $tableSel is an existing TableSelection object, consider the code:
$whatToSum = 'rows'
If $whatToSum == 'rows'
$range = $tableSel.rowRange
Else
$range = $tableSel.columnRange
End
$count = $range.length
Prompt "The selection has $count elements."
Rather than using an If block, it might be simpler if the name of the property was stored in a variable:
$propertyName = 'rowRange'
$range = $tableSel.getProperty($propertyName)
$count = $range.length
Prompt "The selection has $count elements."
The following commands can be used with any type of object to execute any command it supports or to read/write any of its properties:
Dynamic Object Commands
.getProperty propertyName v1.1
Returns the value of the property with the given name.
.setProperty propertyName, newPropertyValue v1.1
Sets the value of the property with the given name. The property must not be read-only.
.doCommand commandName, [argument1] ... [argumentN] v1.1
Executes a single object command with the given name. The other arguments are passed directly to the named command. The doCommand command can also be used to read (but not write) object properties:
$range = Range.new(2,4)
$loc = $range.doCommand('location')
Dynamic Object Command Examples
Reading and writing an object property:
$range = Range.new(0,2)
$property = 'location'
$loc = $range.getProperty($property)
$loc += 10
$range.setProperty($property, $loc)
Sending an object command:
$text = 'hi'
$command = 'attributesAtIndex'
$attributes = $text.doCommand($command, 1)
Previous Chapter Object Equality |
<< index >> |
Next Chapter Collection Objects |