Wolfram Alpha

Any ideas on how to integrate Wolfram Alpha with AppleScript eg. Get text returned of dialog box, then search it up on wolfram alpha and display the first result in a dialog box.

Example Queries… “Population of UK” >>Show dialog saying “The population of the UK is 61.2 million people (world rank: 22nd) (2008 estimate)”

or

“Number of cats in US” >>Show dialog saying “There are 81.72 million cats in the US”

Any help would be appreciated!

Thanks,
Magikseb.

Anything at all?? :frowning:

Hello.

Take it easy, I’ll have a look at it during the weekend, maybe we can get something similar to work here as with WikiPedia, maybe. :slight_smile:

You may look at this thread :
http://discussions.apple.com/thread.jspa?threadID=2444872&start=30&tstart=0
Question upon Wolfram Alpha was asked on page #2.
As I did’t knew the tool, it started with difficulty but the final result was interesting.

Yvan KOENIG (VALLAURIS, France) vendredi 6 août 2010 23:41:33

Hello and thanks Yvan.

I’ll have a very close look at your solution. :slight_smile:

@OP. I think you really should specify what you are going to use Wolfram Alpha for; with some example queries you are going to make. as this tool can manage a lot.

I responded to a question which is available in the thread.

I apologizes but I have too must duty to jump one more time in this thread.

I sent you the related files which I own.

Maybe I will post your mail address so interested guys would be able to ask you :stuck_out_tongue: :):rolleyes:

Yvan KOENIG (VALLAURIS, France) samedi 7 août 2010 23:33:39

Hello.

Please don’t :slight_smile: I am just a script kiddie compared to you Yvan.

Or, I can leave all the gui and java scripting of Safari to you :smiley: -I just tell you what results I expect! :smiley:

Hello.

I have had a look at it, and it may be possible, but it just isn’t worth it, You can download a widget from here Wolram Mac Os X Widget.

In order to get the data out, you have to effectively query the Wolfram Alpha site, with Java Script, not a single result are rendered in the source of the document. :slight_smile:

What I can provide for you, is a dialog to enter the question in for Wolfram Alpha, and leave you at Wolfram Alpha’s page.

Just tell me if you are interested. That one, is tried, thanks to Yvan Koenig.

Edit: Here you are, for your own convenience, you should try to copy and paste a result from their webpage.
:slight_smile:


on run
	
	set dialogResponse to display dialog "Your for:)mula please ?" default answer "Number of AppleScript Users"
	
	set theFormula to text returned of dialogResponse
	if theFormula is "" then error number -128
	
	set windowTitle to theFormula & " - Wolfram|Alpha"
	
	set theFormula to my hideQuotes(theFormula)
	set theFormula to do shell script "echo " & quote & "<?PHP echo urlencode(" & quoted form of theFormula & ") ; ?>" & quote & " | php"
	set theFormula to my unhideQuotes(theFormula)
	
	tell application "Safari"
		activate
		if exists window windowTitle then close window windowTitle
		open location "http://www.wolframalpha.com/input/?i=" & theFormula
		
		repeat until name of window 1 is windowTitle
			delay 1
		end repeat
	end tell -- Safari
end run

on hideQuotes(f)
	local exchanges, i, j
	set exchanges to {{"'", "¹¹"}, {quote, "««"}}
	repeat with i from 1 to count of exchanges
		set j to item 1 of item i of exchanges
		if f contains j then set f to my remplace(f, j, item 2 of item i of exchanges)
	end repeat
	return f
end hideQuotes

--=====

on unhideQuotes(f)
	local exchanges, i, j
	set exchanges to {{"%E2%80%B9%E2%80%B9", "%27"}, {"%C2%AB%C2%AB", "%22"}}
	repeat with i from 1 to count of exchanges
		set j to item 1 of item i of exchanges
		if f contains j then set f to my remplace(f, j, item 2 of item i of exchanges)
	end repeat
	return f
end unhideQuotes

--=====

(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to d1
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

--=====

Ok, thanks for all the help guys :slight_smile:

Sorry to bump an old thread but it appears the Wolfram Alpha API is now public… http://blog.programmableweb.com/2011/01/21/wolfram-alpha-api-now-free-and-open-to-all/

Expanding on what McUsr wrote, which is simply brilliant, I made an improvement. GetDefaultWebBrowser is a Perl function written by MacScripter user “crgand” and appears on “http://macscripter.net/viewtopic.php?id=27553”. The only thing I have done is integrating one function into another.

--Lookup Wolfram|Alpha in Default Web Browser


--Get Default Web Browser
--Written by crgand
--http://macscripter.net/viewtopic.php?id=27553
on GetDefaultWebBrowser()
	set _scpt to "perl -MMac::InternetConfig -le " & ¬
		"'print +(GetICHelper \"http\")[1]'"
	do shell script "defaults write com.apple.versioner.perl Prefer-32-Bit -bool yes"
	return do shell script _scpt
	do shell script "defaults write com.apple.versioner.perl Prefer-32-Bit -bool no"
end GetDefaultWebBrowser

--Wolfram|Alpha Lookup
--Copyright © McUsr, 2008.
--http://macscripter.net/viewtopic.php?id=33563
on remplace(t, d1, d2)
	local oTIDs, l
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to d1
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

on hideQuotes(f)
	local exchanges, i, j
	set exchanges to {{"'", "¹¹"}, {quote, "««"}}
	repeat with i from 1 to count of exchanges
		set j to item 1 of item i of exchanges
		if f contains j then set f to my remplace(f, j, item 2 of item i of exchanges)
	end repeat
	return f
end hideQuotes

on unhideQuotes(f)
	local exchanges, i, j
	set exchanges to {{"%E2%80%B9%E2%80%B9", "%27"}, {"%C2%AB%C2%AB", "%22"}}
	repeat with i from 1 to count of exchanges
		set j to item 1 of item i of exchanges
		if f contains j then set f to my remplace(f, j, item 2 of item i of exchanges)
	end repeat
	return f
end unhideQuotes

on run
	
	set a to GetDefaultWebBrowser()
	set dialogResponse to display dialog "Formula/Question: " default answer "When will the world end?"
	
	set theFormula to text returned of dialogResponse
	if theFormula is "" then error number -128
	
	set windowTitle to theFormula & " - Wolfram|Alpha"
	
	set theFormula to my hideQuotes(theFormula)
	set theFormula to do shell script "echo " & quote & "<?PHP echo urlencode(" & quoted form of theFormula & ") ; ?>" & quote & " | php"
	set theFormula to my unhideQuotes(theFormula)
	
	tell application a
		activate
		if exists window windowTitle then close window windowTitle
		open location "http://www.wolframalpha.com/input/?i=" & theFormula
		
		repeat until name of window 1 is windowTitle
			delay 1
		end repeat
	end tell
end run