List of subfolders

Hi,

I’m looking for help in creating (or modifying) a script that will copy all subfolders names to the clipboard from a selection of folders in the finder.

For example If I have a folder structure like this:

folder 1
subfolder 1
subfolder 2
folder 2
subfolder 3
folder 4
subfolder 4

If i select the parent folders (ie I select folder 1, 2, and 3 in the finder) I need to return this list of the subfolders to the clipboard (ignoring any files)

subfolder 1
subfolder 2
subfolder 3
subfolder 4

I have a working script, below, that will give me the folder names of whatever I select, but instead I need the subfolders names.

Thanks in advance!



set theSel to (selection of application "Finder")
set pathList to {}
repeat with anItem in theSel
	set the end of pathList to name of (info for anItem as alias)
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to pathList as text
set text item delimiters to TID


Getting Posix paths list:

property pathList : {}

tell application "Finder" to set aFolders to (selection) as alias list
repeat with aFolder in aFolders
	my get_All_Folders_of_Folder(aFolder)
end repeat
set the clipboard to pathList
return pathList

on get_All_Folders_of_Folder(aFolder)
	tell application "System Events"
		set subFoldersList to every folder of aFolder
		if subFoldersList is {} then
			set the end of pathList to (path of aFolder)
		else
			repeat with aFolder in subFoldersList
				my get_All_Folders_of_Folder(aFolder)
			end repeat
		end if
	end tell
end get_All_Folders_of_Folder

NOTE: If you want not Posix paths list, but names list, then replace (path of aFolder) with (name of aFolder)

Another suggestion. It’s reasonably quick with a small number of folders but becomes slow and perhaps unusable if the number of subfolders is large.

BTW, there is no folder 3 in the OP’s example and I assume there is a typo in his/her post.

set folderNames to {}

tell application "Finder"
	set selectedFolders to selection as alias list
	repeat with aFolder in selectedFolders
		try
			set folderNames to folderNames & name of every folder of entire contents of aFolder
		end try
	end repeat
end tell

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set the clipboard to (folderNames as text)
set AppleScript's text item delimiters to ATID

Fortunately, the OP only specified immediate subfolders, so you could justifiably leave out searching for deeper levels. :wink: I’d include a check though to make sure that each item in the selection actually is a folder.

set folderNames to {}

tell application "Finder"
	set selectedFolders to selection
	repeat with aFolder in selectedFolders
		if (aFolder's class is folder) then
			set folderNames to folderNames & name of aFolder's folders
		end if
	end repeat
end tell

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set the clipboard to (folderNames as text)
set AppleScript's text item delimiters to ATID

the clipboard

Thanks Nigel. It was unclear to me what the OP wanted, as he states “all subfolders” but his example only shows immediate subfolders. I’ve fixed my script in case a selection is not a folder, so the OP has a workable script for all subfolders and immediate subfolders.

Hi!

Thanks for the help on this.

The omission of folder 3 was indeed a typo. Blarg.
And in this particular use case, there is only one level of subfolders and your script works perfect for my needs.

I’d buy you a beer if I could!