Select exactly two files from top of list

I have a need to put pairs of sequentially-named files into sub-folders. (Scanning large pages in two parts and using a Photoshop script to batch merge contents of folders.) It would be helpful to not have to manually create all of the subfolders from a list of hundreds of files.


Is there a way in Automator to (1) select only the top two files in a folder, (2) create a new folder with that selection, and (3) repeat 1 and 2 until all files are in subfolders? The name of the new folders is not important. Thanks in advance for any direction.

Mac mini 2018 or later

Posted on Feb 15, 2021 8:47 AM

Reply
3 replies

Feb 15, 2021 12:03 PM in response to village_green

This is pretty easy to do with AppleScript.


Either paste this into a new Script Editor document, or use Automator's Run AppleScript action.


-- prompt the user for the folder to process
set theFolder to (choose folder with prompt "Please select a folder")
tell application "Finder"
	-- get a list of all the files in the folder
	-- by default the Finder will return alphabetically-sorted list
	set theFiles to every file of theFolder --whose visible is true
end tell

-- setup some variables
set subFolderName to 0
set subFolder to missing value

-- iterate through the files
repeat with i from 1 to (count theFiles)
	tell application "Finder"
		-- do we have an odd-numbered file?
		if i mod 2 = 1 then
			-- if yes, increment the subfolder name
			set subFolderName to subFolderName + 1
			-- and create a new folder
			set subFolder to make new folder at theFolder with properties {name:subFolderName}
		end if
		-- move the current item into the current subfolder
		move (item i of theFiles) to subFolder
	end tell
end repeat


If you're only using AppleScript you might be able to simplify it a little bit by using Automator to select the folder and pass that into the script, but I generally prefer this more direct approach.


You might be able to craft this all in Automator workflows, but looping in Automator is FUBAR and I wouldn't bother when there are far easier paths.

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.

Select exactly two files from top of list

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