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>")
  • 1
    From the looks of the website. The answers are all actually images. So you cannot extract text!!
    – markhunte
    Jun 5, 2015 at 22:33
  • Yes, but there is an option to get the answer in copyable plaintext form... and the text is located in the html code where it was specified in the script above Jun 6, 2015 at 3:19
  • It would have been good if you had explained that nugget clearly in your answer. We all do not use the site. But anyway luckily my main answer works better as it does not require you to go clicking around.. I have updated my answer though just in case you do.
    – markhunte
    Jun 6, 2015 at 8:09
  • 2
    @rob This post is asking about Applescript. Although it would be better on stackexchange. Are you sure it is off topic: Here is a quote from: apple.stackexchange.com/help/on-topicCode-level programming questions (cocoa, LLVM, etc…) **are off-topic here**. We do encourage AppleScript, Automator, and UNIX shell scripting questions as well as how to use tools like Xcode for non-language specific tasks."
    – markhunte
    Jun 6, 2015 at 9:45

1 Answer 1


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 " & (quoted form of theString) & "|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


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.

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .