How to select "x" number of items in Finder list?

I'm trying to select only the first "x" (128, in this case) amount of Finder items in a given folder, so that I can then move them to their own folder. (In context, I'm trying to break up a folder containing thousands of files into groups of 128.)

Haven't had any luck researching a solution, maybe someone here could help me out? I really appreciate it!

Jonathan

1 Like

I'd suggest a slightly different approach.

First, I'd create two variables, one to be the file counter and the other to be the folder name suffix (A-Z or some such whose sequence covers however many groups of 128 files in your thousands of files).

Then I'd open the selected folder in the Finder (the macro would expect a folder to be selected), get a list of file names in that folder and sort them in the order preferred.

Next, I'd step through that list of file names one at a time, moving them to the currently active destination folder and incrementing the counter. When I hit the 129th file, I'd change the name of the destination folder to the next in sequence.

And continue until there are no more file names in the list.

To run the macro, you should only have to select the master folder in Finder and trigger the macro.

You can write this in Keyboard Maestro actions but given the volume of files you're processing, I'd just do it with a shell script that got the folder name from a Keyboard Maestro variable.

So, to sum up, rather than try to select a group of files and move them somewhere, you would select a folder and process its contents one at a time. Make sense?

Hey mrpasini, thanks for the response!

Ah interesting - it sounds like a slower process (having to step through and move each 128 files individually?) - and I was hoping to come up with a faster solution with less steps (select all 1-128 files + move them to new folder). Or maybe I'm not understanding it clearly, perhaps this is the best way to handle this?... I am still a novice at setting up these macros, still trying to wrap my head around some of the concepts :stuck_out_tongue:

Do you mind sharing a screenshot/macro example of your solution for me? So I could make sure I'm doing this right - especially inexperienced with the shell script stuff. I'd really appreciate it!

To reiterate what I'm attempting to do in more detail:

  • Within current Finder location, select the first 1-128 files
  • (at some point) Create a new folder (according to some naming sequence) next to the parent folder, and place the 128 files within.
  • I could then easily make this repeatable, or be able to process it one "batch" at a time.

Thanks again!

It's actually as fast a method as any, the limiting factor being your disk's read/write speeds.

I don't myself have a macro that does quite what you want and there aren't enough details like folder and filename patterns in your post to write anything specifically for you. But fortunately you'll probably find a good starting point looking at other macros submitted here that move files to a new folder.

Move Selected Finder Items to Choose Folder , for example, might be a good starting point for you.

Or you might look at a few shell scripts that make it pretty easy to move large numbers of files around.

1 Like

Ok sounds good, thanks so much - I'll check out the links you shared...

One part that I might need help with - how would accomplish this?:
"When I hit the 129th file, I'd change the name of the destination folder to the next in sequence."
How do I set it up so that the macro knows to do this at the 129th file, rather than processing all the finder items?

This is easy and very fast using AppleScript.
But we need more information:

  1. What is naming convention of your destination folders?
  2. What do you want to do if the file or folder already exists?

==UPDATED==: 2019-11-23 20:32 GMT-6

Here's an AppleScript that should do the job.
You will need to CHANGE these statements to suit your workflow:

--~~~~~~~~~~ USER SETTINGS ~~~~~~~~~~~~~~~~~~
-- Each Destination Folder = destFolderPrefix & "1"
-- Each folder will be created if necessary

set destFolderPrefix to "/Users/Shared/Dropbox/Public/Images Sub "
set numFilesPerFolder to 128
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here's the complete script:

AppleScript to Move Files in Batches to New Folders

property ptyScriptName : "Move N Files from Selected Folder to New Folder via System Events"
property ptyScriptVer : "3.0" --  REWRITE Using System Events
property ptyScriptDate : "2019-11-23"
property ptyScriptAuthor : "JMichaelTX"

### Required:  macOS Sierra+    Tested In: macOS Mojave 10.14.5

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

--~~~~~~~~~~ USER SETTINGS ~~~~~~~~~~~~~~~~~~
-- Each Destination Folder = destFolderPrefix & "1"
-- Each folder will be created if necessary

set destFolderPrefix to "/Users/Shared/Dropbox/Public/Images Sub "
set numFilesPerFolder to 128
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tell application "Finder"
  set fItemList to selection
  set folderSelected to item 1 of fItemList
  if (folderSelected's class is not folder) then
    error "Selected Finder Items is NOT a Folder: " & (name of folderSelected)
  end if
end tell

set folderSelected to folderSelected as alias

--- IT IS MUCH FASTER TO USE System Events than Finder TO MOVE FILES ---

tell application "System Events"
  
  set fileList to (files of folderSelected)
  set numSourceFiles to count of fileList
  --  log ("numSourceFiles: " & numSourceFiles)
  set iDestFolder to 0
  
  repeat with iFile from 1 to (count of fileList) by numFilesPerFolder
    
    set lastFile to iFile + numFilesPerFolder - 1
    if (lastFile > numSourceFiles) then set lastFile to -1
    --    log (iFile & lastFile)
    set fileSubList to items iFile thru lastFile of fileList
    
    set iDestFolder to iDestFolder + 1
    set destFolder to my createFolder(destFolderPrefix & iDestFolder)
    --    make new folder in folder folderPath with properties {name:"test folder"}
    log destFolder
    
    move fileSubList to destFolder
    
  end repeat
  
end tell -- System Events


--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on createFolder(pPosixPath) --  @Folder @File @ASObjC
  (*  VER: 1.1    2019-11-23
---------------------------------------------------------------------------------
  PURPOSE:  Creates Folder, and any needed sub-folders, in Path
                (Similar to `mkdir -p`)
  PARAMETERS:
    • pPosixPath    | text  | POSIX Path to end folder
  RETURNS:   folder alias if successfull
  AUTHOR:  Chris Stone (@ccstone)
              Refactored by JMichaelTX
  REF:
    1. ASObjC – Write File & Create Directory Handlers
          https://lists.apple.com/archives/applescript-users/2016/Jun/msg00088.html
  REQUIRES:
    1. macOS 10.10.5+
    2. use framework "Foundation"
—————————————————————————————————————————————————————————————————————————————————
*)
  
  set nsPosixPath to (current application's NSString's stringWithString:pPosixPath)'s stringByExpandingTildeInPath
  
  set {theResult, theError} to current application's NSFileManager's defaultManager()'s createDirectoryAtPath:nsPosixPath withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
  
  if not (theResult as boolean) then
    set errorMsg to theError's localizedDescription() as text
    error errorMsg
  end if
  
  set folderAlias to POSIX file pPosixPath as alias
  return folderAlias
  
end createFolder
--~~~~~~~~~~~~~~~ END OF handler createFolder ~~~~~~~~~~~~~~~~~~~~~~~~~

You need to make these changes:

  1. The statements in the "USER SETTINGS" block
    • set destFolderPrefix,
    • set numFilesPerFolder
  2. OR ADD getvariable calls to get this data from KM Variables

HOW TO USE

  1. Select the source folder in the Finder.
  2. Open script in Script Editor, or better, Script Debugger 7 and run.
  3. OR, put in KM Execute an AppleScript action.

If you need more help in fully implementing this script, just let us know.

2 Likes

Oh man, that's perfect - thank you guys so much, really appreciate all the detailed help!

Jonathan