Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > AppleScript Question #47142: Lists in Lists

AppleScript Question #47142: Lists in Lists
Thread Tools
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Aug 6, 2001, 01:18 AM
 
Okay here is stupid question #47142, lists in lists. Okay well I know you can write stuff to empty properties..

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>property emptyProp : {}

set emptyProp to (emptyProp & <font color = red>"added"</font>)</font>[/code]

I also know that I can set stuff to lists in lists...

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>property emptyProp : {{}}

set item <font color = blue>1</font> of emptyProp to ((item <font color = blue>1</font> of emptyProp) & <font color = red>"added"</font>)</font>[/code]

But how can I write a new list to a blank list..? Like this:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>property emptyProp : {}

set source to <font color = red>"Synotic"</font>
set whatToWrite to (source & <font color = red>" "</font> & <font color = red>"text"</font>)
set emptyProp to (emptyProp & {whatToWrite}</font>[/code]

So now if I get emptyProp it'll be:

{{Synotic text}}

If I ran it again.. it'd be:

{{Synotic text}, {Synotic text}}


Why do I want to do this? Well here is my final goal.. I want to make a script for ircle.. what it will do is, every time somebody speaks.. it will check the property to see if they already have their own list.. if not it will add a list for them.. then it adds the number of words that they said something.. next time they say something.. it will add the number of words to the first number.. Get it? Well here is my goal script:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>on pubmsg() –in ircle <font color = green>this</font> means that it will respond to something someone says…
tell application “ircle”
repeat with itemx in emptyProp
<font color = green>if</font> word <font color = blue>1</font> of itemx is source then
set theyHaveAlistAlready to true
<font color = green>else</font>
set theyHaveAlistAlready to false
end <font color = green>if</font>
end repeat
<font color = green>if</font> theyHaveAlistAlready is true then
set matchIt to match “”&source&” *” with emptyProp –here I want to find the list in the list that belongs to source (the guy who said it)
set theNumb to get the last character of item matchIt of emptyProp
set theFinal to (theNumb + (length of (words of thestring)))—thestring is what they said.. i.e. “Hey guys”
set item matchIt of emptyProp to (source & “ “&theFinal&””)
end <font color = green>if</font>
end tell
<font color = green>return</font> false –I am doing <font color = green>this</font> cause I want ircle to process the event..
end pubmsg</font>[/code]

This should add the number of words that they said to what they already have.. I just wrote this all in my head… it probably won’t even compile.. it is just an idea of what I want to do. Here is how it would be activated

&lt;simulating chat room talk&gt;
Synotic: Hey guys.
&lt;/simulating chat room talk&gt;

It would check the list.. let us say item 3 of emptyProp is: {Synotic 35}
It will add the length of the words of what I said (Hey guys).. to my current number of words.. so now it is 37. 35 words already + the 2 words I uttered now.. This is probably very confusing.. so please ask questions.
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Aug 6, 2001, 05:27 AM
 
Phew, I did it.. kinda,

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>property x : {theName:{<font color = red>"<font color = blue>99999</font>"</font>, <font color = red>"Synotic"</font>}, Num:{<font color = blue>0</font>, <font color = blue>6</font>}}

tell application <font color = red>"ircle"</font>
set source to <font color = red>"Synotic"</font>
set TheMessage to length of words of <font color = red>"Hello world, how are you all?"</font>
<font color = green>if</font> theName of x contains source then
set theItemNum to match source with theName of x
set getCurrentCount to item theItemNum of Num of x
set theAddedCount to (getCurrentCount + TheMessage)
set item theItemNum of theName of x to source
set item theItemNum of Num of x to theAddedCount
<font color = green>return</font> x
<font color = green>else</font>
set theName of x to (theName of x & source)
set Num of x to (Num of x & TheMessage)
<font color = green>return</font> x
end <font color = green>if</font>
end tell</font>[/code]

That works.. if you don't have ircle just replace the app with Finder or something else that has "match". But when I put it in its final form:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>property HipScriptUsercommandList : {<font color = red>"/getcount"</font>}
property x : {theName:{<font color = red>"<font color = blue>99999</font>"</font>, <font color = red>"Synotic"</font>}, Num:{<font color = blue>0</font>, <font color = blue>6</font>}}

on usercommand(con, target, thestring, thecmd, theargs)
tell application <font color = red>"ircle"</font>
<font color = green>if</font> thecmd is <font color = red>"/getcount"</font> then
set thePerson to item <font color = blue>1</font> of theargs
<font color = green>if</font> thePerson is in theName of x then
set matchItem to match thePerson with theName of x
set whatToType to (item matchItem of Num of x) as string
InputIn(con, target, <font color = red>"/echo "</font> & thePerson & <font color = red>" has typed "</font> & whatToType & <font color = red>" words that you have seen."</font>)
<font color = green>else</font>
InputIn(con, target, <font color = red>"/echo "</font> & thePerson & <font color = red>" has not been seen typing by you."</font>)
end <font color = green>if</font>
end <font color = green>if</font>
end tell
end usercommand

on pubmsg(con, source, sourcehost, target, thestring)
tell application <font color = red>"ircle"</font>
set TheMessage to length of words of thestring
<font color = green>if</font> theName of x contains source then
set theItemNum to match source with theName of x
set getCurrentCount to item theItemNum of Num of x
set theAddedCount to (getCurrentCount + TheMessage)
set item theItemNum of theName of x to source
set item theItemNum of Num of x to theAddedCount
<font color = green>else</font>
set theName of x to (theName of x & source)
set Num of x to (Num of x & TheMessage)
end <font color = green>if</font>
end tell
<font color = green>return</font> false
end pubmsg</font>[/code]Then it doesn't work. It'll just tell me that I haven't seen anyone talking.. Even though I saw them talk.. any suggestions?

BTW, I want to have a search and replace a string handler.. someone already showed me one:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>set thestring to <font color = red>"adadadad"</font>
set searchString to <font color = red>"a"</font>
set replaceString to <font color = red>"<font color = blue>4</font>"</font>
replaceTheString(thestring, searchString, replaceString)

on replaceTheString(thestring, searchString, replaceString)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to searchString
set tempList to text items of thestring
set AppleScript's text item delimiters to replaceString
set resultString to tempList as string
set AppleScript's text item delimiters to oldDelim
<font color = green>return</font> resultString
end replaceString</font>[/code]That'll work.. but I want to be able to do this:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>set thestring to <font color = red>"adadadad"</font>
set searchString to {<font color = red>"a"</font>, <font color = red>"d"</font>}
set replaceString to {<font color = red>"<font color = blue>4</font>"</font>, <font color = red>"?"</font>}

replaceTheString(thestring, searchString, replaceString)
on replaceTheString(thestring, searchString, replaceString)
display dialog <font color = red>"Find a way to let me replace multiple items in a string damnit."</font> buttons {<font color = red>"Back away cowardly"</font>, <font color = red>"Yes Master"</font>} <font color = green>default</font> button <font color = red>"Yes Master"</font>
end replaceTheString</font>[/code]
     
Fresh-Faced Recruit
Join Date: Jul 2001
Status: Offline
Reply With Quote
Aug 6, 2001, 08:29 AM
 
Not really sure how that match term works, but one thing you might try is breaking the list up into individual names with their corresponding word counts... then you could match the name in theArgs with the names in the list using a simple repeat... something like this:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>property theList : {{name:<font color = red>"<font color = blue>99999</font>"</font>, Num:<font color = blue>3</font>}}

set thesource to <font color = red>"Synotic"</font>
set thestring to <font color = red>"Hello world, how are you all?"</font>
set theResult to usercommand(thesource, thestring) -- or, (con, target, thestring, thecmd, theargs) <font color = green>for</font> ircle...

on usercommand(thesource, thestring)
-- using the name & the message; I guess these'll be part of theArgs, but...
-- tell application <font color = red>"ircle"</font> (*wouldn't need <font color = red>"ircle"</font> here*)
set theyHaveAlistAlready to false
set TheMessage to length of words of thestring
repeat with anentry in theList
<font color = green>if</font> name of anentry is thesource then
set Num of anentry to ((Num of anentry) + TheMessage)
set theyHaveAlistAlready to true
display dialog ((name of anentry) & <font color = red>" has typed "</font> & (Num of anentry) & <font color = red>" words that you have seen."</font>)
exit repeat
end <font color = green>if</font>
end repeat
<font color = green>if</font> theyHaveAlistAlready is false then
copy {name:thesource, Num:TheMessage} to the end of theList
display dialog ((thesource) & <font color = red>" has not been seen typing by you... (until now)!"</font>)
end <font color = green>if</font>
<font color = green>return</font> theList
-- end tell
end usercommand</font>[/code]
of course, not using ircle here, but the idea would be to scan each item of the list until the name is matched, & if matched, update the word count & exit the repeat... if not matched after scanning the entire list, add the name & word count.
Just an idea...

For the search & replace, Akua Sweets has a nifty "munge" command that should do what you need. The syntax would be something like:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>set x to <font color = red>"adadadadad"</font>
set y to munge x from {<font color = red>"a"</font>, <font color = red>"d"</font>} to {<font color = red>"<font color = blue>4</font>"</font>, <font color = red>"?"</font>}
<font color = green>return</font> y -- <font color = red>"<font color = blue>4</font>?<font color = blue>4</font>?<font color = blue>4</font>?<font color = blue>4</font>?<font color = blue>4</font>?"</font></font>[/code]
sigh... UBB...

[ 08-06-2001: Message edited by: kfkel ]
     
<Synotica>
Guest
Status:
Reply With Quote
Aug 6, 2001, 05:58 PM
 
Originally posted by kfkel:
<STRONG>Not really sure how that match term works, but one thing you

&gt;&gt;The match term is a pretty standard command that is not ircle &gt;&gt;specific... I am pretty sure the Finder has it. What it basically is..
&gt;&gt;set theList to {"This", "is", "an", "example"}
&gt;&gt;set theMatch to match "an" with theList
&gt;&gt;
&gt;&gt;That'll return the number 3.. since it is the third item in the list. This &gt;&gt;is useful at times.

might try is breaking the list up into individual names with their corresponding word counts... then you could match the name in theArgs with the names in the list using a simple repeat...

&gt;&gt;Yeah, well the "match" seems to be working fine in the *first* script, &gt;&gt;it just appears that something got screwed in the second variation.

something like this:

code:
------------------------------------------------------------------------

property theList : {{name:"99999", Num:3}}

set thesource to "Synotic"
set thestring to "Hello world, how are you all?"
set theResult to usercommand(thesource, thestring) -- or, (con, target, thestring, thecmd, theargs) for ircle...

on usercommand(thesource, thestring)
-- using the name & the message; I guess these'll be part of theArgs, but...
-- tell application "ircle" (*wouldn't need "ircle" here*)
set theyHaveAlistAlready to false
set TheMessage to length of words of thestring
repeat with anEntry in theList
if name of anEntry is theSource then
set Num of anentry to ((Num of anEntry) + TheMessage)
set theyHaveAlistAlready to true
display dialog ((name of anentry) & " has typed " & (Num of anentry) & " words that you have seen.")
exit repeat
end if
end repeat
if theyHaveAlistAlready is false then
copy {name:thesource, Num:TheMessage} to the end of theList
display dialog ((thesource) & " has not been seen typing by you... (until now)!")
end if
return theList
-- end tell
end usercommand
------------------------------------------------------------------------

&gt;&gt;Well my pubmg handler is the logger. I don't want feedback &gt;&gt;everytime someone types, lol. I just want to be able to get the &gt;&gt;current count on someone.. /getcount Synotic

of course, not using ircle here, but the idea would be to scan each item of the list until the name is matched, & if matched, update the word count & exit the repeat... if not matched after scanning the entire list, add the name & word count.
Just an idea...

&gt;&gt;While I understand the script and it makes sense... that wasn't &gt;&gt;really the problem. As you can see in my *first* script, it runs. Just &gt;&gt;stick it in your script editor and hit run a couple times. It adds to &gt;&gt;Synotic's count. Then change the name of the source, it'll add, then &gt;&gt;keep adding to the count. So I just figured it had to do with my &gt;&gt;logger. Cause pubmsgs(public messages) do NOT include what I &gt;&gt;say. Thus it doesn't count.. I can still do /getcount Synotic and it'll &gt;&gt;read from the list. i.e. "6". So it has to be something wrong in the &gt;&gt;logger(pubmsg handler). But I'll still try your suggestion.

For the search & replace, Akua Sweets has a nifty "munge" command that should do what you need. The syntax would be something like:

code:
------------------------------------------------------------------------

set x to "adadadadad"
set y to munge x from {"a", "d"} to {"4", "?"}
return y -- "4?4?4?4?4?"
------------------------------------------------------------------------

&gt;&gt;Yeah, well, this is for a calculator script. I want to clear out any &gt;&gt;spaces. I already did it with a scripting addition in my !calc function &gt;&gt;in IRC.. with HipTools: hipTranslateSubstrings "1 + 3 * 4 " &gt;&gt;replacing any of {" ", "+"} with corresponding {"", "-"}
&gt;&gt;
&gt;&gt;But I kinda wanted to stay away from using scripting additions.. Oh, &gt;&gt;I realized I already cleared out spaces as they were only one item.. &gt;&gt;but in my one that uses a scripting addition, it'll filter out all the &gt;&gt;GOOD characters leaving the bad ones, then it tells them what they &gt;&gt;are..

sigh... UBB...

&gt;&gt;Anyways, I'll try your way and see what happens.
</STRONG>
     
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
Aug 6, 2001, 09:03 PM
 
Yes... with ircle that first script seems to be working fine, but the "match" term appears to be "ircle" specific... at least it's not in the Finder or Standard Additions dictionaries. And, since presumably theargs are defined earlter in the script & to test the logging function, if the property theargs is redefined in the usercommand handler, like:
set theargs to {"99999"}
it seems to work as expected. So the trouble may be in matching the class & content of item 1 of theargs with that of the corresponding item in theName of x. If it's still not matching, maybe try returning "thePerson" to see what's up. Sometimes it's just a matter of specifying the class of the item (item 1 of theargs as string).
In any case, there doesn't seem to be any real need to try my original suggestion, since the posted part of your script seems fine otherwise.

For a pure applescript implementation of search & replace for multiple noncontiguous items in a string, your setup of the list in your original script lends an idea... combined with your search & replace script, although not especially neat, this should work:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>set thestring to <font color = red>"abcdabcdabcdabcd"</font>
set x to {searchstring:{<font color = red>"a"</font>, <font color = red>"d"</font>}, replacestring:{<font color = red>"<font color = blue>4</font>"</font>, <font color = red>"?"</font>}}
replaceTheString(x, thestring)

on replaceTheString(x, thestring)
set i to <font color = blue>1</font>
repeat with anitem in searchstring of x
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to anitem
set tempList to text items of thestring
set AppleScript's text item delimiters to item i of replacestring of x
set thestring to tempList as string
set AppleScript's text item delimiters to oldDelim
set i to (i + <font color = blue>1</font>)
end repeat
<font color = green>return</font> thestring -- <font color = red>"4bc?4bc?4bc?4bc?"</font>
end replaceTheString</font>[/code]
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:10 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2