Create folder from filename?

After removing all the �’s try this


on run
	set chosen_folders to choose folder with multiple selections allowed
	open chosen_folders
end run

on open theseItems
	repeat with oneFolder in theseItems
		set {folder:Fo, package folder:Pa} to info for oneFolder
		if Fo and not Pa then
			tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "."}
			tell application "Finder"
				try
					set file_list to files of oneFolder
				on error
					set file_list to {}
				end try
				repeat with this_file in file_list
					set folder_name to name of this_file
					if folder_name contains "." then set folder_name to ((text items 1 thru -2 of folder_name) as string)
					set new_folder to ((oneFolder as string) & folder_name & ":")
					try
						get new_folder as alias
					on error
						make new folder at oneFolder with properties {name:folder_name}
					end try
					move this_file to folder new_folder
				end repeat
			end tell
			set my text item delimiters to old_delim
		end if
	end repeat
end open

Hi Stefan,

thanks for the reply!

Well … that’s actually a much better version than I already had. Drag and drop is far simpler. But it’s not what I was really wanting. :frowning:

I was meaning drag and drop of ‘files’ not folders (although … both would be perfect! :wink: )

How do I get it to work with dragged files?

Grant

I haven’t tested this, you can drop files or folders or choose folders by double-click it


on run
	set chosen_folders to choose folder with multiple selections allowed
	open chosen_folders
end run

on open theseItems
	repeat with oneItem in theseItems
		set {folder:Fo, package folder:Pa} to info for oneItem
		if Fo and not Pa then
			tell application "Finder" to set file_list to files of oneItem
			repeat with oneFile in file_list
				processFile(oneFile as alias, oneItem)
			end repeat
		else
			tell application "Finder" to set parentFolder to container of oneItem
			processFile(oneItem, parentFolder as alias)
		end if
	end repeat
end open

on processFile(this_file, oneFolder)
	tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "."}
	tell application "Finder"
		set folder_name to name of this_file
		if folder_name contains "." then set folder_name to ((text items 1 thru -2 of folder_name) as string)
		set new_folder to ((oneFolder as string) & folder_name & ":")
		try
			get new_folder as alias
		on error
			make new folder at oneFolder with properties {name:folder_name}
		end try
		move this_file to folder new_folder
	end tell
	set my text item delimiters to old_delim
end processFile

Stefan,

thanks soooooooo much. That works perfectly! It will save me even more time than Jon’s original script.

:slight_smile:

Grant

This script is such a timesaver for me … but here I am trying to tweek it even more … and failing. :frowning: I’m running it on a Tiger 10.4.11 machine (t’s a long story) and I’m trying to have the script add a Finder Label (colour) to the newly created Folders.

Am I correct in thinking that I cannot do this with the Applescript under Tiger?

Grant

no problem, also in Tiger.
change the two lines in the error branch


.
on error
	set newFolder to make new folder at oneFolder with properties {name:folder_name}
	set label index of newFolder to 2 -- red
end try
.

Another big thank you Stefan.

When I opened the Finder’s dictionary in Script Editor, it says ‘NOT AVAILABLE YET’ for Labels … so I guessed it wouldn’t work. Either way though, I’d never have managed to fluke the correct code. Ho hum. If ever you need advice about photography … just let me know. :slight_smile: :slight_smile:

the class label is not implemented, but the property label index (of item, folder, file) is available