script for counting text items in BBEdit

Hi,

I am very new to Applescript and would just like to create a simple script that takes a text file containing a list of search terms (one on each line), finds the number of times that term is used in a document, then outputs the number of each term found to another text file. I just can’t figure out how to get the script to “count” the number of lines in the dialog following the “Find All” command in BBEdit.

Thanks in advance.

Something like this; doing the whole job directly in AppleScript. I suspect this is sensitive to capitalization.


(* Suppose your Search Term file looked like this, and was on the desktop called "TTF.txt"

now
later
before
sooner
ages
eons
was
*)

set SrchTrms to paragraphs of (read alias ((path to desktop folder as text) & "TTF.txt"))

-- that's a list of search terms

(* Now suppost your text looked like this, and was on the desktop called "Main.txt"

now is the time for all good men to come to the party later rather than sooner. Its been eons, positively ages and ages, since we've seen you. Last time was before you were married and that was before I was.
*)

-- To count the occurrences of each search term in TTF.txt as found in Main.txt:
set tCounts to {}
set Main to (read alias ((path to desktop folder as text) & "Main.txt"))
set tid to AppleScript's text item delimiters
repeat with T in SrchTrms
	set AppleScript's text item delimiters to T
	set temp to count (text items of Main)
	set end of tCounts to {contents of T, temp - 1}
end repeat
set AppleScript's text item delimiters to tid
--> {{"now", 1}, {"later", 1}, {"before", 2}, {"sooner", 1}, {"ages", 2}, {"eons", 1}, {"was", 3}}

This looks very good. I would also like the results placed in a text file, say results.txt on the desktop. How would that be done?

Thank you very much, Adam. Your help and all of the tutorials will be very helpful as I learn more about how to implement AppleScript into my computational work.

This has not been tested - I didn’t set up the files for it to read from - but it goes like this (where we write it out to our file instead of putting it at the end of a list.

-- Make a list of the terms
set SrchTrms to paragraphs of (read alias ((path to desktop folder as text) & "TTF.txt"))
-- To count the occurrences of each search term in TTF.txt as found in Main.txt:
set tCounts to {}
set Main to (read alias ((path to desktop folder as text) & "Main.txt"))
set Rfile to open for access ((path to desktop as text) & "Results.txt") with write permission
set eof of Rfile to 0 -- erases the file if there was something in it. Leave out to append.
try
	set tid to AppleScript's text item delimiters
	repeat with T in SrchTrms
		set AppleScript's text item delimiters to T
		set temp to count (text items of Main)
		write (contents of T & tab & tab & temp - 1 & return) to Rfile -- one per line.
	end repeat
	set AppleScript's text item delimiters to tid
	close access Rfile
on error -- put things in order if something screws up
	set AppleScript's text item delimiters to tid
	close access Rfile
end try
-- with a few more steps they could have been sorted by word or size as well.

Adam,

Thank you so much for your help. This script works great and I would recommend it for anyone else looking for the same functionality.