Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

APPLESCRIPT AND HTML PARSING.

hi,

im new to applescript so im not quite sure if what i want to do is actually called html parsing.. but basically i want to put a variable in applescript that is linked to the actual html but i dont know how to make applescript access data inside a html code... to give u a better idea, inside the html is something like this:

100

now that value "100" changes but its maximum amount is 100. i want to create a script which responds to change when that value starts to drop by loading another link.

am i making sense? again the thing id like to achieve is make applescript use that value INSIDE the HTML as its own variable (and perform the right actions as that value changes)

any help would be appreciated.

15" Macbook Pro, Mac OS X (10.5.8)

Posted on Mar 5, 2010 3:02 PM

Reply
17 replies

Mar 5, 2010 4:23 PM in response to intrikate01

In first place you could open the site you talked about in safari and run a little javascript via applescript to get that value.
Javascript is the "best" way to get a special value out of an HTML-Element, but only works in browsers.
e.g.

tell application "Safari"

open location "http://apple.com"

delay 6

set mypromo to do JavaScript "document.getElementById('promos').getElementsByTagName('a')[0].title" in document 1

display dialog "Title of first Promo is:" & return & mypromo

end tell




Or you could just d/l the pure source convert it to text and search for the phrase you are looking for
e.g.

set mysource_html to do shell script "curl http://mysite.org/bla.html"

set mysource_txt to do shell script "curl http://mysite.org/bla.html | textutil -stdin -convert txt -format html -stdout"

if mysource_html contains "<a>100</a>" then

display dialog "Hey, value of 100 is reached"

end if

--or something like

if mysource_txt contains "100" then

display dialog "Hey, value of 100 is reached"

end if

Mar 5, 2010 4:42 PM in response to intrikate01

set theURL to "www.example.com"

tell application "Safari"

--in safari tell block, so it will always be open in safari and no other browser

open location theURL

--since you do not know when the side is loaded....

delay 6

--and if <span id="currentHP" class="statEmphasis">100</span> is still the code

-- and only in document 1 since the site you opened via "open location" will always be document 1, if you won't activate some other side during delay....

set current_health to do JavaScript "document.getElementById('currentHP').innerHTML" in document 1


end tell

Mar 5, 2010 5:03 PM in response to intrikate01

I would say, just keep the page loaded and
try do debug the javascript
ck="selectThis(this);">

tell application "Safari"

do JavaScript "document.getElementById('currentHP').innerHTML" in document 1

end tell



when you run it in AppleScript Editor you will get an output if the code is correct... in Addition you can take a look in Safari's Skript-Debugger, after you ran the script you can check Safari Error Log therefore:

User uploaded file
then you get a Developer Menu. Then choose "Show Error Console" this could look like that....
Errors of your javascript-code will show-up there.

Mar 5, 2010 5:30 PM in response to hubionmac

ok ive figured out what was wrong.. for some reason i cant search for a span id tag since the


document.getElementById()


looks for a div id tag... so i replaced the


document.getElementById(currentHP)
to
document.getElementById(topBar)


and it returned to me everything that is inside that div.

however, there are a whole heap of data under that div that i dont want and i need to look for that specific value inside that span tag but i dont know how to make applescript search for it after

Mar 5, 2010 6:16 PM in response to intrikate01

strange... getElementById works perfectly with div, span, a, input... nearly every tag.

perhaps there more that just one element with that id...

you could try this loop instead:

do JavaScript "var a=''
for (var i=0; i < document.getElementsByTagName('span').length; i++) {
if (document.getElementsByTagName('span') .id=='currentHP')
a=document.getElementsByTagName('span')
.innerHTML
};
a"

Mar 5, 2010 6:25 PM in response to hubionmac

i actually have applescript running in two tabs where in tab 1 it would run something like this:


property theURL:"www.example.com/php?"
tell application "Safari"
set the URL of tab 1 of window 1 to theURL
set myHealth to do JavaScript "var a=''
for (var i=0; i < document.getElementsByTagName('span').length; i++) {
if (document.getElementsByTagName('span').id=='currentHP')
a=document.getElementsByTagName('span').innerHTML
};
a" in tab 1 of window 1

display dialog "My Current Health is:" & return & myHealth


but i still cannot get the dialog to display the number between the appropriate span tag.

Mar 12, 2010 7:14 PM in response to intrikate01

I'm not that familiar with javascript, but I do have a text handler that works for me:

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 340px;
color: #000000;
background-color: #DAFFB6;
overflow: auto;"
title="this text can be pasted into the Script Editor">
on run -- example
set theURLtext to "<body>
<div id=\"topBar\">
<a href=\"hospital.php?\">
<div class=\"healthAreaContainer\">
<table class=\"statTable\">
<tbody>
<tr>
<td class=\"statTableInfo\">
<div class=\"statTopArea\">
<span id=\"currentHP\" class=\"statEmphasis\">100</span>
/
<span id=\"maxHP\">100</span>
</div>"

set currentHPitems to getHTMLElement(theURLtext, "<span id=\"currentHP\"", "</span>", true)
if currentHPitems is not {} then
display dialog first item of currentHPitems
else
display dialog "tag not found"
end if
end run


to getHTMLElement(someText, openTag, closeTag, contentsOnly)
(*
return a list of the specified HTML element in someText
parameters - someText [mixed]: the text to look at
openTag [text]: the opening tag (the ending ">" will be searched for if the tag is incomplete)
closeTag [text]: the closing tag (the tag should be complete when returning the element)
contentsOnly [boolean]: true returns just the contents, false returns the entire element
returns [list]: a list of the HTML elements found - {} if none
*)
set someText to someText as text
set currentOffset to 0 -- the current offset in the text buffer
set elementList to {} -- the list of elements found
try
repeat while currentOffset is less than (count someText)
set currentOffset to currentOffset + 1

set here to offset of openTag in (text currentOffset thru -1 of someText) -- start of opening tag
if here is 0 then exit repeat -- not found
set currentOffset to currentOffset + here
set currentTag to currentOffset - 1 -- mark the start of the element
if openTag does not end with ">" then -- find the close of the tag
set here to offset of ">" in (text (currentOffset - 1) thru -1 of someText) -- end of opening tag
if here is 0 then exit repeat -- not found
set currentOffset to currentOffset + here - 1
else
set currentOffset to currentOffset + (count openTag) - 1
end if
set here to currentOffset

set there to offset of closeTag in (text currentOffset thru -1 of someText) -- end tag
if there is 0 then exit repeat -- not found
set currentOffset to currentOffset + there + (count closeTag) - 2
set there to currentOffset

if contentsOnly then -- add the element contents
set the end of elementList to text here thru (there - (count closeTag)) of someText
else -- add the complete element (tags and contents)
set the end of elementList to text currentTag thru there of someText
end if

end repeat
on error errorMessage number errorNumber
if (errorNumber is -128) or (errorNumber is -1711) then -- nothing (user cancelled)
else
activate me
display alert "Error " & (errorNumber as string) message errorMessage as warning buttons {"OK"} default button "OK"
end if
end try
return elementList
end getHTMLElement
</pre>

Mar 12, 2010 11:15 PM in response to red_menace

thats not the whole url text though. its just a basic path to the main thing im trying to point to. the actual url text has alot of things inside the <body> tag and there are cookies in there which changes for every session if im making any sense. i dont mind not using javascript as long as i can get script editor to use that value between the

VALUE

as a variable and perform actions as that value continues to change...

Mar 12, 2010 11:33 PM in response to intrikate01

The handler will get the contents of the specified tag from any HTML text - I just used your previous snippet as an example. To get a list of all matching tags from the current page you would use something like:
tell application "Safari" to set theURLtext to source of document 1
set currentHPitems to getHTMLElement(theURLtext, "<span id="currentHP"", "</span>", true) --> list of matching items

Mar 13, 2010 12:08 AM in response to red_menace

i have been able to get the html codes using java script. is there a way i can use what i got from that java script as a url source? and make the applescript just look for the tag from what the javascript found? if im making any sense....

perhaps something along the lines of


set theURL to "www.example.com/home.php?"
tell application "Safari"
set the URL of tab 1 of window 1 to theURL
set theSource to do JavaScript "document.getElementById('topBar').innerHTML" in tab 1 of window 1
set theURLtext source to theSource -- this i know is not right but something along the lines of that maybe? and then maybe if
i can get this line to somehow work i can write my if and else lines. coz basically i want my script to NOT do anything if the "VALUE"
between the VALUE is at 100... then load another url IF the "VALUE" between the
VALUE is less that 100.


Message was edited by: intrikate01

APPLESCRIPT AND HTML PARSING.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.