Last week I posted an Applescript question about accessing a slot of an object using a variable. Thanks to all for the great suggestions (particularly StevanK’s comments).
This week I’m wanting to know how to pass a user-defined function into a ˜run script’ command, or another way to get analogous functionality.
I know you can pass a parameter (e.g., an object) into a ˜run script’ command like this:
tell application “iTunes”
run script (“on run{cTrack}
set " & slotName & " of cTrack to "” & slotVal & “"
end”) with parameters {cTrack}
end tell
But can a parameter passed (cTrack in this example) be a function? Specifically, I want to run something like:
tell application “iTunes”
run script(“on run{cTrack,cFunction}
set " & slotName & " of cTrack to "” & “cFunction (” & “"” & slotVal & “"” & “)
end”) with parameters{cTrack,cFunction}
end tell
Where ˜cFunion’ is a user-defined function outside of the ‘run script’ command.
I actually haven’t tried that, but my guess is that it won’t work, due to the fact that ˜cFunction’ is not a parameter (variable), it’s a function.
I’m also thinking of nesting the above script command within a tell ’ application “cFunction” ’ command, analogous to how I nested the function within a 'tell application “iTunes” command… this way the script ‘sees’ the function ‘cFunction’ as well… but not sure if this would work either (i.e., can we extend the database of ‘tell application X’ commands by defining our own???)
Any ideas anyone? StefanK (you had some great suggestions last time)?
For lispers, passing a function as a variable to another function is done directly (i.e., functions are treated just like constants and variables in lisp).
In Matlab, these are called ˜function functions’, and are defined by the @ command.
I’m hoping Applescript has something similar.
-Clayton