Make Safari download something

Hello,

Can I make Safari download anything by passing a URL?
Let´s say we have a picture on the web and we have the path (like this: “http://www.some-random-domain.com/picture.jpg”), now how can I make Safari automatically download it and save it to the desktop?

Help would be appreciated.
Thanks in advance.

Greetings
-Milan

You don’t need to involve Safari at all; in fact, it’s easier if you don’t, because then you won’t have to deal with the unpredictability of page loading times. You can download a file directly by using URL Access Scripting:

set theLocation to (path to desktop) as text
set theDownload to theLocation & "picture.jpg"
set theLink to "http://www.some-random-domain.com/picture.jpg"
tell application "URL Access Scripting"
	download theLink to file theDownload
end tell

Thank you very much!

Supposing I had to use Safari, and not URL Access Scripting, or cURL. Safari has several cookies that give me access to the site. w/o them URL Access Scripting and cURL come up with the login page :frowning:

Any ideas?

Hi nitefall,

cURL does do cookies. See the cookies section of this page curl - Tutorial.

Best wishes

John M

yes… but how does one get a cookie form Safari into cURL?

I think Safari’s scriptability sucks! Its more accurate to say its not as straightforward as Explorer, but whatever…


set TheURL to text returned of (display dialog "Enter a file address" default answer "")
set AppleScript's text item delimiters to "/"
set Filename to last text item of TheURL
set AppleScript's text item delimiters to ""

tell application "Safari"
	activate
	make new document at the beginning of documents
	set the URL of the front document to TheURL
	
	set the target_file to (POSIX path of ((path to desktop folder))) & Filename
	save document 1 in target_file
	close window 1
end tell

SC

dkmarsh,

that bit of code that you put together rocks, and I was hoping you could give me some help adapting it a bit.

I’m trying to use that script to download a series of files. Since the series runs into the hundreds, is there a way to direct the script to another download location as opposed to the desktop? I’ve tried the few options that I can come up with, but can’t get anything to work. for the sake of argument lets say the new folder is “~/desktop/test”

Sorry about the newb question, but I’m coming into this with almost no experience.

Diq

edit: after some more tinkering I came up with this solution


set theLocation to (path to desktop) as text
set theDownload to theLocation & ":test:" & "picture.jpg"