New Folder According to the Current Finder-Selected File, Then Move File Inside?

So let me start by declaring I'm a Keyboard Maestro newbie, and this is my first post. Hello all!

I'm trying to figure out how to take a currently selected file or folder and trigger a macro to ideally automate:

  1. Creating a new folder in the same location as the selected file or folder.
  2. Moving the selected file or folder inside the new folder.
  3. Renaming this new folder with the originally selected file or folder name, leaving me with an ability to edit the new folder name before committing it.

Perhaps this is best done with some advanced scripting beyond me but I was wondering, for learning purposes, if something like this was possible without dipping into script as ugly and inefficient as it may be. I'm willing to take what I can get here but thought both roads could lend me some insight to how to begin using this awesome software.

I was imagining my solution might include "For Each Path in Finder Selection", "New Folder", "Set Variable To Text", "Move (or Rename) a File" actions as well as some kind of Clipboard action including the use of %Variable%Path% but am too new to be able to wrap my head around the complexity I'm finding in going to do this.

Anyone care to weigh in and help the new guy out?
: )

Any help for this novice apparently looking to do something not so novice would be greatly appreciated! Thanks in advance!

Its not really too hard. You use the For Each Path in Finder Selection pseudo action to iterate through the selected files, and then you get the parent path and the file name. Make a new folder with a temporary name, move the existing file into the new folder, and the rename the folder. At the end I finish with a Reveal File which will ensure it is selected so you can just press return to edit the file name.

You might want to lose the extension by using Get Base Name instead of Get Name, although the problem with that is there might already be a file or folder with that name which could get messy fast.

Enclose File in Folder.kmmacros (3.7 KB)


Thank you so much for lending me your time Peter! That seems to work as I wanted!

I did notice that when the file selected happens to be an application file a new folder is not created and the selected application file ends up corrupted…?

One of the reasons I was after this function was for nesting older applications inside of folders with version info, cleaning up various application files for archiving purposes (I guess I could be called a digital hoarder).

Does this outcome make any sense to you?

Regardless, it seems to be working for other file types and folders.

Thanks again for lending me your time in accomplishing this!!

Check the Engine.log file to see why the folder is not being created. My guess would be that there are permission problems with the folder you are in which is stopping the folder creation.

Here’s a screen recording demonstrating this behavior that I made with a Console window opened monitoring the Engine.log the entire time.

Keep in mind that any other file, folder or .zip file performs as expected in this same location.

Any ideas on how to resolve this or at least find out what is occurring?

Heh. Of course it’s a broken application.

It’s a folder named “” which is not an application. The “.app” extension is there, it is just hidden by the Finder.

So the system thinks it is an application (it ends with .app), but its not an application (its a folder containing an application, not a folder containing the stuff that makes up an application.

As I said in my original message, “You might want to lose the extension”.

I read your original post about losing the extension but didn’t understand the implications until now. Experience is the best teacher.

Now I see.

I just changed Get File Name to Variable 'Name’ to Get Base Name to Variable 'Name’ and as you already know that fixed my issue.

Thanks for being so responsive and for all your patience with me Peter.

Back to it!

As I mentioned, that may fail in the case where you have selected “myfile.ext” and there is a folder named “myfile” already.

Glad to help.

Thanks for the macro!

I’d like it to ask the user to input the name of the folder. How would I do that? I’m just learning about variables, and I can’t seem to figure it out.

TIA for your help.

Hey @anamorph,

Have a look at this:

Holler if you have any problems.


Thanks, before I posted I did get the Prompt For User Input bit.

The problem I’m having is how to collect the input into a variable and then rename (or create) the folder with the input variable.

Suggestions? Thanks.

That's shown in Peters macro above.

When you do user-input you have to input to a variable.

When you use that variable in a TEXT-FIELD then you have to use the tokenized representation of it:

%Variable%Variable Name%

Here's a very simple example:

Generic-Test 01.kmmacros (2.3 KB)


Thanks for the help.

After a lot of mistakes and hair-pulling, I was finally able to figure out my syntax mistakes and get it to work.

The only thing that I can’t figure out is the “Reveal” action. In Peter’s script it didn’t do anything. When I specify the file name, like %Variable%NewFolderPath%/%FileName% it opens a new finder window showing the file. I could’ve sworn there was a way in list view for it to just expose the file by turning the disclosure triangle.

Am I remembering it wrong?

Thanks again.

Afraid so.

Reveal <item> is always going to open a new window, UNLESS the front window already contains the item to be revealed. (Items in sub-folders don’t count.)


Ok, thanks.

For PathFinder users, there is a macro in this thread.

I'd like to do something very similar but with multiple selected files.

Let's say I have three files called File 1, File 2 and File 3. It would be handy to be able to select them and trigger a macro which automatically puts them in a new folder in the same location. The macro would then rename the folder with the name of the first of the selected files (in this case "File 1"), and leave the renaming field open. It would then be very quick and easy to quickly rename it to something useful like "Files" and hit enter.

This is a quick and dirty way of achieving most of that within the Finder, but I'm not sure how to copy the name of File 1 for pasting. Any help appreciated as this is a common thing for me and would save a fair amount of time.

Hey Neil,

That's not super easy to do, although it's possible with either native Keyboard Maestro actions or AppleScript.

I'll go the AppleScript route, although I'll let you build the KM macro with an Execute an AppleScript action.

Keep in mind that the first item in the Finder that you pick will be the one used to rename the folder.


# Auth: Christopher Stone
# dCre: 2021/11/13 16:50
# dMod: 2021/11/13 16:50 
# Appl: Finder
# Task: Move Selected Items to a Folder Named for Item 1 of the Selection.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Move, @Selected, @Items

tell application "Finder"
   set currentFolder to get insertion location
   set finderSelectionList to selection as alias list
   if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
   set nameTemplateFile to item 1 of finderSelectionList
   set newFolderName to name of nameTemplateFile
   set nameExt to name extension of nameTemplateFile
   if nameExt ≠ "" then
      set newFolderName to text 1 thru -((length of nameExt) + 2) of newFolderName
   end if
   set tempFolderName to newFolderName & ".dupe!"
   set newFolder to make new folder at currentFolder with properties {name:tempFolderName}
   move finderSelectionList to newFolder
   set name of newFolder to newFolderName
   select folder newFolderName of currentFolder
end tell


Chris, you legend! Works first time! I added a 0.5s pause and a Return keypress which reopens the renaming field. Perfect!

Blimey you're quick!! Thank you!!!

1 Like