Re: [Q] Communicating with websites?
Re: [Q] Communicating with websites?
- Subject: Re: [Q] Communicating with websites?
- From: "Daniel A. Shockley" <email@hidden>
- Date: Mon, 24 Feb 2003 10:02:26 -0500
Date: Sun, 16 Feb 2003 14:38:14 -0500
To: email@hidden
From: email@hidden
Subject: [Q] Communicating with websites?
I would like to write an applescript that grabs the contents of a URL
and places those contents into a variable.
It seems as if I should be able to use the URL Access Scripting, but
the only option there (download) requires a file specification for
where the contents are to be placed.
You can use the command-line utility 'curl' if you are running Mac OS
X. That allows you to get the result directly, rather than saving a
file.
Here's a handler I wrote that lets you do that. If it gets mangled by
the list-serve, you can get it from
<
http://www.danshockley.com/codebits.php>
-- SAMPLE USAGE
set actionURL to "
http://www.macslash.org"
set testFormInfo to ""
set saveFilePath to ""
curlSimpleDownload(actionURL, saveFilePath, testFormInfo)
on curlSimpleDownload(downloadURL, destExpected, theFormInfo)
-- version 1.1, Daniel A. Shockley - public domain
-- downloadURL is STRING
-- saves to destExpected (Mac path as STRING, FILE SPEC, or
ALIAS), if given
-- if file path is "", returns source
-- optional form data for POST - use "" for no form data
try
-- basic download to standard output
set curlCode to "curl \"" & downloadURL & "\""
if (length of theFormInfo) > 0 then
set curlCode to curlCode & " -d \"" &
theFormInfo & "\""
end if
-- now, add on the desired file location, if there is one given
if destExpected is not "" then
set unixDestExpected to quoted form of POSIX
path of (destExpected as string)
set curlCode to curlCode & " --output " &
unixDestExpected & " --write-out \"%{http_code}\""
else -- result as string
set curlCode to curlCode & " | vis" -- pipe
into vis to strip nonprintable characters
end if
set curlResponse to do shell script curlCode
return curlResponse
(*
curlResponse will be the http success code ("200"),
or an error code.
If no destination was given, curlResponse will be the source
returned, and no file will be saved
*)
on error errMsg number errNum
error "curlSimpleDownload FAILED: " & errMsg number errNum
end try
end curlSimpleDownload
--
----
Daniel A. Shockley
email@hidden
email@hidden
http://www.danshockley.com
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.