How do I extract the contents of a specified text field within a webpage using applescript? 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('" &amp; theClass &amp; "')[" &amp; num &amp; "].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" &amp; 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, "&lt;pre&gt;", "&lt;/pre&gt;")
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.

tell application "Safari" to set theString to (source of document 1)

(* Strip the text and only return the last line*)
set input to do shell script "echo " &amp; (quoted form of theString) &amp; "|sed -n \"/stringified/,/mInput/p\" | sed '$!N;$!D'"



global answer

set offSet1 to "\"stringified\": \""
set offSet2 to "\",\"mInput\""


my strip(offSet1, offSet2, input)

return answer
on strip(offSet1, offSet2, thedata)
    (* Use the offsets of the pattens to match the text # thru # *)
    set textNumber1 to (offset of offSet1 in thedata)
    set theData1 to text -1 thru (textNumber1 + (count of offSet1)) of thedata
    set textNumber2 to (offset of offSet2 in theData1)
    set textString2 to text from word 1 to (textNumber2 - 1) of theData1
    set thedata to theData1
    set answer to textString2
end strip




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.:

tell application "Safari"
    set input to do JavaScript "theclass = document.getElementsByClassName('popup ui-draggable')[0]; theclass.getElementsByTagName('PRE')[0].innerHTML;" in document 1
end tell