Desktop: how to get title tag contents ?

In a DESKTOP application, I have to get the title tag string contents.

I do not have anything else to do with strings at all.

What is the way to go ?

a. RegEx (how ?)

b. InStr on the html source and use Left(source,start,len)

TIA.

[quote=82574:@Emile Schwarz]In a DESKTOP application, I have to get the title tag string contents.

I do not have anything else to do with strings at all.

What is the way to go ?

a. RegEx (how ?)

b. InStr on the html source and use Left(source,start,len)

TIA.[/quote]

I suppose you are referring to an HTML page. Here with Instr() :

dim f as folderitem = SpecialFolder.Desktop.child("index.html") Dim t As TextInputStream Dim line as String t = TextInputStream.Open(f) dim depart as integer while depart =0 line = t.ReadLine depart = instr(lowercase(line),"<title>") wend t.close depart = depart+7 dim fin as integer = instr(lowercase(line),"</title>") dim titletag as string = mid(line, depart,fin-depart) msgbox titletag

When Kem gets here, he may have one of his stunning RegEx solutions for you :wink:

Thanks for the tip Michel. (1 000 merci)

Similar solution with NthField:

Dim f As FolderItem = SpecialFolder.Desktop.Child("index.html") Dim t As TextInputStream t = TextInputStream.Open(f) MsgBox NthField(NthField(t.ReadAll, "<title>", 2), "</title>", 1) t.Close

Hi Frederick,

nice alternate way to get what I want !!!

Thank you.

[quote=82590:@Emile Schwarz]Hi Frederick,

nice alternate way to get what I want !!!

Thank you.[/quote]
As Michel mentioned, Kem may reply with yet another solution using RegEx. :wink:

Yes, but it will be for tomorrow: I have to go right now.

If the document is loaded in a HTMLViewer, you can always access the title tag contents via the DOM through JavaScript…

shao sean:
I load the document thru HTMLviewer. I do checked the HTMLviewer doc page earlier and found an event with a misnamed parameter:

HTMLViewer.TitleChanged(NewTitle As String)

The misnamed parameter (NewTitle As String) is misnamed only if you load many times the same page who have the same title ! ;-:slight_smile:

Thank you for your answer: I will explorate that later today.

The TitleChanged event is only fired if the title actually changes in the source code (i.e., through JavaScript)…

[code]Sub Open()
me.LoadURL “http://www.apple.ca/
End Sub

Sub DocumentComplete(URL as String)
me.ExecuteJavaScript “window.status = document.getElementsByTagName(‘title’)[0].innerText;”
End Sub

Sub StatusChanged(newStatus as String)
MsgBox newStatus
End Sub[/code]

The JavaScript will get the first tag in the DOM, grab the text between the opening and closing tags and then set the window.status property to it (this is about the only way I know of passing data back from the HTMLViewer to Xojo, so if anyone else knows another way that is better, let us know)…

If there are no tags in the DOM I believe the return data will be the literal string “undefined”…

If you decide to go the regex way, you can obtain the title using the following pattern.

^....*$

You could even use a javascript to get the whole page source (if you happen to need other data from the page) and use regex to parse the title :slight_smile:

[quote=83241:@Matthew Combatti]If you decide to go the regex way, you can obtain the title using the following pattern.

^....*$

You could even use a javascript to get the whole page source (if you happen to need other data from the page) and use regex to parse the title :-)[/quote]

Thank you Matthew.

shao shen:

Thank you for your answer.

I do not know what really do TitleChanged, but it seems to work fine. I will try to check a bit more later today.

Strange. I placed an HTMLViewer on a page, and no way to have Titlechange for HTMLViewer1. Not there in autofill, and generates error when entered manually.

Smells like an insect :s

[quote=83342:@Michel Bujardet]Strange. I placed an HTMLViewer on a page, and no way to have Titlechange for HTMLViewer1. Not there in autofill, and generates error when entered manually.

Smells like an insect :s[/quote]
Hum…

I recall dropping a file on the HTMLViewer (where I put MsgBox TitleChange in the appropriate event) and get the very same string (of course, I drop the same file) each time I drop that file (the same file).

I only had that file handly and do not wanted to waste time to seach another.

Self reminder: time to make a serious debug session here.

it depend for the purpose
It’s not ideal for large and commercial selling apps
But for personnal and internal business use
I pass through applescript + Safari ability to get ALL DOM element
in Applescript

on run {} tell application "Safari" activate set checktitle to do JavaScript "document.title;" in document 1 return checktitle end tell end run
in XOJO you drag the applescript icon
then use the return string

Dim webtitle As String webtitle = (applescript name)

My bad. Sorry. I was trying to get Titlechanged as a property. I had read the documentation too fast : HTMLViewer.TitleChanged ( NewTitle as String )

It works perfectly. Much simpler than anything discussed so far :slight_smile:

Usually, I start doing “anything discussed” so far then read the docs.

This time I do it on the other way, but it was too simple for me thus my question ;-:slight_smile:

Thank you all.

This was not a debug session, but I really feel that

HTMLViewer.TitleChanged ( NewTitle as String )

change reliably. I checked my Bookmark list (I save URL and ) and I had the correct tag contents.

If there is something strane here, it will be my faul; no bug involved.

Many thanks to all.