• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Use AS operators dinamically
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use AS operators dinamically


  • Subject: Re: Use AS operators dinamically
  • From: Takaaki Naganoya <email@hidden>
  • Date: Mon, 4 Jan 2010 19:38:59 +0900

This is 10 minutes' work.
AppleScript's language specification is very limited. But our imagination is unlimited.

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



 _______________________________________________
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

References: 
 >Re: Use AS operators dinamically (From: Matt Neuburg <email@hidden>)

  • Prev by Date: Re: Does the variable N have special status?
  • Next by Date: Re: Use AS operators dinamically
  • Previous by thread: Re: Use AS operators dinamically
  • Next by thread: Re: Use AS operators dinamically
  • Index(es):
    • Date
    • Thread