Apple’s Worldwide Developers Conference to kick off June 10 at 10 a.m. PDT with Keynote address

The Keynote will be available to stream on apple.com, the Apple Developer app, the Apple TV app, and the Apple YouTube channel. On-demand playback will be available after the conclusion of the stream.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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
Question marked as Best reply

Posted on Feb 15, 2021 12:03 PM

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.

3 replies
Question marked as Best reply

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.

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 ID.