Hi All,
I am still new to the world of Applescript, so please, be gentle.
I am trying to design an Applescript which will access a specific URL, and download the webpage as an HTML File to my MacIntosh HD.
This works fine, when I specify the name that I want to save it as, however, as I want this to be done daily, and I want to archive each day’s result, I want to have the Script calculate the name to save as using the following format “YYMMDD.htm” (YY=Last 2 Digits of Year, MM = Month - with leading zero, DD = Day - with leading zero)
My script, as it stands is:
tell application "Finder"
activate
select file " 6. Netscape" of folder "Applications" of folder "Apple Menu Items" of folder "System Folder" of startup disk
open selection
end tell
tell application "Netscape Communicator™"
activate
GetURL "http://www.bored.com" to file "020326.htm"
quit
end tell
It is the ’ to file “020326.htm” ’ section which I need to change.
If anyone could assist me with this, I would be truly appreciative.
(Please email me at lukes555@hotmail.com)
Thank You
Well, the answer provided has sorted out a second question, but what I need to know is how to make the script calculate the name for the file.
E.g. on 26/03/02, I want it to save to 020326.htm,
on 27/03/02, I want it to dave to 020327.htm, etc…
I will incorporate your suggestion regarding the full path into the script. If you have any suggestions for calculations to perform the above, it would be appreciated.
Thanks Again.
This will create the file name. If you need help incorporating it into your script, let us know.
set monthNums to {{January, "01"}, {February, "02"}, {March, "03"}, {April, "04"}, {May, "05"}, {June, "06"}, ¬
{July, "07"}, {August, "08"}, {September, "09"}, {October, "10"}, {November, "11"}, {December, "12"}}
set currDate to (current date)
set currDay to day of currDate
if (count of (currDay as text)) is 1 then set currDay to "0" & currDay as text
set currYear to text -2 thru end of (year of currDate as text)
repeat with i in monthNums
if month of currDate is equal to item 1 of i then
set currMonth to item 2 of i
exit repeat
end if
end repeat
set fileName to currYear & currMonth & currDay & ".htm" as text
-- Result --> "020326.htm"
set fileName to dateAsYYMMDD(current date) & ".htm"
set filePath to (path to desktop as string) & fileName -- if you want to save on desktop
tell app "URL Access Scripting" -- if you have Mac OS 8.6 or later
download "http://www.bored.com" to file filePath without progress
end
on dateAsYYMMDD(incomingDate)
-- NEEDS getMonthNum()
-- version 1.0
set textYear to text 1 through 2 of (year of incomingDate as string)
set textMonth to month of incomingDate as string
try
set textMonth to my getMonthNum(incomingDate) as string
on error -- JUST IN CASE HANDLER IS MISSING, use this simple one
-- the plain vanilla get month number function - I believe I found it on macscripter.ent
--set theDate to the current date --or any other date
copy incomingDate to b
set the month of b to January
set monthNum to (1 + (theDate - b + 1314864) div 2629728)
set textMonth to monthNum as string
end try
if length of textMonth is 1 then set textMonth to "0" & textMonth
set textDay to day of incomingDate as string
if length of textDay is 1 then set textDay to "0" & textDay
return textYear & textMonth & textDay
end dateAsYYMMDD
on getMonthNum(theDate)
--set theDate to the current date --or any other date
-- the plain vanilla get month number function - I believe I found it on macscripter.ent
copy theDate to b
set the month of b to January
set monthNum to (1 + (theDate - b + 1314864) div 2629728)
return monthNum
end getMonthNum
This means that the file will download without launching Netscape or any web browser. You could also change the without progress to ‘with progress’ to see when it happens. You can read more about URL Access Scripting in this BBS archive
The post dates on this are pretty old but just in case someone comes back to this thread here’s a way to do it in 1 line of code. You do need the DateTime addition which is free.
You can find it here- http://osaxen.com/datetime.html
It comes with a legend for formating the date and time.
I think what you are looking for could be achieved with: