Re. Differentiating between "A1", "A12", "A13"
Re. Differentiating between "A1", "A12", "A13"
- Subject: Re. Differentiating between "A1", "A12", "A13"
- From: has <email@hidden>
- Date: Thu, 10 Jan 2002 12:59:44 +0000
David.Wadson wrote:
>
True, but whether I have leading zeros or a specific character after each
>
filename, than I have to operate under the assumption that the user(s) have
>
named every file properly. I'm trying to put in some margin for error here
>
in case the user mistypes the name.
Quite right. If the filenames were machine-generated I wouldn't think twice
about leading zeroes being compulsory, but for human users I don't think
it's wise - or reasonable - to insist on it. (Be forgiving to humans, but
strict with machines.)
>
I was hoping that there would be some
>
sort of easy routine for doing this. Too bad Applescript doesn't allow the
>
use of regular expressions because it looks like my only options are some
>
convoluted coding.
If you're happy using 3rd-party osaxen, the Satimage osax has regex.
Another is Regex Commands, though it won't work on OS X. (See osaxen.com)
However, you don't need to be too convoluted (just a little bit lateral).
Here's a simple extractor/sorter that'll work for files from A0 to A99:
======================================================================
on makeEmptyList()
set emptyList to {}
repeat with x from 1 to 100
set emptyList's end to missing value
end repeat
emptyList
end makeEmptyList
property emptyList : makeEmptyList()
property theFolder : choose folder "Choose source folder:"
on listFiles()
copy emptyList to finishedList
tell application "Finder"
tell its folder theFolder
set theList to every file whose name begins with "A"
try
repeat with x from 1 to count theList
set fileName to theList's item x's name
if fileName's character 3 is in "0123456789" then
set offsetPos to fileName's text 2 thru 3
else
set offsetPos to fileName's character 2
end if
set finishedList's item (offsetPos + 1) to theList's
[NO-BREAK]item x as alias
end repeat
on error number -1700
error "Bad filename: " & fileName & " (should have a
[NO-BREAK]number after A)."
end try
end tell
end tell
finishedList's aliases
end listFiles
listFiles()
--> returns a list of aliases, sorted by name from A0 to A99
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
It's not fussy whether you use A1 or A01, and anything up to A99 is
supported. You could easily adapt it to take the filenames' first
character(s) as a parameter, allowing you to use the handler more generally
- e.g. listFiles("B"), listFiles("FN").
You'll need to change the error handling if you want it to ignore files
whose names begin with "A" but aren't followed by a number - right now it
just throws a fatal error when that happens. Up to you.
Also, if any files have 3 digits or more only the first two digits will be
considered, giving unwanted results. If you have any files with names
beyond A99 you'll need to modify the code to handle them correctly.
HTH
has