I want to write this script, but i cant seem to get started. I want the script to find the number of times i use a word starting with certain letters, and then print those words. For example, If i set it to find the words beginning with “fr”, i might get french, free, freckle, depending on what i wrote. Is this possible?
set thetext to "foobar bazblee foobar"
set allwords to (every word of thetext)
set wordcount to 0
repeat with i in my allwords
if i starts with "foo" then set wordcount to wordcount + 1
end repeat
wordcount
you could also say:
.
if i contains "foo" then set wordcount to wordcount + 1
.
or you could say:
.
if i ends with "foo" then set wordcount to wordcount + 1
.
Starting with an empty list, and then filling it as you go will give the words themselves, which is what I believe you are interested in:
set thetext to "foobar bazblee foobar fobar"
set sel_words to {}
set allwords to (every word of thetext)
set wordcount to 0
repeat with i in my allwords
if i starts with "fo" then
set end of sel_words to i as Unicode text--If the word fits the test, add it to the list
set wordcount to wordcount + 1--Counts the total words that fit the test
end if
end repeat
wordcount & return & sel_words
tell application "TextEdit"
activate
choose file "Get word info for this file:" without invisibles
open (result)
set theWords to every word of front document whose first character is "f" and second character is "r"
close front document
quit
end tell
return {wordCount:(count theWords), wordList:theWords}
set theText to "froo bar baz
Frub fig froo
frub Frub frub"
set wordPattern to "fr.*?" -- careful
do shell script "echo " & quoted form of theText & " | perl -0777 -e " & quoted form of ("
$_ = <STDIN>;
@lst = sort {lc $a cmp lc $b} m/\\b(" & wordPattern & ")\\b/ig;
push @lst, '';
$wrd = shift @lst;
while (@lst) {
$i = 1;
$i++ while (lc($nxt = shift @lst) eq lc $wrd);
print \"$i\\t$wrd\\n\";
$wrd = $nxt;
}")
(* -->
"2 froo
4 Frub"
*)
Preserves the case of [the first instance of] each word found, which is why it’s a bit longer (uniq’s no good for this). It’s still not unicode-aware though (Perl’s unicode support is awkward and I can’t be bothered to sort it - I’d normally use Python anyway).