Re: Problem with folder searching script
Re: Problem with folder searching script
- Subject: Re: Problem with folder searching script
- From: kai <email@hidden>
- Date: Sat, 23 Oct 2004 20:16:05 +0100
On 21 Oct 2004 14:33:05 -0700, John Goodchild wrote:
I am trying to write a script that searches the contents of a set of
folders. When the script finds a folder, it processes the contents of
that folder etc, etc.
I am having difficulty parsing the new folder name. The script stops
with the error message "Expected a reference". Below is the basic form
of the script:
on open theFolder
parseFolder(theFolder)
end open
on parseFolder(aFolder)
tell application "Finder"
set theNumItems to number of items in aFolder
end tell
repeat with i from 1 to theNumItems
tell application "Finder"
set anItem to item i of aFolder
if kind of anItem = "Folder" then
set contProc to true
else
set contProc to false
end if
end tell
if contProc then
parseFolder(anItem) -- gives error "Expected a reference"
end if
end repeat
end parseFolder
Hopefully, Michelle's excellent template will have given you a much
better way forward, John. :-)
In addition, I wondered if it might help to look at a couple of
specific points from your original script.
Firstly, exactly why *did* your version error? The error message
("Finder got an error: Expected a reference.") gives us a clue here. I
believe this is an Apple event manager error (-1727), usually caused by
an object of the 'wrong' type.
Evidently, the Finder in OS X currently requires a reference to perform
a 'count' of a folder's items (the 'count' command is generally
synonymous with 'number of'). An 'open' handler like the one in your
script (which I assume you intended to work as a droplet) would
normally process a list of aliases - so I believe the error may
actually be occurring on the second* iteration of the 'parseFolder'
handler, just here:
set theNumItems to number of items in aFolder
[* Why the second iteration? Because first time around, the variable
'aFolder' actually represents the initial list of aliases, rather than
a specific folder/alias.]
To demonstrate the problem, try running this snippet:
----------------
tell application "Finder" to count items in (path to "docs")
--> error: "Finder got an error: Expected a reference."
----------------
This is because < path to "docs" > returns an alias (e.g: alias
"Macintosh HD:Users:username:Documents:"), rather than a Finder
reference (e.g: folder "Documents" of folder "username" of folder
"Users" of startup disk of application "Finder").
There are various ways to work around this. We could, for example,
evaluate a folder/alias's items and then count the resulting list:
----------------
tell application "Finder" to count (get items in (path to "docs"))
----------------
Alternatively (and better/faster), we could give the Finder what it
currently seems to expect:
----------------
tell application "Finder" to count items of folder (path to "docs")
----------------
In fact, with this approach, we can do away with 'items' altogether:
----------------
tell application "Finder" to count folder (path to "docs")
----------------
However, to be fair to your script, it still seems to me that the
Finder in OS X is being somewhat intolerant over the 'count' command -
so I checked a few syntax variations in OS 9. Confirming my
recollections, the Finder there was quite happy to resolve something as
simple as:
----------------
tell application "Finder" to count (path to "docs") -- (works in OS 9
but not in OS X)
----------------
Chris Nebel may already have a take on this, but I suspect a bug might
have crept in somewhere along the line...
--
Finally, a general suggestion for testing a condition and assigning a
Boolean value to a variable. Instead of:
if kind of anItem = "Folder" then
set contProc to true
else
set contProc to false
end if
- you might in future consider something like:
----------------
set contProc to kind of anItem = "Folder"
----------------
...which should do pretty much the same thing.
HTH
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden