Analysing folder contents in 8.1 (was URL access scripting)
Analysing folder contents in 8.1 (was URL access scripting)
- Subject: Analysing folder contents in 8.1 (was URL access scripting)
- From: Kai Edwards <email@hidden>
- Date: Sun, 16 Dec 2001 17:52:13 +0100
>
Date: Fri, 14 Dec 2001 21:33:01 -0700
>
Subject: Re: URL access scripting
>
From: Bryan Kobler <email@hidden>
>
To: email@hidden
>
>
In a script I am writing I look at a folder and get the contents, make
>
an alias list, then ftp the contents. This works with multiple folders
>
in OS 9 but when I take the script to OS 8.1 it doesn't work. With one
>
folder it does work, so I guess the version of AppleScript on 8.1 is a
>
little different.
>
>
Does anyone know if I need a scripting addition for 8.1 or perhaps have
>
a loop function that breaks the folder down to one folder at a time? I
>
think I have see that before, can someone point me in the right
>
direction?
>
>
Thanks, Bryan.
I vaguely recall some looping process of the kind, too - although I haven't
been able to turn it up.
However, what could work here is a handler that calls itself whenever a
folder is detected within the folder currently being analysed - but only
after contents of the current folder have been noted. (See the
'getFolders()' handler below.)
While I'm not experienced enough to offer much in the way of particularly
fast or efficient scripts, I have tested the following effort on 8.1 and
found that it at least works.
Of course, it really depends what you're trying to get from such a script.
This one merely searches out every folder within the main one and then lists
each file (as an alias) within all the folders found. If you want to end up
with a hierarchical list showing files within each folder, you'd need to
modify the approach. (When trying to extract filenames, I resisted using
'list file' since it appears to result in the inclusion of custom icons.)
Anyway, FWIW you may find this a reasonable starting point (as usual, watch
out for line wraps)...
**********************************
global folderList, fileList
on run
analyseFolder(choose folder with prompt "Please select a folder to
analyse")
end run
on open theItem --save script as an application to use as a droplet
if (count theItem) > 1 then
err("I can only handle one item at a time.")
else
if not folder of (info for theItem) then
err("I can only analyse the contents of a folder.")
else
analyseFolder(theItem)
end if
end if
end open
on analyseFolder(mainFolder)
set {folderList, fileList} to {mainFolder as list, {}}
tell application "Finder" to activate --speeds up Finder responses
getFolders(mainFolder)
repeat with theFolder in folderList
getFiles(theFolder)
end repeat
--do whatever is required with fileList
end analyseFolder
on getFolders(thisFolder)
try
tell application "Finder" to set theFolders to folders of thisFolder
on error
set theFolders to {}
end try
if theFolders {} then --thisFolder contains at least one more folder
set folderList to folderList & theFolders --add any new folder(s) to
the folderList
repeat with theFolder in theFolders --check each new folder for the
presence of further folders
getFolders(theFolder) --handler calls itself here
end repeat
end if
end getFolders
on getFiles(theFolder)
try
tell application "Finder" to set theFiles to files of theFolder as
alias
--for a list of filenames only, use 'name of files of theFolder'
instead
on error
set theFiles to {}
end try
set fileList to fileList & theFiles
end getFiles
on err(theMsg)
beep
display dialog "Sorry - " & theMsg buttons "Cancel" default button 1
with icon 0
end err
**********************************
--
**********************************
Kai Edwards Creative Resources
1 Compton Avenue Brighton UK
Telephone +44 (0)1273 326810
**********************************