Create folder (by date) and download files

Hi,

I’m pretty new to AppleScript and hope you can help me with my problem: I’d like to automatically download my newspaper and place the files in the right folder. It is somewhat similar to this solution (http://macscripter.net/viewtopic.php?id=27166), but as a newbie I wasn’t able to rewrite the script appropriately.

I’d like to
a) check if “todays” folder, e.g. 20090712 already exists on my desktop and if not, create it
b) download all the PDFs from the server (max 52 files, one for each page)
c) if possible, delete any “empty” file that was downloaded with an error, say, if there where only 28 pages or so

The thing is that the download link changes for each day, as well as for each page:
http://domain.com/folder/2009/20090709/01_file_1.pdf
http://domain.com/folder/2009/20090709/02_file_1.pdf
http://domain.com/folder/2009/20090709/03_file_1.pdf

I already violated some of the code in the aforementioned link wich resulted in this monstrosity:
(Did I mention I’m new to this?)

property mytitle : "Newspaper"
property destfolderpath : "Leopard:Users:David:Desktop:"

property starturl : "http://domain.com/folder/"
property endurl : "01_file_1.pdf"

on run
	set todaysdatestring to my gettodaysdatestring()
	set filename to my createfilename()
	set filepath to destfolderpath & filename
	try
		tell application "URL Access Scripting"
			download pageurl to filepath
		end tell
	end try
end run

on gettodaysdatestring()
	set command to "date \"+%Y/%Y%m%d\""
	set todaysdatestring to do shell script command
	return todaysdatestring
end gettodaysdatestring

on createfilename()
	set command to "date \"+%Y%m%d\""
	set datestring to do shell script command
	set filename to datestring & ".pdf"
end createfilename

Help would be REALLY appreciated. Thank you.

P.s.: I own a copy of Speed Download ” If it’s of any use to finding a solution or making it easier…

A few questions:

  1. Does the newspaper require a cookie (e.g., are you a paying customer)?
  2. Speed Download’s dictionary indicates that it will accept a list in its “addurl” (thanks for suggesting it too; I’d never noticed that SD was scriptable, confess I haven’t tried it).

addurl accepts the following arguments:

set theResult to AddURL {list, …} ¬
with form data string ¬
from string ¬
with cookies list ¬
startqueue boolean ¬
to folder file ¬
add to download boolean ¬
Queue UID unsigned integer

That looks like an easy way to go.

  1. Otherwise, you don’t tell us quite enough to suggest an actual script, though you seem to have the construction of the file name set up correctly.

Hi,

I’m sorry I couldn’t reply any sooner, I was without access to web for a few days.

re 1) I’m a paying customer, but no cookie is required as of now. They just have this awfull Flash-thing with single-page-download that I simply cannot stand. I could PM the actual URL and/or access info if needed.

re 2) I’m trying to do it from scratch, but SD still prompts where to download it even though I declared the “to folder” (I hope corectly):

on run
	set todaysurl to my gettodaysurl()
	try
		tell application "Speed Download"
			AddURL "http://epaper.domain.com/PUBS/DOLO/2009/DOLO" & todaysurl to folder "Macintosh HD:Users:david:Desktop:"
		end tell
	end try
end run

on gettodaysurl()
	set command to "date \"+%Y%m%d/DOLO%Y%m%d_opf_files/pdfs/01_DOLO_1.pdf\""
	set todaysurl to do shell script command
	return todaysurl
end gettodaysurl

Also, another thing left is the possibility to add all URLs, i.e.
01_DOLO_1.pdf
02_DOLO_1.pdf
03_DOLO_1.pdf

Unfortunately, I don’t really know how to do that. Any help, suggestion or solution (so I can actually understand it) would be appreciated.

This should work. I couldn’t try it obviously because the URLs aren’t real but it looks good to me. It uses curl to download the files but you can easily substitute speed download if you’d rather use that. In this case curl is in a try block so if some of the urls don’t exist then it should just skip them without an error.

set folderLocation to path to desktop folder as text

-- calculate the folder name
set now to current date
set theYear to (year of now) as text
set theMonth to text -2 thru -1 of ("00" & ((month of now as number) as text))
set theDay to text -2 thru -1 of ("00" & day of now as text)
set folderName to theYear & theMonth & theDay

-- make folder on the desktop
tell application "Finder"
	if not (exists folder (folderLocation & folderName)) then
		make new folder at folder folderLocation with properties {name:folderName}
	end if
end tell

-- download the urls
set url1 to "http://domain.com/folder/" & theYear & "/" & folderName & "/"
set url2 to "_file_1.pdf"
repeat with i from 1 to 52
	set thisURL to url1 & text -2 thru -1 of ("00" & i) & url2
	try
		do shell script "curl " & quoted form of thisURL & " -o " & quoted form of POSIX path of (folderLocation & folderName)
	end try
end repeat

Perfect! I modified it just slightly so I could bring SD into the workflow. Unfortunately, I wasn’t able to get it to download the files into the desired folder. Still, this is a huge timesaver for me. Thanks, Adam and regulus6633. :slight_smile: