Saving or Downloading a Safari file

Hi, I am just starting with Applescript and I’m in need of some help.
I am trying to open a specific data file in Safari and download it to my hard drive. I can’t seem to get the save command to work. Any suggestions would be appreciated.

I have pasted the script at the stage I am at.

set theDate to current date

set dd to text -2 thru -1 of ("0" & theDate's day)

copy theDate to b
set month of b to January
set mm to text -2 thru -1 of ("0" & (1 + ((theDate - b + 1314864) div 2629728)))

set yyyy to year of theDate

set SD to yyyy & mm & dd

set FN to "pp" & SD & ".txt"

set UR to "http://www.cooltrader.net/730data/purepad/" & FN

tell application "Safari"
	open location UR
	save UR
end tell

Model: ibook G4
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

Downloading the file: (note: It looks like you need a username and password to have access)

property url_start : "http://www.cooltrader.net/730data/purepad/"
property user_name : "username"
property user_password : "password"

tell (current date) to set url_end to "pp" & (10000 * year) + (100 * (its month)) + day & ".txt"
do shell script "curl -u " & user_name  & ":" user_password & space & url_start & url_end
-- [ do shell script "curl http://username:password@www.cooltrader.net/730data/purepad/pp20051010.txt" ]

Query: Do you want to just save the file to disk and leave it or do you want to read the contents for use later on in the script? If you want to download it to disk, use the ‘-o’ parameter in curl.

Query: Do you want to just save the file to disk and leave it or do you want to read the contents for use later on in the script?

Firstly, thanks for your reply. I am new to the Mac switching from XP, so I’m getting used to the differences, but I commend Apple for the robustness and security of OS X.
I want to download these stock quotes found at the site mentioned and save the file. I do use a username and password, however, I have it stored in Safari, so it logs on automatically. (Whether this is wise or not I is subjective, but it is convenient.) Secondly, I would like to import the data into a Technical analysis program. Thanks, Rob

Or perhaps even:

tell (current date) to set url_end to "pp" & 10000 * year + 100 * (its month) + day & ".txt"

I have a feeling that the Save command in Safari is still shot, Robert - so Qwerty’s curl suggestion might be a better option. :slight_smile:

(If you really need to use Safari, it’s possible via UI scripting. Depending on any other requirements you might have, you could even save the source or just the text from Safari to file…)

Thanks kai!

I am having trouble saving this file to disk using curl -o. Help.

property url_start : "http://www.cooltrader.net/730data/purepad/"
property user_name : "username"
property user_password : "password"

tell (current date) to set url_end to "pp" & (10000 * year) + (100 * (its month)) + day & ".txt"
do shell script "curl -o " & user_name & ":" & user_password & space & url_start & url_end

Try this, filling in your username and password:

property url_start : "http://www.cooltrader.net/730data/purepad/"
property user_name : "username"
property user_password : "password"

tell (current date) to set url_end to "pp" & (10000 * year) + (100 * (its month)) + day & ".txt"
do shell script "cd ~; curl -O -u " & user_name & ":" & user_password & space & url_start & url_end

Thanks for your reply. I’m still scratching because it seems the download won’t work because authorisation is required. I assume that is -u parameter in curl. I tried combining both but without success. Am I missing something?

I think I have found the answer. curl -o curl -u

Thanks for all the help given!:smiley:

Could you please elaborate? :slight_smile:

He didn’t supply a name for the output file.

As a side note, you could get the date from inside the shell script:

property baseURL : "http://www.cooltrader.net/730data/purepad/"
property userName : "username"
property userPassword : "password"

do shell script "cd ~; curl -o curl.txt -u " & userName & ":" & userPassword & " " & baseURL & "pp`date +%Y%m%d`.txt"

I’m sorry for the ambiguity. I meant curl -o Name of output File -u
I tried this and it worked well. again, thanks for the input.

Thanks! I just read at http://curl.haxx.se/docs/manual.html that using ‘-O’ (capital o) will save the file using the downloaded file’s name.

Also, if you want to download www.apple.com (using -O), you must do this:

instead of:

because it needs the name of a file for it to work.