Re: Simple Date-Time Format Question...
Re: Simple Date-Time Format Question...
- Subject: Re: Simple Date-Time Format Question...
- From: Bill Hernandez <email@hidden>
- Date: Wed, 25 Apr 2007 16:58:06 -0500
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_title()
(*
Author : Bill Hernandez
ProjectNo : 4305
ProjectName : bb_date_format.scpt
FileName : bb_date_format.scpt
FilePath :
Version : 1.0.0
VersionDate : [ 2007_04_25 ]
Created : 4/25/07 ( 4:43 PM )
Updated : Wednesday, April 25, 2007 ( 4:43 PM )
*)
end script_title
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_notes()
(*
Novice question #2
I would like to set a date-time like:
"4-24-2007 6-41-07 PM"
I don't know how to say it in 'applescript' - something like...?
set date_today to ((the current date) as string)
set theFormat to "mm-dd-yy hh-MM-ss ?m"
Now what?
*)
end script_notes
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
property bh_aFormatSearch : {}
property bh_aFormatReplace : {}
property bh_pad_long_format : false -- dont pad with a "0" anything
formatted in the long mode "weekday monthname daynumber, year
hours:minutes:seconds ampm"
property bh_pad_short_format : true -- pad anyting formatted in the
short mode "DD yyyy_mm_dd (h.m.s ?m)"
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on run
set AppleScript's text item delimiters to ""
set theDate to ((current date) as string)
set aSearch to {", ", " ", ":"}
set aReplace to {"*", "*", "*"}
set myResultStr to my bh_string_replace(theDate, aSearch, aReplace)
set myStr to ""
set myPrompt to "Since AppleScript's text item delimiters do not
handle multiple delimiters you can easily split a date, which
contains several delimiters using : " & return
set myStr to (myStr & "set theDate to ((current date) as string)
--> " & (current date) as string) & return
set myStr to myStr & "set aSearch to {\", \", \" \", \":\"}" &
return
set myStr to myStr & "set aReplace to {\"*\", \"*\", \"*\"}" &
return
set myStr to myStr & "set myStr to my bh_string_replace(theDate,
aSearch, aReplace) --> " & myResultStr & return
set myStr to myStr & "display dialog myStr" & return & return
set myStr to myStr & "set myPrompt to \"Splitting is much easier
then : \" & return & myStr & return" & return
set myStr to myStr & "set delim to \"*\"" & return
set myStr to myStr & "set aStr to my bh_string_split(myStr,
delim)" & return
set myStr to myStr & "choose from list aStr with prompt myPrompt
with empty selection allowed" & return
set delim to return
set aStr to my bh_string_split(myStr, delim)
choose from list aStr with prompt myPrompt with empty selection
allowed
set theDate to ((current date) as string)
set aSearch to {", ", " ", ":"}
set aReplace to {"*", "*", "*"}
set myStr to my bh_string_replace(theDate, aSearch, aReplace)
display dialog myStr
set myPrompt to "Splitting is much easier then : " & return &
myStr & return
set delim to "*"
set aStr to my bh_string_split(myStr, delim)
choose from list aStr with prompt myPrompt with empty selection
allowed
set theFormat to "mm-dd-yy hh-MM-ss ?m"
set aSearch to {"-", " "}
set aReplace to {"*", "*"}
set myStr to my bh_string_replace(theFormat, aSearch, aReplace)
display dialog myStr
set theDate to (current date)
set aFormat to {}
copy "mm-dd-yy hh-MM-ss ?m" to the end of aFormat
copy "monthname-daynumber-year hours-minutes-seconds ampm" to the
end of aFormat
copy "monthnumber-daynumber-year hours-minutes-seconds ampm" to
the end of aFormat
copy "(DD) mmm-dd-yyyy (hh:MM ?m)" to the end of aFormat
copy "<[weekday]> monthname-daynumber-year (hours.minutes ampm)"
to the end of aFormat
copy "[weekday] monthname daynumber, year (hours:minutes ?m)" to
the end of aFormat
copy "weekday.monthname.daynumber.year (hours:minutes ?m)" to the
end of aFormat
copy "yyyy.mm.dd (h.m.s ?m)" to the end of aFormat
copy "yyyy_mm_dd (h.m.s ?m)" to the end of aFormat
copy "m/d/y (h.m ?m)" to the end of aFormat
copy "mm/dd/yyyy (h.m ?m)" to the end of aFormat
copy "(h.m ?m) y.m.d" to the end of aFormat
copy "(h.m ?m) yyyy.mm.dd" to the end of aFormat
copy "(hh#mm ?m) yyyy<*>mm<*>dd" to the end of aFormat
my bh_date_format_GetHelpMessage()
set myStr to ""
set myPrompt to "This shows pretty much any formatting you want.
Some of it is pretty bizare, but it shows what can be done : " &
return & return
repeat with theFormat in aFormat
set myStr to myStr & "set myStr to my bh_date_format(current
date, \"" & theFormat & "\")" & return
set myStr to myStr & my bh_date_format(theDate, theFormat) &
return & return
end repeat
-- display dialog myStr
set aStr to my bh_string_split(myStr, return)
choose from list aStr with prompt myPrompt with empty selection
allowed
end run
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_date_format_Update(theDate)
set bh_aFormatSearch to {}
set bh_aFormatReplace to {}
-- THESE HAVE TO BE REPLACED IN A SPECIFIC ORDER
-- (mmm before mm, and yyyy before yy, dd before d), etc...
copy "weekday" to the end of bh_aFormatSearch
copy "monthname" to the end of bh_aFormatSearch
copy "daynumber" to the end of bh_aFormatSearch
copy "year" to the end of bh_aFormatSearch
copy "hours" to the end of bh_aFormatSearch
copy "minutes" to the end of bh_aFormatSearch
copy "seconds" to the end of bh_aFormatSearch
copy "ampm" to the end of bh_aFormatSearch
copy "DD" to the end of bh_aFormatSearch
copy "mmm" to the end of bh_aFormatSearch
copy "dd" to the end of bh_aFormatSearch
copy "yyyy" to the end of bh_aFormatSearch
copy "hh" to the end of bh_aFormatSearch
copy "MM" to the end of bh_aFormatSearch
copy "ss" to the end of bh_aFormatSearch
copy "?m" to the end of bh_aFormatSearch
copy "monthnumber" to the end of bh_aFormatSearch
copy "mm" to the end of bh_aFormatSearch
copy "yy" to the end of bh_aFormatSearch
copy "m-d-y" to the end of bh_aFormatSearch
copy "d-m-y" to the end of bh_aFormatSearch
copy "y-m-d" to the end of bh_aFormatSearch
copy "m.d.y" to the end of bh_aFormatSearch
copy "d.m.y" to the end of bh_aFormatSearch
copy "y.m.d" to the end of bh_aFormatSearch
copy "m/d/y" to the end of bh_aFormatSearch
copy "d/m/y" to the end of bh_aFormatSearch
copy "y/m/d" to the end of bh_aFormatSearch
copy "h-m-s" to the end of bh_aFormatSearch
copy "h-m" to the end of bh_aFormatSearch
copy "h.m.s" to the end of bh_aFormatSearch
copy "h.m" to the end of bh_aFormatSearch
set theDateStr to theDate as string
set aSearch to {", ", " ", ":"}
set aReplace to {"*", "*", "*"}
set myStr to my bh_string_replace(theDateStr, aSearch, aReplace)
set delim to "*"
set bh_aFormatReplace to my bh_string_split(myStr, delim)
set n to count items of bh_aFormatReplace
repeat with counter from 1 to n
copy item counter of bh_aFormatReplace to the end of
bh_aFormatReplace
end repeat
copy ((theDate)'s month as number) as string to the end of
bh_aFormatReplace
copy characters -2 thru -1 of ("0" & ((theDate)'s month as
number)) as string to the end of bh_aFormatReplace
copy characters -2 thru -1 of (item 4 of bh_aFormatReplace) as
string to the end of bh_aFormatReplace
-- copy (item 3 of bh_aFormatReplace) as string to the end of
bh_aFormatReplace
set m to ((theDate)'s month as number) as string
set d to ((theDate)'s day as number) as string
set y to ((theDate)'s year as number) as string
set delim to "-"
copy (m & delim & d & delim & y) as string to the end of
bh_aFormatReplace
copy (d & delim & m & delim & y) as string to the end of
bh_aFormatReplace
copy (y & delim & m & delim & d) as string to the end of
bh_aFormatReplace
set delim to "."
copy (m & delim & d & delim & y) as string to the end of
bh_aFormatReplace
copy (d & delim & m & delim & y) as string to the end of
bh_aFormatReplace
copy (y & delim & m & delim & d) as string to the end of
bh_aFormatReplace
set delim to "/"
copy (m & delim & d & delim & y) as string to the end of
bh_aFormatReplace
copy (d & delim & m & delim & y) as string to the end of
bh_aFormatReplace
copy (y & delim & m & delim & d) as string to the end of
bh_aFormatReplace
set h to (item 5 of bh_aFormatReplace as string)
set m to (item 6 of bh_aFormatReplace as string)
set s to (item 7 of bh_aFormatReplace as string)
set delim to "-"
copy (h & delim & m & delim & s) as string to the end of
bh_aFormatReplace
copy (h & delim & m) as string to the end of bh_aFormatReplace
set delim to "."
copy (h & delim & m & delim & s) as string to the end of
bh_aFormatReplace
copy (h & delim & m) as string to the end of bh_aFormatReplace
if (bh_pad_long_format) then
set (item 3 of bh_aFormatReplace) to characters -2 thru -1 of
("0" & (item 3 of bh_aFormatReplace) as string) as string -- force "4/
xx/xx" to "04/xx/xx"
set (item 5 of bh_aFormatReplace) to characters -2 thru -1 of
("0" & (item 5 of bh_aFormatReplace) as string) as string -- force
"4:xx:xx" to "04:xx:xx"
set (item 6 of bh_aFormatReplace) to characters -2 thru -1 of
("0" & (item 6 of bh_aFormatReplace) as string) as string -- force
"xx:5:xx" to "xx:05:xx"
set (item 7 of bh_aFormatReplace) to characters -2 thru -1 of
("0" & (item 7 of bh_aFormatReplace) as string) as string -- force
"xx:xx:5" to "xx:xx:05"
set (item 17 of bh_aFormatReplace) to characters -2 thru -1 of
("0" & (item 17 of bh_aFormatReplace) as string) as string -- force
"xx:xx:5" to "xx:xx:05"
end if
if (bh_pad_short_format) then
set (item 11 of bh_aFormatReplace) to characters -2 thru -1 of
("0" & (item 11 of bh_aFormatReplace) as string) as string -- force
"4/xx/xx" to "04/xx/xx"
set (item 13 of bh_aFormatReplace) to characters -2 thru -1 of
("0" & (item 13 of bh_aFormatReplace) as string) as string -- force
"4:xx:xx" to "04:xx:xx"
set (item 14 of bh_aFormatReplace) to characters -2 thru -1 of
("0" & (item 14 of bh_aFormatReplace) as string) as string -- force
"xx:5:xx" to "xx:05:xx"
set (item 15 of bh_aFormatReplace) to characters -2 thru -1 of
("0" & (item 15 of bh_aFormatReplace) as string) as string -- force
"xx:xx:5" to "xx:xx:05"
set (item 18 of bh_aFormatReplace) to characters -2 thru -1 of
("0" & (item 18 of bh_aFormatReplace) as string) as string -- force
"xx:xx:5" to "xx:xx:05"
end if
end bh_date_format_Update
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_date_format(theDate, theFormat)
my bh_date_format_Update(theDate)
set returnStr to my bh_string_replace(theFormat,
bh_aFormatSearch, bh_aFormatReplace)
return returnStr
end bh_date_format
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_string_replace(theText, SearchObject, ReplaceObject)
set old_delim to AppleScript's text item delimiters
if (class of SearchObject is list) then
set newText to theText
set NoOfItems to count items in SearchObject
repeat with counter from 1 to NoOfItems
set AppleScript's text item delimiters to (item counter of
SearchObject as string)
set newText to text items of newText
if (class of ReplaceObject is list) then
set AppleScript's text item delimiters to (item counter
of ReplaceObject as string)
else
set AppleScript's text item delimiters to ReplaceObject
end if
set newText to newText as text
end repeat
else -- IF NOT A LIST IT MUST BE A STRING
set AppleScript's text item delimiters to SearchObject
set newText to text items of theText
set AppleScript's text item delimiters to ReplaceObject
set newText to newText as text
end if
set AppleScript's text item delimiters to old_delim
return newText
end bh_string_replace
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_date_GetFullDate(theDate)
set DS to date string of (theDate)
set TS to time string of (theDate)
tell application "Finder"
set TS to ((get characters 1 thru -7 of TS) & (get characters
-3 thru -1 of TS)) as string
end tell
return (DS & " (" & TS & ")")
end bh_date_GetFullDate
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_string_split(str, delim)
set old_delim to get AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set aList to every text item of str
set AppleScript's text item delimiters to old_delim
return aList
end bh_string_split
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_string_join(aList, delim)
set str to ""
repeat with theItem in aList
set str to str & theItem & delim as string
end repeat
return str
end bh_string_join
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_date_format_GetHelpMessage()
set s to ""
set s to s & "Formats available : "
set s to s & "(1 thru 8) are the same as (9 thu 16), escept (1
thru 8) pad single digits with a leading zero :" & return & return
set theDate to current date
my bh_date_format_Update(theDate)
set myFormats to ""
repeat with counter from 1 to count items in bh_aFormatSearch
set myFormats to myFormats & "( " & counter & " ) " & item
counter of bh_aFormatSearch & return
end repeat
display dialog s default answer myFormats
end bh_date_format_GetHelpMessage
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden