Creating sub folders based on multiple parent folders

Im trying to build a script that will create subfolders based on the parent name like this

folder AAAAA BBBBB would have 2 folders inside of it. AAAAA and BBBBB

I’ve managed to cobble together something based upon red_menace’s script but it only works for one folder, and i’m fairly sure I don’t need some of the code to do what i’m doing.

on run {input, parameters}
	(*
make new sub folders in the specified folder from a list of names
names that contain colons (:) will create intermediate sub folders as required
input: a list of names (text items)
output: a list of Finder items (aliases) created
*)
	set output to {} -- this will be a list of the created folders (for each name item)
	set useExistingFolders to true -- use folders that already exist?
	
	repeat with anItem in the input
		set anItem to anItem as text
		if anItem is not "" then -- skip blank items
			set targetFolder to (get path to home folder)
			set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
			set {nameList, AppleScript's text item delimiters} to {text items of anItem, tempTID}
			
			repeat with theName in nameList -- deal with sub folders
				if not useExistingFolders then set theName to (getUniqueName for theName from targetFolder) -- avoid duplicate names
				try
					tell application "Finder"
						make new folder at targetFolder with properties {name:theName}
						set targetFolder to (the result as alias)
					end tell
				on error number -48 -- folder already exists
					set targetFolder to ((targetFolder as text) & theName) as alias
				end try
			end repeat
			set the end of output to targetFolder
		end if
	end repeat
	
	return output
end run

I’m running a shell script before this script to give me the folder name rather than the path and then returning words of the folder name, and I’ve set a variable to place the new folders inside the original one.
It all works perfectly, but id like to be able to do multiple folders at once.

I looked at the automator action ‘dispense items incrementally’ but I’d rather not install this on every computer I plan on running this script on. So was hoping for a applescript solution.

I’m guessing its a repeat function but I can’t work out how to wrap all of what my automator script is doing into one bit of applescript.

So if this all makes any sense, some help with the repeat function would be grand, or a script to replace the dispense items incrementally action.

Thanks

Hi,

I don’t see what is so hard about this. All you need to do is:

get the name of the folder
parse the name
create folders depending on how many folder names seperated by spaces in the main folder inside the main folder

What is going wrong?

Edited: maybe some of the folder names have spaces in them?

gl,
kel

Hi Kel

I’m totally new to this, I can just about work out what scripts are doing, and can usually work out how to tweak things to what I want them to do, but when I have a big script with lots of different things going on then I’m lost.
I can usually work out the logic in automator but switching that to applescript has me stumped.

I have a list of folder names that I want to use the script on. The one I posted works for individual ones but I’d like to be able to just copy the whole list in one go.

The list would be something like (numbers to show individual list, and names are random digits but all have a space in them, and some have more spaces than others.)

  1. AAAAAA BBBBBB
  2. CCCCCC DDDDDD CCCCCCC
  3. AAAAAA EEEEEEEE HHHHHHHH
  4. FFFFFFF YYYYYY

and i’d like to create subfolders into each of these based on the names between the spaces.
So AAAAAA and BBBBBB into the AAAAAA BBBBBB folder.

I can make it work if I use the dispense items incrementally automator action but i’d prefer an applescript answer as that is more portable.

Basically I have no idea how cobble this all together!

Hi Jon,

It’s pretty easy to create subfolders from the parent folder names using AppleScript Text Item Delimeters. There are probably a thousands of post on this. I have the feeling that there may be more to your post than meets the eye.

But, if you really want a simple subroutine for learning purposes I need to get back into teaching mode :).

Later,
kel

Here’s a simple example of deliminating “a b c”"

set t to "ant bat cow"

set AppleScript's text item delimiters to space
set name_list to text items of t
repeat with this_name in name_list
	say this_name
	beep 1
end repeat

Darn I used to have more imagination than this, but for what it’s worth. :smiley:

gl,
kel

Hi,

this is a solution which creates the 4 mentioned folders at desktop and creates the requested subfolders


set theFolders to {"AAAAAA BBBBBB", "CCCCCC DDDDDD CCCCCCC", "AAAAAA EEEEEEEE HHHHHHHH", "FFFFFFF YYYYYY"}

-- create the folders at desktop
repeat with aFolder in theFolders
	try
		tell application "Finder" to make new folder at desktop with properties {name:aFolder}
	on error e number n
		if n is not -48 then
			display dialog e
		end if
	end try
end repeat

set desktopPOSIX to quoted form of POSIX path of (path to desktop)
set TID to text item delimiters
repeat with aFolder in theFolders
	set text item delimiters to space
	set foldersToCreate to text items of aFolder
	set text item delimiters to ","
	set foldersToCreate to foldersToCreate as text
	do shell script "/bin/mkdir -p " & desktopPOSIX & quoted form of aFolder & "/{" & foldersToCreate & "}"
end repeat
set text item delimiters to TID


Hi Stefan,

You guys are sleeping man! :slight_smile: The op posted 3 days ago on an easy question.! Just joking:D.

I was thinking again that you were from France (by your name), but disregard because I remember that you’re from somewhere else. Was going to ask about the terrorsism so disregard this.

gl,
kel

Thanks guys, this is not quite what i’m after but i think I can work out an automator workflow to move the files where I need rather than the desktop.
Ideally i’d like to be able to have some folders selected and then run the script and it places the subfolders inside the folders. But i can just move them from the desktop and replace via automator however I’m hitting a stumbling block with the names anyway!

on run {input, parameters}
	tell application "Finder"
		set fileNames to {}
		set theItems to selection
		repeat with itemRef in theItems
			set end of fileNames to name of itemRef
		end repeat
	end tell
	
	set theFolders to {fileNames}
	
	-- create the folders at desktop
	repeat with aFolder in theFolders
		try
			tell application "Finder" to make new folder at desktop with properties {name:aFolder}
		on error e number n
			if n is not -48 then
				display dialog e
			end if
		end try
	end repeat
	
	set desktopPOSIX to quoted form of POSIX path of (path to desktop)
	set TID to text item delimiters
	repeat with aFolder in theFolders
		set text item delimiters to space
		set foldersToCreate to text items of aFolder
		set text item delimiters to ","
		set foldersToCreate to foldersToCreate as text
		do shell script "/bin/mkdir -p " & desktopPOSIX & quoted form of aFolder & "/{" & foldersToCreate & "}"
	end repeat
	set text item delimiters to TID
	
	return input
end run

if i do this i get the following error

Finder got an error: Can’t make {“AAAAAA BBBBBB”, “CCCCCC DDDDDD CCCCCCC”, “AAAAAA EEEEEEEE HHHHHHHH”, “FFFFFFF YYYYYY”} into type Unicode text.
and if

set theFolders to {fileNames} as text

i then get every letter individually.

I tried splitting up the code into 2 separate scripts to pass the items, but when i then pass as text it just makes one long folder name.

Thank for your help

OK I’ve guessed my way to get the folders made by using

set theFolders to {fileNames} as text item

rather than just text. So now I can pass the items to the scrip rather than hardcode.

But I can’t work out how to get the new folders so I can move them. I just don’t know what to return?
Ideally i’d like to use the original files presented to the script if thats possible, but just a way to select the new folders created would be amazing.

As its now 4 am and I need to be at work at 8 i think i better give up on this for now!

EDIT

I lied, I kept messing about and now I’ve broken it again the above doesn’t seem to work.

My script was only an example how to create the subfolders.
If you want to create the folders from a file you have to consider also the name extension I guess.
Try this


on run {input, parameters}
	set theFolders to {}
	tell application "Finder"
		set theItems to selection
		repeat with itemRef in theItems
			set {name:fileName, name extension:fileExtension} to itemRef
			set end of theFolders to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
		end repeat
	end tell
	if theFolders is {} then return input
	
	set homePOSIX to quoted form of POSIX path of (path to home folder)
	set TID to text item delimiters
	repeat with aFolder in theFolders
		set text item delimiters to space
		set foldersToCreate to text items of aFolder
		set text item delimiters to ","
		set foldersToCreate to foldersToCreate as text
		do shell script "/bin/mkdir -p " & homePOSIX & quoted form of aFolder & "/{" & foldersToCreate & "}"
	end repeat
	set text item delimiters to TID
	
	return input
end run