How can I use Applescript And Safari to extract the answer from this site: http://www.wolframalpha.com/input/?i=whats+the+meaning+to+life%3F
I'm attempting to extract the answer in copyable plaintext. That is, .txt
format.
I get this error:
"Can’t make text items 2 thru -1 of "missing value" into type text.
When implementing the following script:
to getInputByClass(theClass, num)
tell application "Safari"
set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
end tell
return input
end getInputByClass
to extractText(searchText, startText2, endText)
set tid to AppleScript's text item delimiters
set startText1 to "x"
set searchText to ("x" & searchText)
set AppleScript's text item delimiters to startText1
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText2
set finalText to (text items 2 thru -1 of beginningToEnd) as text
set AppleScript's text item delimiters to tid
return finalText
end extractText
getInputByClass("popup ui-draggable", 0)
set theText to getInputByClass("r", 0)
set theResult to extractText(theText, "<pre>", "</pre>")
Best Answer
Although the page you supplied is returning an image of the answers which means you cannot return text from it.
The source of the page holds the answer in text form in a javascript function.
This applescript is using one of my old methods to get the text between pattens.
I would probably now do this using ApplescriptOBJC but thought it better to keep it in Applescript with a dash of shell. As it may be more understandable.
Update.
The OP has pointed out that there is an option to have a popup show the answer in plain text.
This is not obvious to anyone who is not familiar with the site. The popup does not exist in the page source until you click this option, which is the reason I could not find the classes the OP was referring to in the page source.
The first script above does not need you to click any option or get the popup.
But if for some reason you do then you can use this script which will require you to have the popup on display first.: