Applescript recursive calls - help debugging please...

neuby

Registered
Trying to write code that recursively 'drills down' into nested folders, then applies a script to the contents of the bottom folders files.

Here is the code so far - I have a structure with the following folders:

temp>2006>Jan>Jan1>many files
..................................Jan3>many files
..................................Jan 15>many files
..................................file 1
..................................file 2
............................feb>feb9>many files

so - five levels of nesting, with some files at the fourth level in Jan. Here is the drill_down code:

on drill_down(fpath)

tell application "Finder"
set item_list to list folder fpath without invisibles
set counter to count of item_list
set fpath to fpath as string
repeat with i from 1 to counter
set this_item to item i of the item_list
set this_item to (fpath & this_item) as alias
set this_info to info for this_item
set the container_name to the name of container of this_item
if kind of this_info is "Folder" then
open this_item
my drill_down(this_item)
end if
beep 1
return (container of this_item)
exit repeat
end repeat
end tell
end drill_down

drill_down({alias "Macintosh HD:Users:eek:ld_bfindlay:Desktop:temp:"})

When I run this - it beeps four times, opens the folders 2006, Feb and Feb9 and the result is:

folder "temp" of folder "Desktop" of folder "old_bfindlay" of folder "Users" of startup disk of application "Finder"

So I am misunderstanding what this code is telling applescript to do. What I thought it would do would be to drill down to the Feb9 level, beep once and return the container value 'Feb9' - indicating it was on the first of the files that reside there.

...color me lost.
 
I think the problem is that when you return a value from within a custom handler like drill_down, it DOES NOT cease the entire script; it merely ends that handler, and the rest of the script goes along its merry way. So, no matter how many folders it drills through, the last "return" item will always be the first handler that was started. That's also why it beeps all 4 times, not just once.

Try this is a test: at the very end of your script, after you call drill_down, put in "return 1". Now run the script, and the end result will be "1", because nothing you return in drill_down will stop the script.
 
I wasn't sure exactly what to recommend before, but I just realized a simple change that I think would give you the result you want. Just change "my drill_down(this_item)" to "return my drill_down(this_item)". That way the final drill_down will pass its result all the way down the drill_down chain.
 
'folder "temp" of folder "Desktop" of folder "old_bfindlay" of folder "Users" of startup disk of application "Finder"' - is correct; based on the code presented.

The code you seek, Grasshopper, is ...

Code:
drill_down(alias "Macintosh HD:Usersld_bfindlayesktop:temp:")

on drill_down(fpath)
	tell application "Finder"
		activate -- use, if you want 'Finder' to become front most process.
		set item_list to list folder fpath without invisibles
		
		repeat with i in item_list
			set this_item to ((fpath as string) & i & ":") as alias
			
			if ((kind of this_item) is "Folder") then
				open this_item
				my drill_down(this_item)
			end if
		end repeat
		
	end tell
end drill_down

[
Added 12 Mar. 2006 at 9.35.

One does not have to 'open' a folder to rename the items within it.

Based on 'Automator/Apple script question...' of 09 Mar. 2006 ...

'I would like to rename the files so that pict#####.jpg, etc becomes Jan 3-1.jpg, Jan 3-2.jpg etc.'

Code:
if ((kind of this_item) is "Folder") then
	my drill_down(this_item)
else if ((name extension of this_item) is "jpg") then
--else if ((kind of this_item) contains "Document") then
	my rename_file(this_item)
end if

or

Code:
if ((kind of this_item) is "Folder") then my drill_down(this_item)
if ((name extension of this_item) is "jpg") then my rename_file(this_item)
--if ((kind of this_item) contains "Document") then my rename_file(this_item)

Naturally, a new handler titled 'on rename_file(fPath) ... end' is then required.
Notice the commented out line? ...

--if ((kind of this_item) contains "Document") then

... As you can see, one can check a file by 'name extension' or by 'kind'. Not all documents have a 'kind' of 'Document' or 'document'. For example, Adobe Photoshop 'psd' files have a 'kind' of 'Adobe Photoshop file'.
]
 
Back
Top