Automator program creating folder and moving files

Hello;

I haven't really much knowledge of the automator.. So I would like to ask if there is a simple way to:

create a automator program that lets me

  • select files
  • Creates a folder with the name of the first file and moves the file in it.
  • Then proceeds with the following files, creating folder with the name of second file and moves the file in it and so on

Folder name without file extension..

Is there a simple way to automate this?

Thanks in advance for any help;

Mac mini, macOS 10.14

Posted on Feb 17, 2022 1:17 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 17, 2022 11:19 AM

You can simplify your request:


There is zero relevance of 'first', 'second', etc. here. It is simply enough to say "select some files and put each of them in a directory named according to the file.


This is not something that Automator can do, per se. Automator is almost useless when it comes to looping and logic flows - it's very linear.


You can do this via AppleScript, and that AppleScript can be embedded in an Automator workflow, so you can get there, but it is not as simple as dragging Automator Actions into a sequence.


Here's a simple AppleScript that prompts for files and creates a series of folders, one per file.


Put this in an Automator 'Run AppleScript' action, or run it directly via Script Editor.app:


tell application "Finder"
	set theFilesToMove to (choose file with multiple selections allowed)
	repeat with eachFile in theFilesToMove
		set theFolder to container of eachFile
		set basename to my stripExtension(eachFile)
		set newFolder to make new folder at theFolder with properties {name:basename}
		move eachFile to newFolder
	end repeat
end tell

on stripExtension(theFile)
	-- there is a 'proper' way to do this via AppleScriptObjC
	-- but here's a simple pure AppleScript method
	tell application "Finder"
		set fn to name of theFile
		set ext to name extension of theFile
		if ext is not "" then
			set charsToStrip to 2 + (count ext)
			set fn to characters 1 through -charsToStrip of fn as text
		end if
	end tell
	return fn
end stripExtension


There are other approaches too, depending on how you want to trigger it. This script is based around the script prompting you to select the files, but you could also set it up via Services so you can Ctrl-click on files in the Finder and trigger the script. Your call.

2 replies
Question marked as Top-ranking reply

Feb 17, 2022 11:19 AM in response to gaetano132

You can simplify your request:


There is zero relevance of 'first', 'second', etc. here. It is simply enough to say "select some files and put each of them in a directory named according to the file.


This is not something that Automator can do, per se. Automator is almost useless when it comes to looping and logic flows - it's very linear.


You can do this via AppleScript, and that AppleScript can be embedded in an Automator workflow, so you can get there, but it is not as simple as dragging Automator Actions into a sequence.


Here's a simple AppleScript that prompts for files and creates a series of folders, one per file.


Put this in an Automator 'Run AppleScript' action, or run it directly via Script Editor.app:


tell application "Finder"
	set theFilesToMove to (choose file with multiple selections allowed)
	repeat with eachFile in theFilesToMove
		set theFolder to container of eachFile
		set basename to my stripExtension(eachFile)
		set newFolder to make new folder at theFolder with properties {name:basename}
		move eachFile to newFolder
	end repeat
end tell

on stripExtension(theFile)
	-- there is a 'proper' way to do this via AppleScriptObjC
	-- but here's a simple pure AppleScript method
	tell application "Finder"
		set fn to name of theFile
		set ext to name extension of theFile
		if ext is not "" then
			set charsToStrip to 2 + (count ext)
			set fn to characters 1 through -charsToStrip of fn as text
		end if
	end tell
	return fn
end stripExtension


There are other approaches too, depending on how you want to trigger it. This script is based around the script prompting you to select the files, but you could also set it up via Services so you can Ctrl-click on files in the Finder and trigger the script. Your call.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator program creating folder and moving files

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.