Cron & Safari's Cache!

Hi all,

I want to be able to write a script that downloads images from a webserver overnight and sticks them in Safari’s cache so when I come in in the morning I don’t have to wait for the images to load!

Is this possible, and if so how would I go about doing it?

Thanks!

dai.hop

Model: Aluminium iMac, 2.4Ghz Core 2 Duo, 4GB RAM
Browser: Safari 3.1.2
Operating System: Mac OS X (10.5)

Hi dai.hop,

I don’t know about a method to manipulate Safari’s cache directly. But maybe loading the images from the webserver into new Safari documents and closing them right afterwards could already be of some help:


set weburls to {"http://images.apple.com/home/images/macbook20081014.jpg", "http://images.apple.com/home/images/macbookpro20081014.jpg"}

tell application "Safari"
	repeat with weburl in weburls
		make new document with properties {URL:weburl}
		-- wait 10 seconds
		delay 5
		close document 1
	end repeat
end tell

Great stuff, that may well do the job!

The next thing I need to be able to do is ftp into my webserver and put some filenames into the array. Heres some code I’ve found on the net that I’ve been struggling with…

property download_path : "ftp://username:¢¢¢¢¢¢¢@ftp.outlookimages.co.uk/htdocs/uploads/"
property server_name : "uploads"
property download_folder : "Macintosh HD:Users:davidhopkins:Desktop"

on run
-- need some code to read the filenames in the directory here
set the_file_str to {"TEST.jpg"} as string
tell application "Finder"
open location download_path
delay 10
set download_URL to (server_name & the_file_str) as alias
set file_downloaded to duplicate download_URL to folder download_folder
delay 10
delete download_URL
delay 5
eject (server_name as alias)
end tell
end run

The problem with this code is that it doesn’t connect to my server properly (needs a password somewhere), and it also lacks the part that would read the filenames into an array.

Could I either get some corrections to this code or a link to good tutorial on AppleScript and FTP (as I’m not having alot of joy Googling around)?

Thanks!

dai.hop

If you need to get a directory listing, you can use code as follows which utilizes the «curl» command:


try
	set uname to "anonymous"
	set pword to "anonymous"
	set ftpserver to "[url=ftp://ftp.apple.com]ftp.apple.com[/url]"
	set ftpdir to "developer/"
	set command to "curl -l ftp://" & uname & ":" & pword & "@" & ftpserver & "/" & ftpdir
	set output to paragraphs of (do shell script command)
on error errmsg number errnum
	tell me
		activate
		display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 30
	end tell
end try

Martin thats awesome!

I now have the following script…

try
	set uname to "outlookimages.co.uk"
	set pword to "****"
	set ftpserver to "[url=ftp://ftp.outlookimages.co.uk]ftp.outlookimages.co.uk[/url]"
	set ftpdir to "htdocs/uploads/"
	set command to "curl -l ftp://" & uname & ":" & pword & "@" & ftpserver & "/" & ftpdir
	set output to paragraphs of (do shell script command)
on error errmsg number errnum
	tell me
		activate
		display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 30
	end tell
end try

tell application "Safari"
	repeat with filename in output
		make new document with properties {URL:"http://www.outlookimages.co.uk/uploads/" & filename}
		-- wait 10 seconds
		delay 5
		close document 1
	end repeat
end tell

Theres about 1000 files in this directory, varying sizes. Files are formatted like this…

[date file was uploaded]-[time file was uploaded]-[random 5 digit hex number]-[8 characters of original filename]

So, my final question is - how would I go about only running the script above on only the files that have been uploaded today?

Thanks!

dai.hop

It’s just Mac automation :smiley:

Try code like follows:


set command to "date \"+%y%m%d\""
set datestring to do shell script command

tell application "Safari"
	repeat with filename in output
		if filename begins with datestring then
			make new document with properties {URL:"http://www.outlookimages.co.uk/uploads/" & filename}
			-- wait 10 seconds
			delay 5
			close document 1
		end if
	end repeat
end tell

Martin, you’re a star!

Here’s my final script if your interested…

beep
try
	set uname to "outlookimages.co.uk"
	set pword to "darkskies"
	set ftpserver to "[url=ftp://ftp.outlookimages.co.uk]ftp.outlookimages.co.uk[/url]"
	set ftpdir to "htdocs/uploads/"
	set command to "curl -l ftp://" & uname & ":" & pword & "@" & ftpserver & "/" & ftpdir
	set output to paragraphs of (do shell script command)
on error errmsg number errnum
	tell me
		activate
		display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 30
	end tell
end try

set command to "date \"+%y%m%d\""
set datestring to do shell script command

tell application "Safari"
	repeat with filename in output
		if filename begins with datestring then
			make new document with properties {URL:"http://www.outlookimages.co.uk/uploads/" & filename}
			delay 60
			close document 1
		end if
	end repeat
end tell

I’ve scheduled it to run via cron at 02:00 every day. Hopefully I can finally start enjoying fast page loads for online order system!

Thanks a bunch!

Be careful: If you mean 02:00 AM, then of course the script will generate a date string for the current day, not for the day before. Therefor you will only process images uploaded between midnight and 02:00 AM in the morning (2 hours). If you want to get the date string of yesterday instead, then you need to use code as follows:


set command to "date -v -1d \"+%y%m%d\""
set datestring to do shell script command

Your a good man to have around Martin, thanks for that!