Create folder structure based on filenames, move files inside

Thanks for the feedback.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mercredi 2 aout 2017 10:14:32

I don’t suppose someone could wrap this up into a droplet, so I can just drag the folder full of files onto it?

Save as an application.

on open dropped_items
	-- Examine each of the one or more dropped items in turn.
	repeat with dropped_folder in dropped_items
		-- Is this dropped item actually a folder?
		tell application "Finder" to set is_Folder to (class of item (dropped_folder as text) is folder)
		
		-- Act only if it is.
		if (is_Folder) then
			tell application "Finder"
				-- Get the folder's items and, separately, their names.
				set original_items to every item of dropped_folder
				set item_names to name of every item of dropped_folder
			end tell
			
			-- Derive quoted and unquoted forms of the names, without extensions, to use for the folders.
			set shot_folder_names to {}
			set quoted_shot_folder_names to {}
			set astid to AppleScript's text item delimiters
			repeat with this_name in item_names
				-- If a name has an extension, lose the extension. Otherwise assume it's a folder name containing "_bg_plt_v" and lose everything from that point.
				if (this_name contains ".") then
					set AppleScript's text item delimiters to "."
				else
					set AppleScript's text item delimiters to "_bg_plt_v"
				end if
				set end of shot_folder_names to text 1 thru text item -2 of this_name
				set end of quoted_shot_folder_names to quoted form of result
			end repeat
			
			--- Put together a path formula representing all the required "shot folder" hierarchies.
			set item_count to (count quoted_shot_folder_names)
			if (item_count > 1) then
				-- More than one shot folder name. Get a text containing them all, comma-delimited and in braces.
				set AppleScript's text item delimiters to ","
				set shot_folder_name_group to "{" & quoted_shot_folder_names & "}"
			else if (item_count is 1) then
				-- Only one shot folder name.
				set shot_folder_name_group to item 1 of quoted_shot_folder_names
			else
				set AppleScript's text item delimiters to astid
				error "The selected folder is either empty or not a folder!"
			end if
			set AppleScript's text item delimiters to astid
			set hierarchy_formula to quoted form of POSIX path of dropped_folder & shot_folder_name_group & "/{2D/{ae,mocha,nuke,ps},3D/{cache,data,scenes,textures},plates/{etc,plate,proxy},renders/{comp,graphics,ibtw,lightning,mattes,playblasts,precomp,quicktimes,temp}}"
			-- Use it to create the shot folder hierarchies.
			do shell script "mkdir -p " & hierarchy_formula
			
			-- Move stuff into each shot folder hierarchy in turn.
			set dropped_folder_path to dropped_folder as text
			repeat with i from 1 to (count shot_folder_names)
				set this_shot_folder_name to item i of shot_folder_names
				set shot_folder_path to dropped_folder_path & this_shot_folder_name
				-- Create a text file in the "3D" folder.
				close access (open for access file (shot_folder_path & ":3D:workspace.mel"))
				-- And one each in the "2D:ae" and "2D:ps" folders.
				set text_file_name to this_shot_folder_name & "_v001.txt"
				close access (open for access file (shot_folder_path & ":2D:ae:" & text_file_name))
				close access (open for access file (shot_folder_path & ":2D:ps:" & text_file_name))
				-- Move the original item into the "plates:plate" folder.
				tell application "Finder" to move (item i of original_items) to folder (shot_folder_path & ":plates:plate") -- replacing yes
			end repeat
		end if
	end repeat
end open

Hey guys,

So Everything is absolutely perfect!

I’ve been using the script successfully for a little while now with no complaints.

I did find one operational thing that I didn’t consider, and wondered if there’s an easy way to change one small thing.

I’m using another script inside AE that references the specific folder structure that you’ve built for me. But I made an error when I asked for the name of the text file inside the 2d/ps folder and 2d/ae folder. In other words, your work is perfect… I’m the idiot that didn’t ask for the right thing!!

The text file needs to be named with “_main_comp” in it’s title.

So current results create the following: gon102_006_005_v001.txt

but it needs to add “_main_comp” so it should read gon102_006_005_main_cmp_v001.txt

Would it be possible to alter the existing script to include this?

I tried figuring out how to alter it in the existing script, but that part’s not in plain english… and is sadly way outside my level of comprehension.

I’d appreciate any help you guys could offer!

Thanks!