My previous post described an AppleScript Folder Action I created to automatically rename a certain file on my Desktop. That script used a utility “library” (which is just another script with functions we can call). In this post I’ll elaborate on the utility library.
AppleScript provides the date and time via “current date”. set datetime to current date. That produces something like this: “Saturday, June 26, 2010 4:19:43 PM”. I wanted that formatted like this instead: “20100626_161943″ so I could use it as part of a filename.
So I created a function called datestamp(). datestamp() gets the current date and time and returns a string of the format YYYYMMDD_HHMMSS (where HH is 24 hour based). Within that datestamp() function I found myself needed to pad strings, such that month 4 becomes “04″. Thus, I created three utility functions to meet my needs.
Please note that I’m brand new to AppleScript. Surprisingly, I find the language very complicated and convoluted (probably because I’m new to it), despite the fact that I am comfortable doing real work in many other languages (Ruby, Java, Python, C/C++, and more). It’s quite possible that I have reinvented the wheel or otherwise done things the hard way because of my ignorance
.
Here’s the full text of my utility library, named “mt_util_library.scpt”.
----------------------------------------------------------------
on repeatStr(s, cnt)
-- Repeat a string s cnt times.
-- repeatStr("*",4) -> "****"
-- repatStr("12",3) -> "121212"
set outStr to ""
repeat cnt times
set outStr to s & outStr
end repeat
return outStr
end repeatStr
----------------------------------------------------------------
on padIntStr(n, c, totalLen)
-- Return an integer represented as a string, with leading spaces padded
-- as necessary to make the string at least totalLen in length.
-- n = the number to pad
-- c = the character to pad with (usually "0")
-- totalLen = the total length of the string when finished
-- padIntStr(34,"0",4) -> "0034"
set s to (n as string)
if length of s < totalLen then
-- FIXME: We probably should ensure that c is just one char...
set padLen to totalLen - (length of s)
set pad to repeatStr(c, padLen)
set s to pad & s
end if
return s
end padIntStr
----------------------------------------------------------------
on datestamp()
-- Return the current date and time as a string formatted like this:
-- "YYYYMMDD_HHMMSS" -> "20100626_150537"
-- Get the current datetime.
set theDate to current date
-- Get the year.
set y to year of theDate
-- Get the month.
-- AppleScript only gives the month name, but it
-- converts to a month number when cast as integer.
set m to month of theDate
set intM to (m as integer) -- Convert from month name to number.
set strM to padIntStr(intM, "0", 2) -- zero-pad month number (04) = April
-- Get the day.
set d to day of theDate
set strD to padIntStr(d, "0", 2) -- zero-pad it
-- Get the time (in seconds since midnight... the only way it's offered
).
set t to time of theDate
-- Calculate the hour.
set h to round (t / 60 / 60 as real) rounding toward zero
set strH to padIntStr(h, "0", 2) -- zero-pad it
-- Calculate the minutes.
set mins to round (((t / 60 / 60 as real) - h) * 60) rounding toward zero
set strMins to padIntStr(mins, "0", 2) -- zero-pad it
-- Calculate the seconds.
set s to round (((((t / 60 / 60 as real) - h) * 60) - mins) * 60) rounding toward zero
set strS to padIntStr(s, "0", 2) -- zero-pad it
set datestamp to (y & strM & d & "_" & strH & strMins & strS as text)
return datestamp
end datestamp
They are pretty much self-documented, so I’ll just note the purpose of each.
datestamp() creates a string with the date/time formatted as mentioned above.
padIntStr() takes an integer, pads it according to the length and using the character specified, then outputs a string.
repeatStr() takes a string (or character) and outputs a string consisting of the input repeated some number of times.
I’m sure this utility library will grow in time, assuming I do more AppleScript work. If so, future posts will show what’s new.