Re: Use AS operators dinamically
Re: Use AS operators dinamically
- Subject: Re: Use AS operators dinamically
- From: Matt Neuburg <email@hidden>
- Date: Tue, 05 Jan 2010 18:53:26 -0800
- Thread-topic: Use AS operators dinamically
On Mon, 4 Jan 2010 19:38:59 +0900, Takaaki Naganoya <email@hidden>
said:
>This is 10 minutes' work.
>AppleScript's language specification is very limited. But our imagination is
unlimited.
Oh, yeah, like you're really persuading me. Now let's try it in a language
that actually does this kind of thing for a living. Instead of "10 minutes'
work", it's 5 seconds work:
def apply(target, op, comp)
target[its.send(op, comp)].get
end
All done! This meets Hagimeno's original request for a completely dynamic
dispatch, where we can start with
set myArray to {myVar, "starts with", "john"}
and use myArray to perform the desired operation and comparand on the
specified target. To prove it, I'll actually use "apply()" in exactly the
way Hagimeno specified:
target = app("TextEdit").documents[1].words
myArray = [target, :begins_with, "t"]
There's my array, all ready for use. So let's use it:
p apply(*myArray)
Presto! The result is ["this", "test"], which is correct (the original
document reads "this is a test").
When you compare that with what is quoted below, you see what I meant when I
said that some languages do this kind of thing easily but AppleScript is not
one of them. Ladies and gentlemen of the jury, I rest my case.
m.
>
><AppleScript>
>set myVar to {"John Smith", "Mike Smith", "Michael Jackson"}
>set myArray to {myVar, "ends with", "Smith"}
>set aList to retDynamicOperatedData(myArray) of me
>--> {"John Smith", "Mike Smith"}
>
>on retDynamicOperatedData(myData)
> try
> set aVar to contents of item 1 of myData
> set operatorCmd to contents of item 2 of myData
> set operatorStr to contents of item 3 of myData
> on error
> return {}
> end try
>
> --Command Check
> if operatorCmd is not in {"starts with", "ends with", "contains"} then return
{}
>
> --Make Dynamic Script
> set aScript to "set aList to " & listToText(aVar) of me & "
> set rList to {}
> repeat with i in aList
> set j to contents of i
> if j " & operatorCmd & " \"" & operatorStr & "\" then
> set the end of rList to j
> end if
> end repeat
> return rList
> "
> try
> set aRes to (run script aScript)
> on error
> set aRes to {}
> end try
>
> return aRes
>
>end retDynamicOperatedData
>
>--List to String Sub-routine
>on listToText(aList)
> set listText to {"{"}
> set quotChar to ASCII character 34
> set firstFlag to true
> repeat with i in aList
> set j to contents of i
> set aClass to class of i
> if (aClass = integer) or (aClass = number) or (aClass = real) then
> set the end of listText to (getFirst(firstFlag) of me & j as text)
> set firstFlag to false
> else if (aClass = string) or (aClass = text) or (aClass = Unicode text) then
> set the end of listText to ((getFirst(firstFlag) of me & quotChar & j as
text) & quotChar)
> set firstFlag to false
> else if aClass is list then
> set the end of listText to (getFirst(firstFlag) of me & listToText(j))
--recursive call
> set firstFlag to false
> end if
> end repeat
> set the end of listText to "}"
> set listText to listText as text
> return listText
>end listToText
>
>on getFirst(aFlag)
> if aFlag = true then return ""
> if aFlag = false then return ","
>end getFirst
></AppleScript>
>
>On 2010/01/04, at 4:52, Matt Neuburg wrote:
>
>> On Fri, 1 Jan 2010 20:15:48 +0000 (GMT), Hagimeno <email@hidden> said:
>>> Hi,
>>>
>>> I would like to know if and how is possible to use the AS operators like
starts
>> with, contains, ends with, starting from them as "string".
>>> Suppose you have an array like this that contains the variable where to
search,
>> the operator and the string to search:
>>>
>>> set myArray to {myVar, "starts with", "john"}
>>>
>>> I would like to be able to search in myWar using the operator starts with
that
>> is described as string but need to be real operator.
>>>
>>> Is possible to do that?
>>
>> Many modern scripting languages can easily do this sort of thing, but
>> AppleScript is not one of them. m.
>>
>> --
>> matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
>> A fool + a tool + an autorelease pool = cool!
>> AppleScript: the Definitive Guide - Second Edition!
>> http://www.tidbits.com/matt/default.html#applescriptthings
>>
>>
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> AppleScript-Users mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>>
m
>> Archives: http://lists.apple.com/archives/applescript-users
>>
>> This email sent to email@hidden
>
>--
>Takaaki Naganoya
>Piyomaru Software
>http://piyo.piyocast.com
>email@hidden
>
>PiyoCast Web (Podcasting with Music!)
>http://www.piyocast.com
>Free AppleScript Library "AS Hole"
>http://www.piyocast.com/as/
>
>
>
>
>
>
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
http://www.tidbits.com/matt/default.html#applescriptthings
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden