Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rmatias

macrumors newbie
Original poster
Dec 22, 2010
2
0
Hi:

I am a newie with Applescript. I have just finish an example that I have found in the internet but it simple does not work :(


tell application "TextEdit"
open alias ":Users:ricardomatias:Desktop:test.txt"
set word_list to every word of document 1
end tell

tell application "TextEdit"
set word_list to every word of document 1
end tell

set word_frequency_list to {}

repeat with the_word_ref in word_list
set the_current_word to contents of the_word_ref
set word_info to missing value

repeat with record_ref in word_frequency_list
if the_word of record_ref = the_current_word then
set word_info to contents of record_ref
exit repeat
end if
end repeat

if word_info = missing value then
set word_info to {the_word:the_current_word, the_count:1}
set end of word_frequency_list to word_info
else
set the_count of word_info to (the_count of word_info) + 1
end if
end repeat
return word_frequency_list

set the_report_list to {}
repeat with word_info in word_frequency_list
set end of the_report_list to quote & the_word of word_info & ¬
quote & " appears " & the_count of word_info & " times."
end repeat

set AppleScript's text item delimiters to return
set the_report to the_report_list as text

tell application "TextEdit"
make new document with properties {name:"Word Frequencies", text:the_report}
end tell

I am not able to generate the new document with the results.

Can anyone help me?!

All the best
Ricardo
 
Okay after a glance over of this AS it looks like you are wanting it to open a document, perform a word count, and create a new document telling you the word count in plain text. Is that about right?

You need to remove the line:

Code:
return word_frequency_list

IIRC return is used to send the value outside of the script to the app/whatever that called it. In basic terms it steals the value away from the rest of the script.

You are covered by the variables that you set.
 
Last edited:
Thanks; it works (almost) perfectly!

You are right I am trying to make some king of code that:

. opens a txt file
- perform a frequency word count
- create a new document with results

What I am not able to is to define in the document where to look at. For example, when having this text:


1: Day JA, Stecco C, Stecco A. Application of Fascial Manipulation technique in
chronic shoulder pain--anatomical basis and clinical implications. J Bodyw Mov
Ther. 2009 Apr;13(2):128-35. Epub 2008 Jun 24. PubMed PMID: 19329049.


2: Walker MJ. Manual physical therapy examination and intervention of a patient
with radial wrist pain: a case report. J Orthop Sports Phys Ther. 2004
Dec;34(12):761-9. PubMed PMID: 15643731.


What I would like is that the code counts only the words between "1:" or "2:" to the first ".". In this example it would be:


"1: Day JA, Stecco C, Stecco A."
"2: Walker MJ."

If I the code was perfectly written it would count the frequency of:

"Day JA"
"Stecco C"
"Stecco A"
"Walker MJ"

...but for sure this I am not able to do!!!!

Is it to complicated...from a code pointo-of-view?

THANKS!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.