Re: How to get the title as text of a html file
Re: How to get the title as text of a html file
- Subject: Re: How to get the title as text of a html file
- From: has <email@hidden>
- Date: Fri, 1 Mar 2002 14:02:06 +0000
Victor Yee wrote:
>
This handler will return the title after it's found. Otherwise, it'll error
>
after searching the first 20 tags or until eof, whichever comes first.
>
>
The limit of 20 is arbitrary and can be changed in the loop, but I think
>
20 is a
>
reasonable limitation (note that the 20 includes close tags). Of course,
>
you can
>
set your own limit (or no limit, aka eof).
>
>
Note that it isn't entirely thorough in that it doesn't verify the close tag.
Doesn't really need to - the >title< element *must* be properly closed
anyway (otherwise the page is terminally broken).
Below is a slightly modified version of your handler. Strictly speaking,
the >title< element could appear anywhere in the head, so rather than
checking an arbitrary number of tags (20) it's better to keep looking to
the end of the head element. I've also allowed for the possibility of the
title element having attributes by using "begins with" rather than "is" for
the comparison.
======================================================================
on parseHtmlTitle(thisFile)
open for access file thisFile
set fileRef to result
try
ignoring case
repeat
read fileRef until "<"
read fileRef before ">"
if result begins with "title" then
read fileRef before "<"
set thisTitle to result
close access fileRef
return thisTitle
else if result is "/head" then
error "<TITLE> tag not found."
end if
end repeat
end ignoring
on error errMsg number errNum
close access fileRef
if errNum is -39 then
error "EOF. <TITLE> tag not found." number errNum
else
error errMsg number errNum
end if
end try
end parseHtmlTitle
======================================================================
HTH
has
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.