Re: Count word's occurences
Re: Count word's occurences
- Subject: Re: Count word's occurences
- From: Nigel Garvey <email@hidden>
- Date: Tue, 09 Sep 2014 21:09:53 +0100
Guido Tangorra wrote on Tue, 09 Sep 2014 15:22:28 +0200:
>In the idle handler I want to add a fruit counter so the count is made
at
>runtime (and attached to the JSON response) and not after saving the
>document.
Hi Guido.
Well all _I_ can offer in this case is the method I posted before, which
is probably no faster than what you're using already. :( As Shane
pointed out in one of his posts, most of the time is taken up by the
writing of the new line into the document. Finding the number to use
doesn't take much time at all.
However, if your window's showing line numbers, you can make the writes
20% to 25% faster by hiding the line numbers, as TextWrangler then
doesn't have to update them every time it adds a new line. It would be
great if the status bar could be hidden too, but this doesn't seem to be
possible with TextWrangler 4.5.10, even though the scripting dictionary
suggests that it is. What you _can_ do is to go to TextWrangler's
Preferences before running the script and uncheck all the Text Status
Bar options. With these and line numbers disabled, writes are over twice
as fast!
Here's how I think your script might look, though obviously I haven't
been able to test it in context with the "JSON Helper" stuff. I'm not
too sure about the half-second idle time either:
global myFruit, mySource
set theDocName to "My Fruit List"
set mySource to "htt
?p?
://..."
tell application "TextWrangler"
activate
set myDocument to (make new text window with properties {name:theDocName})
set show line numbers of myDocument to false
end tell
on idle
tell application "JSON Helper" to set myFruit to fetch JSON from mySource
set fruitQuantity to my Counter(myFruit)
tell application "TextWrangler" to make new line at end of text of text window theDocName with data (myFruit & space & fruitQuantity)
return 0.5
end idle
on Counter(theFruit)
tell application "TextWrangler"
-- Find the previous occurrence of the fruit.
set searchResult to (find (theFruit & " [0-9]+") searching in text of text window theDocName options {search mode:grep, backwards:true, match words:true})
if (searchResult's found) then
-- If one is found, add 1 to the number with it and return the result.
return (word -1 of searchResult's found text) + 1
else
-- Otherwise this is will be the first instance of this fruit in the document.
return "1"
end if
end tell
end Counter
NG
_______________________________________________
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