Re: Sorting file lists
Re: Sorting file lists
- Subject: Re: Sorting file lists
- From: kai <email@hidden>
- Date: Tue, 21 Oct 2003 19:36:50 +0100
on Mon, 20 Oct 2003 13:25:25 +0100, "Dr Digby L. James" wrote:
>
Thanks Nigel and Kai. I'll have a play with these and let you know how I get
>
on.
Actually Digby, I'm rather pleased that Nigel chipped in.
Since my own suggestion was more by way of an initial experiment, his more
refined offering is bound to be more robust. Also, as anyone who is familiar
with Nigel's work will know, he generally manages to wring every last drop
of performance from a routine. :-)
There's yet another potential benefit from adopting Nigel's approach -
because it gets the required information using the Finder (rather than
Standard Additions' 'list folder')...
From your message of Mon, 20 Oct 2003 00:29:13 +0100:
>
QuarkXPress got an error: Folder some object wasn't found.
...it seems likely that, as Bill Metzinger suggested, you may have an item
within your folder that QuarkXPress can't place in your document. (That
"*Folder* some object" looks a mite suspicious, anyway.)
I noticed that, later in the same message, your script contains a note:
>
--To do: check files are all images - with .tiff extension - and rewrite list
>
if necessary
Part of Nigel's script (Mon, 20 Oct 2003 10:33:38 +0100) already screens out
items that are not files - simply because it tells the Finder to look for
'files', rather than 'items':
>
> tell application "Finder"
>
> set {creationDates, fileNames} to {creation date, name} of theFolder's files
>
> end tell
If all of your required files are tiffs, then you should also be able to
filter out non-tiff files - by modifying the second line above to something
like:
----------------
set {creationDates, fileNames} to {creation date, name} of (theFolder's
files whose file type is "TIFF")
-- or (less safe here - but possibly useful in other situations):
-- whose name extension is "tiff"
-- whose name ends with "tiff"
----------------
I hope you won't mind if I also add a couple of general observations about
coercion and concatenation[1].
The 'choose folder' command returns an alias:
-----------------------------------------------------
set theFolder to choose folder
--> alias "path:to:folder:"
-----------------------------------------------------
Now let's say that, using either the Finder or 'list folder', we get a list
of the folder's files - and assign it to a variable, 'fileList'. Then we
concatenate theFolder with each item in 'fileList' to get a series of file
paths. We could use a repeat loop to do it like this:
-------------------------------
repeat with theFile in fileList
set someVariable to theFolder & theFile as string
--> {alias "path:to:folder:", "filename"} as string
--> "path:to:folder:filename"
-- do something with someVariable
end repeat
-------------------------------
As you can see, when 'theFolder' & 'theFile' are concatenated, the result is
a list - which is then coerced (explicitly) to a string.
Since this method works, there shouldn't be any problem with it. However,
you can achieve similar results (usually much more efficiently) by coercing
the folder alias to a string first - and then concatenating the filenames:
-------------------------------
set thePath to theFolder as string
--> "path:to:folder:"
repeat with theFile in fileList
set someVariable to thePath & theFile
--> "path:to:folder:filename"
-- do something with someVariable
end repeat
-------------------------------
Because the variables 'thePath' and 'theFile' are now both strings, there's
no need for an intermediate list - or a coercion to a string. The two string
values are simply concatenated.
I compared the difference between the two methods, and found the latter to
be anywhere between 8 to 20 times faster than the former (depending on the
number of files involved)[2].
Just thought you might like to consider an alternative...
---
kai
===============================
[1] For anyone not familiar with concatenation in AS, it's simple enough
with, say, strings:
"a" & "b"
--> "ab"
However, with items of different classes, the first item may (where
appropriate - and by implicit coercion) affect the class of subsequent
items:
"a" & 1
--> "a1"
(A number can be coerced to a string.)
Where items of different classes can't be concatenated in a single class,
the result is usually a list:
1 & "a"
--> {1, "a"}
(A string can't be coerced to a number.)
===============================
[2] I checked out a tid-based alternative too, but couldn't really see any
significant advantage - especially since the file paths need to be used
within a repeat loop anyway.
===============================
_______________________________________________
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.