image
image

Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old March 11th, 2006, 09:11 PM
Registered User
 
Join Date: Jun 2003
Location: Whitehorse, Yukon
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
neuby is on a distinguished road
Applescript recursive calls - help debugging please...

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:Usersld_bfindlayesktop: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.
Reply With Quote
  #2  
Old March 12th, 2006, 02:07 AM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,462
Thanks: 3
Thanked 10 Times in 9 Posts
Mikuro is on a distinguished road
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.
__________________
Mac mini — 1.25GHz G4, 1GB RAM — OS 10.5.2

I'm now a four-browser man. How on earth did this happen?!

Useful programs: PithHelmet, Butler, ffmpegX, VLC, Perian, Tofu, Wcalc
Reply With Quote
  #3  
Old March 12th, 2006, 02:52 AM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,462
Thanks: 3
Thanked 10 Times in 9 Posts
Mikuro is on a distinguished road
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.
__________________
Mac mini — 1.25GHz G4, 1GB RAM — OS 10.5.2

I'm now a four-browser man. How on earth did this happen?!

Useful programs: PithHelmet, Butler, ffmpegX, VLC, Perian, Tofu, Wcalc
Reply With Quote
  #4  
Old March 12th, 2006, 06:44 AM
Registered User
 
Join Date: May 2005
Posts: 1,339
Thanks: 0
Thanked 1 Time in 1 Post
barhar is on a distinguished road
'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'.
]

Last edited by barhar; March 12th, 2006 at 08:35 AM.
Reply With Quote
  #5  
Old March 12th, 2006, 09:28 PM
Registered User
 
Join Date: Jun 2003
Location: Whitehorse, Yukon
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
neuby is on a distinguished road
Barhar, Mikuro - thanks so much for your help. Got it running successfully tonight! Woo hoo!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Debugging STL code with Xcode Viro Software Programming & Web Scripting 0 February 10th, 2006 07:34 AM
debugging console applications bladerun Unix & X11 0 July 5th, 2004 02:51 AM
iChat debugging console? kilowatt Mac OS X System & Mac Software 0 September 17th, 2003 10:08 AM
ProjectBuilder, debugging! Oddysseey Software Programming & Web Scripting 0 June 17th, 2001 05:01 PM
cli FTP with put recursive option WhateverJoe Mac OS X System & Mac Software 3 April 20th, 2001 11:26 AM


All times are GMT -5. The time now is 06:53 AM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.