Hi Nigel,
thank you.
While waiting for a reply I started making some experiments…
Every script that operate on finder items at some time requires the list of the files and folders for a given path.
So the first handler I wrore, ItemsInFolder, does exacty that. I wrote it both in the tell finder way and in the do shell script way.
For testing I wrote a third handler that uses ItemsInFolder to get the count of the items (files and folders) inside a given folder, scanning subfolders recursively.
I got the following results (in the form of {items count, seconds})
Finder way, via ItemsInFolderFinder
{14515, 242}
Shell way, via ItemsInFolderShell
{14517, 9}
The speed gain is about 25x
The script is as follows
set f to "MyUsbHD:Marketing - local:" -- this is the path I used for test
set d1 to current date
set n to CountItemsRecursive(f)
set d2 to current date
return {n, d2 - d1}
on CountItemsRecursive(f)
set n to 0
set itm to ItemsInFolderShell(f) -- change to ItemsInFolderFinder(f) to test "finder way"
script ss
property ritm : itm
end script
set L to length of ss's ritm
set n to n + L
repeat with i from 1 to L
if last character of (item i of ss's ritm) is ":" then set n to n + CountItemsRecursive(item i of ss's ritm)
end repeat
return n
end CountItemsRecursive
on ItemsInFolderFinder(f)
set res to {}
try
tell application "Finder" to set ilist to items of folder f
on error
return {}
end try
script ss
property Rilist : ilist
end script
set L to length of ss's Rilist
repeat with i from 1 to L
set end of res to (item i of ss's Rilist) as string
end repeat
return res
end ItemsInFolderFinder
on ItemsInFolderShell(f)
set pof to POSIX path of f
try
set ilist to paragraphs of (do shell script "ls -1 -p " & quoted form of pof)
on error
return {}
end try
script ss
property Rilist : ilist
end script
set L to length of ss's Rilist
repeat with i from 1 to L
set item i of ss's Rilist to (POSIX file (pof & (item i of ss's Rilist))) as Unicode text
end repeat
return ss's Rilist
end ItemsInFolderShell
It is notable that ls ends up finding 2 more items.
In fact it turned out that
tell application "Finder" to set ilist to items of folder f
is quite difficult to translate accurately to a script that relies on shell’s ls
If you run ls on the root path you get some invisible items the Finder doesn’t show, for example Volumes
If Finder preferences are tweaked to show invisible items, then the Finder shows more items than ls -1 -p
If the above is changed to ls -1 -p -A then ls and the Finder return the same number of items, but some of them differs:
ls follows symbolic links to the referenced file (even if -P directive is added), furthermore Finder shows packages as files (Safari.app), ls shows them as folders (Safari.app:).
These are just some examples, I think more issues may reveal when dealing with alias files or remote volumes…
Thank you,
Paolo.