Getting the count of every folder of subfolders in a repeat block

I’m having an issue getting the count of every folder within the subfolders of a parent folder. I want the script to create an alias in a new folder to every folder in the subfolders of the parent folder.

So, if my directory structure looks like this:

Parent
”>Subfolder
””>Sub-subfolder
”>Subfolder
””>Sub-subfolder
””>Sub-subfolder

Then I want to make an alias to every sub-subfolder.

The contents of the parent folder will change, so when the script is run again, I need it to check for any new sub-subfolders and create aliases to those as well. The way I was going to accomplish that was to use a repeat block and then only create an alias to those sub-subfolders who’s names didn’t match the aliases already created.

The problem is that I need the count of all the sub-subfolders, so I can tell my repeat block when to stop, but I’m not sure how to get that.

The part of the script I need this for:

		
repeat with i from 1 to the_subcount

set the_subfolder_item to item i of the_subfolder
set the_subsubfolder to (every folder of the_subfolder_item)
set the_subsubcount to count of the_subsubfolder

The the_subsubcount variable needs to match the count of all the sub-subfolders. The way it’s put here just gives me the count of the folders in the first subfolder.

And the full script:


tell application "Finder"
	
	set the_source to folder "Source"
	set the_subfolder to (every folder of the_source)
	set the_subcount to count of the_subfolder
	
	if (exists folder "Destination" of desktop) then
		
		set the_destination to folder "Destination"
		
		repeat with i from 1 to the_subcount
			
			set the_subfolder_item to item i of the_subfolder
			set the_subsubfolder to (every folder of the_subfolder_item)
			set the_subsubcount to count of the_subsubfolder
			
			repeat with i from 1 to the_subsubcount
				
				set the_subsub_item to item i of album
				set the_subsub_name to (name of the_item)
				
				set the_aliases to (every item of the_destination)
				set the_alias to item i of the_aliases
				set the_alias_name to (name of the_alias)
				
				if not the_subsub_name = the_alias_name then
					
					make new alias to the_subsub_item at the_destination
					
				end if
			end repeat
		end repeat
		
	else
		
		make new folder at desktop with properties {name:"Destination"}
		
		repeat with i from 1 to the_subcount
			
			set the_destination to folder "Destination"
			set the_subfolder_item to item i of the_subfolder
			set the_subsub_item to (every folder of the_subfolder_item)
			
			make new alias to the_subsub_item at the_destination
			
		end repeat
	end if
end tell

Hi. You could actually use a one line statement to alias your folder content without looping.

tell application "Finder" to make alias at folder "Destination" to (get ((folder "Source"'s entire contents)'s folders)'s folders)

Thanks! That works great! I had no idea I could do that.

With your help, I was able to make the script work, and now it’s much less complicated.


tell application "Finder"
	
	if exists folder "Destination" of desktop then
		
		set theSource to folder "Source"
		set theDestination to folder "Destination"
		set theArtists to (every folder of theSource)
		set ArtistsCount to count of theArtists
		set aliasNames to name of items in theDestination
		
		repeat with theAlbum in (get ((folder "Source"'s entire contents)'s folders)'s folders)
			set albumName to name of theAlbum
			if albumName is not in aliasNames then
				make alias at theDestination to theAlbum
			end if
		end repeat
		
		activate
		open theDestination
		
	else
		
		make folder at desktop with properties {name:"Destination"}
		make alias at folder "Destination" to (get ((folder "Source"'s entire contents)'s folders)'s folders)
		set theDestination to folder "Destination"
		
		activate
		open theDestination
		
	end if
	
end tell