RE: filename/date validation [was: RE: applescript-users digest, Vol 3 #346 - 12 msgs]
RE: filename/date validation [was: RE: applescript-users digest, Vol 3 #346 - 12 msgs]
- Subject: RE: filename/date validation [was: RE: applescript-users digest, Vol 3 #346 - 12 msgs]
- From: has <email@hidden>
- Date: Fri, 12 Apr 2002 20:30:21 +0100
Steve Suranie wrote:
>
Hi folks:
>
>
I'm writing a script of which a portion validates file names of a file.
>
The file name structure is as follows:
>
>
NP041102_IL_DESCRIPTION.tif
[...]
>
I'm getting stumped at how to validate the date.
>
>
I thought I could try to coerce the string to an integer and then try to
>
trap for an error such as this:
[...]
>
set dateCheck to text from character 3 to 8 of word 1 of fileName
This line can be simplified:
set dateCheck to text 3 thru 8 of fileName
Also, you should make your error trapping as specific as possible:
try
set dateCheck to dateCheck as integer
on error number -1700
-- [your 'bad value' handling code goes here]
end
As you have it now, a bug anywhere in your "do other scripting stuff here"
code would throw an error and you wouldn't know it from an error caused by
the integer coercion.
--
On a related note (and a shameless self-plug to boot:), you might want to
check out the dateLib library at my site (link below). It includes a nifty
function for converting strings into date objects based on a user-specified
format string; eg:
======================================================================
global dateLib
set dateLib to load script file "MacHD:datelib" --[your path here]
tell dateLib to setDefaultFormat("mmddyy")
--
tell dateLib to stringToDate({theString:"041102"})
--> date "Thursday, April 11, 2002 12:00:00am"
======================================================================
Can't beat it for flexibility and convenience, and it's a pretty decent
performer.
Otherwise (and just to head Nigel Garvey off at the pass here;) if pure,
unadulterated speed is what you need most then you'll most likely want to
roll your own custom-tailored routine:
======================================================================
on getBaseDate()
tell (current date)
set its time to 0
it
end tell
end getBaseDate
property baseDate : getBaseDate()
property monthList : {January, February, March, April, May, June,
[NO-BREAK]July, August, September, October, November, December}
on daysInMonth(theDate) -- modified from Nigel Garvey's Date Tips
copy theDate to d
tell d
set day to 32
set day to 1
day of (it - days)
end tell
end daysInMonth
on mmddyyToDate(theString) -- MAIN CALL
try
if theString's length is not 6 then error number -1700
set theNum to theString as integer
if theNum is less than 1 then error number -1700
copy baseDate to newDate
try
set newDate's month to monthList's item (theNum div 10000)
on error number -1728
error number -1700
end try
set newDate's year to 2000 + (theNum mod 100)
set theday to (theNum div 100 mod 100)
if theday is greater than daysInMonth(newDate) then error
[NO-BREAK]number -1700
set newDate's day to theday
newDate
on error number -1700
error "Bad date: " & theString number 100
end try
end mmddyyToDate
======================================================================
Think that should be robust (though hasn't been heavily tested) and faster,
if less flexible.
HTH
has
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.