Matching a filename to a string

Hello all Newbie here with what I assume is a simple problem.

I have all the episodes of the simpsons on my computer and I was trying to write a script to select one with a random number generator and open it. The part with the random number generator worked fine but I can’t seem to get the file to open. The problem seems to be that When I generate a random episode, it returns the filename as “Simpsons 0310” meaning Season 3 Episode 10, whereas the actual filename is “Simpsons 0310 - Flaming Moe’s.avi”. I traverse to the correct directory but don’t seem able to figure out how to search in that directory to match the string. How does one do that. What i have so far, that compiles is this. It’s probably not elegant but this is my first ever script.


set eps to {13, 22, 24, 22, 22, 25, 25, 25, 25, 23, 22, 21, 22, 22, 21, 22, 22, 22, 19}

set neps to 0
repeat with j in eps
	set neps to neps + j
end repeat

set rn to (random number (neps))
set i to 1

if rn > 13 then
	repeat while (rn > item i of eps)
		set rn to rn - (item i of eps)
		set i to i + 1
		log i
		log rn
	end repeat
end if

set folname to "Simpsons Season " & (i as integer)
if i < 10 then
	set Seasonno to "0" & (i as integer)
else
	set Seasonno to (i as integer)
end if

if rn < 10 then
	set EPno to "0" & (rn as integer)
else
	set EPno to (rn as integer)
end if

set filname to "Simpsons " & (Seasonno) & (EPno)

tell application "Finder"
	activate
	if not (exists window 1) then
		make new Finder window
	end if
	select Finder window 1
	set target of Finder window 1 to folder folname of folder "The Simpsons" of folder "TV" of disk "My Iron Lung 2"
end tell

return filname


That’s because you’ve set it to that!

set filname to "Simpsons " & (Seasonno) & (EPno)

You could search a folder for files that begin with this name:

set filnamestart to "Simpsons " & (Seasonno) & (EPno)
set searchpath to "My Iron Lung 2:TV:The Simpsons" as alias
set filname to every file in folder searchpath whose name begins with filnamestart

Works like a charm.

Thanks