Need info on how to script fetching data thru web ...

Hello everybody!

Do you know how do I get applescript to read some text from a website and put the text in a variable?

Thanks in advance! All info welcome!

Hi,

easiest version

set theVariable to do shell script "/usr/bin/curl http://www.myWebsite.com"

parsing the text is more difficult and each website needs a different way to do it

If you are using Safari there are two ways to get information out of a web page, AppleScirpt and JavaScript. With AppleScript you can do the following:

tell application "Safari"
	activate
	set WindowURL to URL of document 1
	set TheSource to name of document 1
	set TheCode to source of document 1
end tell

This sets TheCode to the HTML source code of the current web page. From there you can parse the page for what you need using AppleScript text item delimiters.

You can also execute JavaScript from an AppleScript in Safari for a more targeted approach.

set ImageLink to (do JavaScript "document.getElementById ('imgMain').src" in document 1)

Thanks - super thanks - to all the answers. I have to admit that I’m SURPRISED to get this good answers in seconds :smiley:

This Stefan’s thing works great! Thanks. I am doing a system that transfers data from a remote server, thru web pages, to local server which is answering the phone … and fetching order data thru web using applescript.
I really do not know if this is smart but it’s the best I can do :rolleyes:
All ideas welcome!

There is also the dead simple “get the text as it appears”. Usually it is not very useful for parsing, but if labels for the things you want are in the text, right before the data, then it is.

tell application "Safari"
	set web_txt to text of document 1
end tell

Allright, thanks!
Things are going smoother than expeted :slight_smile:

OK, I see what you want to do. Here:

set var to do shell script "curl [url=http://www.mactweaks.info/file.txt]http://www.mactweaks.info/file.txt"[/url]

I own a site, so this was easy to upload. Now, you can use a site like http://localhostr.com/ (a uploading site without the annoying “JOIN THE SITE OR WAIT” junk).

First, upload a PLAIN TEXT FILE (an easy way to do this is to open Terminal and type in “touch ~/file.txt”) and the open it (in your home directory) and then upload it
OR, YOU COULD GET THE http://localhostr.com/ dashboard widget, the widget’s download link is on the bottom of http://localhostr.com/. When it is uploaded, replace “http://www.mactweaks.info/file.txt” with the link provided.

I have to study that, seems like a good system.

What I am doing - or planning to do - is a servise system that answers phone calls (using Phlink or PhoneValet) and the caller can manipulate/check their order status. Unfortunatelly the orders are in a remote server MySQL.

The plan is that the caller software in the local Mac is responsing the caller using text-to-speech and fetching/putting data from the remote MySQL using applescript (and a supporting PHP-page).
All seems to work just fine but are still in tiny pieces. Now I have to put it all together.

Thanks for all the superb - and fast - help! :smiley: