parsing an NFL rookie draft file

hello,

preparing for my fantasy football rookie draft here and i’m trying to parse out the offensive players from a file i have. ultimately i’d like to have text editor color the text based on position, but i’ve stumbled a bit on just parsing things out properly. here is round one of the file:

here is the problem i currently have: when i try to find text that is capitalized (RB, QB, WR, TE, or K) so i can grab the line, AppleScript can’t seem to differentiate between uppercase and lowercase letters, even if i specify the ASCII letters. here is my code:


property RBList : {((ASCII character 82) & (ASCII character 66))}
property QBList : {((ASCII character 81) & (ASCII character 66))}
property WRList : {((ASCII character 87) & (ASCII character 82))}
property TEList : {((ASCII character 84) & (ASCII character 69))}
property KList : {(ASCII character 75)}

set fileChosen to choose file
set accessFile to open for access fileChosen without invisibles
set theInfo to read accessFile
set theLine to every paragraph of theInfo as list
set myArray to {}

repeat with i in theLine
	if i contains RBList then
		display dialog i
		set myArray to myArray & i
	else if i contains QBList then
		--display dialog i
		set myArray to myArray & i
	else if i contains WRList then
		--display dialog i
		set myArray to myArray & i
	else if i contains TEList then
		--display dialog i
		set myArray to myArray & i
	else if i contains KList then
		--display dialog i
		set myArray to myArray & i
	end if
end repeat

close access accessFile

return myArray

you’ll notice that when you have the debug statement “display dialog” set for RB, the second one found is Jay Cutler, a QB who went to Vanderbuilt, and Vanderbuilt has lowercase “rb” in it.

NOTE: yeah, i’m screwing around at home on the weekend with AS. but i’d really like to know why this does not work.

Hi waltr,

AppleScript normally ignores case for comparisons. You can change that by adding “considering case … end considering” between your tests.

property RBList : {"RB"}
property QBList : {"QB"}
property WRList : {"WR"}
property TEList : {"TE"}
property KList : {"K,"}

set fileChosen to choose file
set accessFile to open for access fileChosen without invisibles
set theInfo to read accessFile
set theLine to every paragraph of theInfo as list
set myArray to {}

considering case
repeat with i in theLine
	if i contains RBList then
		display dialog i
		set myArray to myArray & i
	else if i contains QBList then
		--display dialog i
		set myArray to myArray & i
	else if i contains WRList then
		--display dialog i
		set myArray to myArray & i
	else if i contains TEList then
		--display dialog i
		set myArray to myArray & i
	else if i contains KList then
		--display dialog i
		set myArray to myArray & i
	end if
end repeat
end considering

close access accessFile

return myArray

I also changed the test for K to “K,”. I 'm guessing K always occurs before a comma. This way, it’ll ignore any names that start with K.

Gary

thanks gary,

i don’t usually parse things–this works perfectly.

BTW–i could have skipped the whole mess by realizing that each has a trailing comma, just like what you said for K. in fact, it should be space letter letter comma. except kickers, which is space letter comma, of course.

EDITED–because of the one exeption in my BTW. K’s, of course.

Hi guys.

Unless you specifically need to ignore case, text comparisons in a ‘considering case’ statement are executed faster, anyway - so it’s a good idea to include one for this kind of stuff.

Here’s a slightly shorter and faster version of the routine:

set l to {}
considering case
	repeat with i in paragraphs of (read (choose file of type "public.plain-text" without invisibles) from 1)
		if " RB, " is in i or " QB, " is in i or " WR, " is in i or " TE, " is in i or " K, " is in i then set l's end to i's contents
	end repeat
end considering
l

I was also wondering how to iterate lines in a file. That example is perfect kai.

Hi,

If you eventually want to color paragraphs that contain text, you may be able to do something like this:

tell app “TextEdit”
set color of every paragraph of front document where it contains " QB," to {0,0,2000}
end tell

I didn’t test it out yet, because wasn’t sure on your ultimate goal.

Edited: I was going to sleep a while and thought that if you want to do this, then you might need to make new attribute runs.

gl,

hi kel,

i read your post after i decided to go a different way. check this out and let me know what you think.

since this is an RTF file, i made an RTF file with some lines and colored 5 of them. then i copied the header with TextWrangler and added it as a property to my script, to be written out to my output file. then i found the correct tags for the color scheme i’d imagined and, viola:


property RBList : " RB,"
property QBList : " QB,"
property WRList : " WR,"
property TEList : " TE,"
property KList : " K,"
property theHeader : "{\\rtf1\\mac\\ansicpg10000\\cocoartf824\\cocoasubrtf110
{\\fonttbl\\f0\\fswiss\\fcharset77 Helvetica;}
{\\colortbl;\\red255\\green255\\blue255;\\red255\\green0\\blue0;\\red0\\green255\\blue0;\\red0\\green0\\blue255;
\\red128\\green0\\blue128;\\red153\\green102\\blue51;}
\\margl1440\\margr1440\\vieww9000\\viewh8400\\viewkind0
\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\ql\\qnatural\\pardirnatura

\\f0\\fs24 \\cf0 1ST ROUND\\
\\
" & {space}


set fileChosen to choose file
set thePath to POSIX path of fileChosen
set myPathArray to {}

set ASTD to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set howMany to (count text items of thePath)
set i to 2
repeat while i < howMany
	if i is "/" then
		set myPathArray to myPathArray
	else
		set myPathArray to myPathArray & text item i of thePath
	end if
	set i to (i + 1)
end repeat
set AppleScript's text item delimiters to ASTD

set myFolderPath to path to startup disk as Unicode text
set howMany to (count text items of myPathArray)
set ASTD to AppleScript's text item delimiters
set AppleScript's text item delimiters to {space}
set i to 1
repeat while i is not (howMany + 1)
	set myFolderPath to (myFolderPath & text item i of myPathArray as string) & ":"
	set i to (i + 1)
end repeat
set AppleScript's text item delimiters to ASTD
--display dialog myFolderPath

--set folderChosen to folder containing fileChosen
set accessFile to open for access fileChosen without invisibles
set theInfo to read accessFile
set theLine to every paragraph of theInfo as list
set myArray to {}

set outputFile to myFolderPath & "offense.rtf"
set writeFile to open for access outputFile with write permission
set writeHeader to write theHeader to writeFile

set numSection to 1

considering case
	repeat with j in theLine
		if j starts with "2" and numSection is 1 then
			set myArray to myArray & "\\
\\
\\f0\\fs24 \\cf0 2nd ROUND\\
\\
"
			set numSection to (numSection + 1)
		else if j starts with "3" and numSection is 2 then
			set myArray to myArray & "\\
\\
\\f0\\fs24 \\cf0 3rd ROUND\\
\\
"
			set numSection to (numSection + 1)
		else if j starts with "4" and numSection is 3 then
			set myArray to myArray & "\\
\\
\\f0\\fs24 \\cf0 4th ROUND\\
\\
"
			set numSection to (numSection + 1)
		else if j starts with "5" and numSection is 4 then
			set myArray to myArray & "\\
\\
\\f0\\fs24 \\cf0 5th ROUND\\
\\
"
			set numSection to (numSection + 1)
		else if j starts with "6" and numSection is 5 then
			set myArray to myArray & "\\
\\
\\f0\\fs24 \\cf0 6th ROUND\\
\\
"
			set numSection to (numSection + 1)
		else if j starts with "7" and numSection is 6 then
			set myArray to myArray & "\\
\\
\\f0\\fs24 \\cf0 7th ROUND\\
\\
"
			set numSection to (numSection + 1)
		end if
		if j contains RBList then
			--display dialog i
			set myArray to myArray & "\\cf2" & j & "\\cf0 \\
"
		else if j contains QBList then
			--display dialog i
			set myArray to myArray & "\\cf3" & j & "\\cf0 \\
"
		else if j contains WRList then
			--display dialog i
			set myArray to myArray & "\\cf4" & j & "\\cf0 \\
"
		else if j contains TEList then
			--display dialog i
			set myArray to myArray & "\\cf5" & j & "\\cf0 \\
"
		else if j contains KList then
			--display dialog i
			set myArray to myArray & "\\cf6" & j & "\\cf0 \\
"
		end if
	end repeat
end considering

set finalBody to {}
set ASTD to AppleScript's text item delimiters
set AppleScript's text item delimiters to {space}
set my finalBody to myArray as string
set writeBody to write finalBody to writeFile
set AppleScript's text item delimiters to ASTD


set writeEnd to write "}" to writeFile
close access writeFile

at this point the script does a lot of things. i went to great effort to create the output file in the same directory as the source–this could probably be refined though. NOTE: this will only work on the boot drive, although i do see how this could be easily changed.

here is the whole file if you want to test it out. it should work on both RTF and plain text files:

sorry the script is not commented. if anyone has a question about what i’m trying to do, just post here & i’ll try to answer it.

cheers!

This should produce similar results, waltr:

property searchList : {" RB, ", " QB, ", " WR, ", " TE, ", " K, "}
property searchCount : count searchList
property theHeader : "{\\rtf1\\mac\\ansicpg10000\\cocoartf824\\cocoasubrtf110
{\\fonttbl\\f0\\fswiss\\fcharset77 Helvetica;}
{\\colortbl;\\red255\\green255\\blue255;\\red255\\green0\\blue0;\\red0\\green255\\blue0;\\red0\\green0\\blue255;
\\red128\\green0\\blue128;\\red153\\green102\\blue51;}
\\margl1440\\margr1440\\vieww9000\\viewh8400\\viewkind0
\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\ql\\qnatural\\pardirnatura

\\f0\\fs24 \\cf0 1ST ROUND\\
\\
 "
set fileChosen to choose file without invisibles
set theLine to paragraphs of (read fileChosen from 1)
set myArray to {}
considering case
	repeat with j in theLine
		if (count j) > 0 then
			if j ends with "ROUND" and j does not start with "1" then
				set myArray's end to "\\
\\
\\f0\\fs24 \\cf0 " & j & "\\
\\
"
			else
				repeat with i from 1 to searchCount
					if my searchList's item i is in j then
						set myArray's end to "\\cf" & i + 1 & space & j & "\\cf0 \\
"
						exit repeat
					end if
				end repeat
			end if
		end if
	end repeat
end considering
set text item delimiters to space
set finalBody to theHeader & myArray & "}"
set text item delimiters to {""}
tell application "Finder" to set outputFile to (fileChosen's folder as Unicode text) & "offense.rtf"
set writeFile to open for access outputFile with write permission
set eof writeFile to 0
write finalBody to writeFile
close access writeFile

Hi waltr,

Good idea using an example to get the header and nice script.

Here’s an example of using the filter reference form in TextEdit if you want to look at it. It writes your original example text and saves it to rtf on the desktop.


property pos_list : {" RB,", " QB,", " WR,", " TE,", " K,"}
property color_list : {{65535, 0, 0}, {0, 65535, 0}, {0, 0, 65535}, {65535, 65535, 0}, {65535, 0, 65535}}
property theHeader : "{\\rtf1\\mac\\ansicpg10000\\cocoartf102
{\\fonttbl}
{\\colortbl;\\red255\\green255\\blue255;}
\\margl1440\\margr1440\\vieww12620\\viewh9000\\viewkind0
}"
--
set t to "1.01 Houston - Mario Williams, DE, NC State
1.02 New Orleans - Reggie Bush, RB, USC
1.03 Tennessee - Vince Young, QB, Texas
1.04 NY Jets - D'Brickashaw Ferguson, OT, Virginia
1.05 Green Bay - AJ Hawk, LB, Ohio State
1.06 San Francisco - Vernon Davis, TE, Maryland
1.07 Oakland - Michael Huff, S, Texas
1.08 Buffalo - Donte Witner, S, Ohio State
1.09 Detroit - Ernie Sims, OLB, Florida State
1.10 Arizona - Matt Leinart, QB, USC
1.11 Denver (from St Louis) - Jay Cutler, QB, Vanderbilt
1.12 Baltimore (from Cleveland) - Haloti Ngata, DT, Oregon
1.13 Cleveland (from Baltimore) - Kamerion Wimbley, OLB, Florida State
1.14 Philadelphia - Broderick Bunkley, DT, Florida State
1.15 St Louis - Tye Hill, CB, Clemson
1.16 Miami - Jason Allen, S, Tennessee
1.17 Minnesota - Chad Greenway, OLB, Iowa
1.18 Dallas - Bobby Carpenter, OLB, Ohio State
1.19 San Diego - Antonio Cromartie, CB, Florida State
1.20 Kansas City - Tamba Hali, DE, Penn State
1.21 New England - Lawrence Maroney, RB, Minnesota
1.22 San Francisco - Manny Lawson, OLB, NC State
1.23 Tampa Bay - Davin Joseph, G, Oklahoma
1.24 Cincinnati - Johnathan Joseph, CB, South Carolina
1.25 Pittsburgh (from NY Giants) - Santonio Holmes, WR, Ohio State
1.26 Buffalo (from Chicago) - John McCargo, DT, NC State
1.27 Carolina - DeAngelo Williams, RB, Memphis
1.28 Jacksonville - Marcedes Lewis, TE, UCLA
1.29 NY Jets - Nick Mangold, C, Ohio State
1.30 Indianapolis - Joseph Addai, RB, LSU
1.31 Seattle - Kelly Jennings, CB, Miami
1.32 NY Giants (from Pittsburgh) - Mathias Kiwanuka, DE, Boston College"
set dp to (path to desktop as string)
set file_spec to (dp & "draft.rtf") as file specification
set ref_num to (open for access file_spec with write permission)
set eof ref_num to 0
write theHeader to ref_num
close access ref_num
tell application "TextEdit"
	launch
	activate
	open file_spec
	set text of front document to t
	considering case
		repeat with i from 1 to 5
			set p to item i of pos_list
			set c to item i of color_list
			set (color of every paragraph of front document where it contains p) to c
		end repeat
	end considering
	save front document
	quit
end tell

I didn’t add error checking for various situations.

gl,

hi guys,

thanks for the tips. this board helps me as an AppleScripter more than any other resource.

kai, yours is really small but it doesn’t catch the ‘ROUNDS’ properly for me and it shows the end of the line sequence ‘\cf0’ for each line, which it shouldn’t. do you see the same thing? it may be because i’m running it against an RTF file.

Ah yes, that would make a significant difference, waltr. I was using a plain text file (hence my earlier use of the choose file of type “public.plain-text” statement).

Besides not iterating through empty paragraphs (as before), the following version avoids checking those that contain the initial RTF data, too. (I’ve also suggested a brief, additional test, just to make sure the file’s data seems appropriate for the routine.)

property searchList : {" RB, ", " QB, ", " WR, ", " TE, ", " K, "}
property searchCount : count searchList
property theHeader : "{\\rtf1\\mac\\ansicpg10000\\cocoartf824\\cocoasubrtf110
{\\fonttbl\\f0\\fswiss\\fcharset77 Helvetica;}
{\\colortbl;\\red255\\green255\\blue255;\\red255\\green0\\blue0;\\red0\\green255\\blue0;\\red0\\green0\\blue255;
\\red128\\green0\\blue128;\\red153\\green102\\blue51;}
\\margl1440\\margr1440\\vieww9000\\viewh8400\\viewkind0
\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\ql\\qnatural\\pardirnatura

\\f0\\fs24 \\cf0 1ST ROUND\\
\\
 "
set fileChosen to choose file of type "public.rtf" without invisibles
set theInfo to read fileChosen from 1
set startPos to (offset of " 1ST ROUND\\" in theInfo) + 12
if startPos is 12 then error "The chosen file does not appear to contain the correct data."
set myArray to {}
considering case
	repeat with j in (get paragraphs of (theInfo's text startPos thru -2 & "\\"))
		if (count j) > 1 then
			if j ends with "ROUND\\" then
				set myArray's end to "\\
\\
\\f0\\fs24 \\cf0 " & j & "
\\
"
			else
				repeat with i from 1 to searchCount
					if my searchList's item i is in j then
						set myArray's end to "\\cf" & i + 1 & space & j & "cf0 \\
"
						exit repeat
					end if
				end repeat
			end if
		end if
	end repeat
end considering
set text item delimiters to space
set finalBody to theHeader & myArray & "}"
set text item delimiters to {""}
tell application "Finder" to set outputFile to (fileChosen's folder as Unicode text) & "offense.rtf"
set writeFile to open for access file outputFile with write permission
set eof writeFile to 0
write finalBody to writeFile
close access writeFile