Another script of mine I’d like to know can be done more efficiently.
I find it handy to write daily files named in series by date, but have them sort in the Finder in a useful fashion. Long ago I got into the habit of using “Filename_YYMMDD.txt” for example. So I have this handler that generates the proper datestamp.
Any suggestions to improve?
global gDateStamp
on dateStamp()
-- Load date components from system
set dayStamp to day of (current date) as integer
set monthStamp to month of (current date) as text
set yearStamp to year of (current date) as text
--Coerce day component to two-digit form
if dayStamp < 10 then set dayStamp to "0" & dayStamp
--Coerce month component to two-digit form
if monthStamp is "January" then set monthStamp to "01"
if monthStamp is "February" then set monthStamp to "02"
if monthStamp is "March" then set monthStamp to "03"
if monthStamp is "April" then set monthStamp to "04"
if monthStamp is "May" then set monthStamp to "05"
if monthStamp is "June" then set monthStamp to "06"
if monthStamp is "July" then set monthStamp to "07"
if monthStamp is "August" then set monthStamp to "08"
if monthStamp is "September" then set monthStamp to "09"
if monthStamp is "October" then set monthStamp to "10"
if monthStamp is "November" then set monthStamp to "11"
if monthStamp is "December" then set monthStamp to "12"
-- Coerce year into two-digit form
set yearStamp to characters 3 thru 4 of yearStamp as text
--Assemble datestamp
set gDateStamp to yearStamp & monthStamp & dayStamp as text
end dateStamp
Hi Kevin,
what’s about this:
on dateStamp()
tell (current date) to set {dayStamp, monthStamp, yearStamp} to {day, its month as integer, year}
return (text 3 thru 4 of (yearStamp as string) & (text -2 thru -1 of ("0" & monthStamp as string)) & (text -2 thru -1 of ("0" & dayStamp as string)))
end dateStamp
set gDateStamp to dateStamp()
Had no idea I could get the month as an integer…cool.
Also love the trick of just adding the filler “0” to everything and just backtracking through the string to get the right characters.
Very nice.
I’ll post an updated copy shortly, since I also changed it from a global variable loader to a simple “return” type handler.
Works like a charm.
Global variable load, “Kevin Style”:
global gDateStamp
on dateStamp()
-- Load date components from system
tell (current date) to set dayStamp to day
tell (current date) to set monthStamp to (its month as integer)
tell (current date) to set yearStamp to year
--Coerce components to two-digit form
set dayStamp to (text -2 thru -1 of ("0" & dayStamp as string))
set monthStamp to (text -2 thru -1 of ("0" & monthStamp as string))
set yearStamp to (text 3 thru 4 of (yearStamp as string))
--Assemble datestamp
set gDateStamp to yearStamp & monthStamp & dayStamp as text
end dateStamp
“return” style handler version:
on dateStamp()
-- Load date components from system
tell (current date) to set dayStamp to day
tell (current date) to set monthStamp to (its month as integer)
tell (current date) to set yearStamp to year
--Coerce components to two-digit form
set dayStamp to (text -2 thru -1 of ("0" & dayStamp as string))
set monthStamp to (text -2 thru -1 of ("0" & monthStamp as string))
set yearStamp to (text 3 thru 4 of (yearStamp as string))
--Assemble datestamp
set finishedDateStamp to yearStamp & monthStamp & dayStamp as text
return finishedDateStamp
end dateStamp
Much, much tidier, even in my preferred “long form.” THANKS!
I’d like to propose a tell block for current date
tell (current date)
set dayStamp to day
set monthStamp to (its month as integer)
set yearStamp to year
end tell
Good point. Was wondering if there was a way to tidy that up a bit. I’ve posted a separate query in the OS X forum about using Tell in this manner. I had no idea…
http://bbs.applescript.net/viewtopic.php?id=20425
You’ll have to excuse my “long form.” I get spread kinda thin then have to remember how the heck I did something. I’ve learned to comment to death and spell things out the long way to keep it straight. So I’ll often over-segment a bit rather than nest and sub-nest.
I even started commenting-in whom I got certain tips from and where in case I have to look them up again.
If I did this as a larger part of my job this would be alot easier.
A “shorter” form:
on dateStamp()
tell (current date) to return text 3 thru 8 of (year * 10000 + (its month as integer) * 100 + day as string)
end dateStamp
set gDateStamp to dateStamp()
Qwerty beat me to it. But I was going to add that it’s actually the most efficient way I know of performing this particular exercise. I was also going to post it in this form:
on dateStamp()
set {year:y, month:m, day:d} to (current date)
return text 3 thru 8 of ((y * 10000 + m * 100 + d) as unicode text)
end dateStamp
dateStamp()
. but I needn’t bother now.
Month-to-integer coercions were introduced with Panther. Before that, a little bit of date mathematics was needed to get the month number:
on dateStamp()
set today to (current date)
set {year:y, day:d} to today
copy today to b
set b's month to January
set m to (b - 2500000 - today) div -2500000
return text 3 thru 8 of ((y * 10000 + m * 100 + d) as Unicode text)
end dateStamp
dateStamp()
Yeah,
I knew, there was an one-liner, but I couldn’t remember it :lol: