Return results of cURL shell script direct to AS?

Is there a way to return the results of a cURL shell command (which I have working fine) directly to the AS script? The result of the cURL command is a small text file.


Or am I stuck with opening the resulting file to read its contents?

iMac, Mac OS X (10.6.7), iMac 24" 2.8 ghz 4 GB RAM

Posted on Jan 1, 2013 4:02 AM

Reply
6 replies

Jan 1, 2013 11:22 AM in response to adayzdone

I'm using POST for this, due to the requirements of the web page, so the successful cURL string, which works in Terminal, is:


curl --data "browserRequest=true&lat1=43%C2%B043%2738.11%22&lat1Hemisphere=N&***1=69%C2%B04 9%2752.85%22&***1Hemisphere=W&startYear=2013&startMonth=1&startDay=1&resultForma t=csv" http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination



that downloads a small text file to Downloads but if I can use the "

set myData to do shell script
" format it would be good, but any suggestions to format that would be appreciated.


Sounds as if I don't need to pipe the results into a shell variable then?

Jan 1, 2013 11:44 AM in response to Dan Hinckley

The resuts of the shell script can be directly put into an AppleScript variable, the only thing you need to watch out for is escaping any double quotes or backslashes - your command doesn't download or put anything into a file, though.

set theAnswer to (do shell script "curl --data 'browserRequest=true&lat1=43%C2%B043%2738.11%22&lat1Hemisphere=N&***1=69%C2%B04 9%2752.85%22&***1Hemisphere=W&startYear=2013&startMonth=1&startDay=1&resultForma t=csv' http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination")


The results are HTML, so I am guessing you have something that is going to parse that.

Jan 1, 2013 11:57 AM in response to Dan Hinckley

The solution is simple - your original shell example uses quoted strings, and you can't just copy/paste that into an AppleScript because AppleScript also uses quotes to delineate strings.


The answer lies in escaping the quotes, so that AppleScript knows to pass the actual quote symbol to the shell (and therefore onto curl) rather than have AppleScript interpret it itself.


do shell script "/usr/bin/curl --data \"browserRequest=true&lat1=43%C2%B043%2738.11%22&lat1Hemisphere=N&***1=69%C2%B0 4 9%2752.85%22&***1Hemisphere=W&startYear=2013&startMonth=1&startDay=1&resultForm at=csv\" http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination"


Note how the quotes surrounding your data are escaped.


(Note also that the above command generates a 500 error off the server because the parameter data is invalid, but I assume you can fix that)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Return results of cURL shell script direct to AS?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.