Finder – Move Selected Items to Parent Folder Macro

I often find that I want to move the selected file(s)/folder(s) to their parent directory.

I wasn't sure how to best do this, so I did what I always do, I wrote a shell script that checks to make sure the same file(s)/folder(s) don't exist in the parent directory, and then moves them.

This is designed to be used in a group that is only active in Finder.app:


Download: Finder - Move to Parent Folder.kmmacros (38 KB)
image

1 Like

Hey @tjluoma,

That looks serviceable to me.

Here's an AppleScript for reference.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/01/15 11:12
# dMod: 2021/01/15 11:12 
# Appl: Finder
# Task: Move the Selected Files to Parent-Parent Folder.
#     : Throw an error if there are any collisions and quit.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder
--------------------------------------------------------

try
   
   tell application "Finder"
      set finderSelectionList to selection as alias list
      if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
      set parentFolder to parent of parent of item 1 of finderSelectionList
      
      repeat with i in finderSelectionList
         set itemName to name of i
         tell parentFolder
            if exists of its item itemName then
               error "An item named “" & itemName & "” already exists!"
            end if
         end tell
      end repeat
      
      move finderSelectionList to parentFolder
      
   end tell
   
on error errMsg number errNum
   set errMsg to errMsg & return & return & "Num: " & errNum
   if errNum ≠ -128 then
      try
         beep
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog errMsg with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to errMsg
      end try
   end if
end try

--------------------------------------------------------

NOTE – that I'm testing all items before they're moved and halting the macro if a collision is detected.

If I was going to do this in the shell I'd scarf up all the relevant file paths and pass them en-mass to the shell script, so there'd only be one Execute a Shell Script action.

-Chris

Hi,

I’ve been looking for the way to move folder contents to the parent directory (like ungrouping a folder) in this forum and finally got here. But there is one thing I’d like to know.

I want to select "the parent folder" in Finder and take the files out of it.

I thought I should change the line below somehow for my purpose and googled a lot, but couldn’t figure out how to do it as I’m not familiar with AppleScript (also have no idea about Shell Script).

set finderSelectionList to selection as alias list

Would you mind telling me how to change your script above?

Thanks

The script above is carefully written to handle cases where files by the same name exist in both places. In your situation, do you need to handle that? Or is it safe to assume there are no duplicate file names in your situation? There may be a MUCH simpler solution if you are confident of no duplicates.

Hey @ewos,

More like a rewrite...  :sunglasses:

This is as close to as basic as it gets, although I could have been a bit more terse.

Basic error checking is included.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/12/16 10:48
# dMod: 2021/12/16 10:48 
# Appl: Finder
# Task: Move the Contents of a Folder to its Parent Folder.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Move, @Contents, @Folder, @Parent
--------------------------------------------------------

try
   
   tell application "Finder"
      
      set finderSelectionList to selection as alias list
      
      if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
      set theFolder to item 1 of finderSelectionList
      
      if kind of theFolder = "Folder" then
         set folderContents to items of theFolder as alias list
         set folderParent to parent of theFolder as alias
         move folderContents to folderParent
      end if
      
   end tell
   
on error errMsg number errNum
   set errMsg to errMsg & linefeed & linefeed & "Num: " & errNum
   if errNum ≠ -128 then
      try
         tell application (path to frontmost application as text) ¬
            to set ddButton to button returned of ¬
            (display dialog errMsg with title ¬
               "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to errMsg
      end try
   end if
end try

--------------------------------------------------------

Hi @Sleepy,

Thank you for your quick response!
I read the script and understood that it includes the error handling when having the same file name. Which is why I thought this was great for my purpose.

Thanks

If you're happy, I'm happy.

1 Like

Hi @ccstone,

Thanks for your response and the script!!!!!
I roughtly read the script and understood this one has the “simple error handling” as you wrote.
I’ll give it a try in KM, and try to read the script for my understanding.

Thank you!

Hi @ccstone,

Thanks for your script again, but I couldn’t make it work and have been wondering why.

I put the script into “Execute Applescript” action block in a macro which has no action else, set the hot key, selected a folder which has a few files and folders inside in Finder, and executed the macro, but nothing happened.

I tried to do the same with selecting no file in Finder, and got “No files were selected in the Finder!” message. So, I think the macro, the script, the hot key setting is working.

Also, your original applescript worked fine when I had tried it before.

I read the script and understood the line

move folderContents to folderParent

make the files move, and “folderContents” and “folderParent” is defined just before, but can’t imagine why this doesn’t work.

Where did I go wrong…? (Sorry I couldn’t reply sooner.)

Don't know...

Run it from Apple's Script Editor.app, and see if that works.

Hi @ccstone,

I figured it out. It’s because of the language setting of macOS (which is Japanese).
Firstly, I tried Script Editor.app and failed again.
After a struggle, I realized that the script works fine when I delete the if clause below.

if kind of theFolder = "Folder" then
    end if

And after some more struggle, I found it works when I write “Folder” in my language.

CP 1

These AppleScript manners are rather confusing when not being familiar with. Normally, we don’t use any Japanese when programming.

Now the script works perfectly.

Unfortunately, I found that after I had made up a macro that does the same thing after a huge struggle. But I learned a lot anyway.

Thanks a lot!

1 Like

On an off chance, does anyone have guidance on why this Macro triggers a security alert from Bitdefender. I've never run into this before while running KM Macros.

Sounds like KM isn't an "allowed application" as far as Bitdefender's Safe Files is concerned. Try the instructions at https://www.bitdefender.com/consumer/support/answer/1448/

Although it might be complaining about AppleScript -- not sure how you'd add that!

Thanks for the tip! It took several tries using the Bitdefender instructions but it worked.

Is there a way to rewrite this Script to move the selection to the top of the directory path for a particular finder window? For example if the folder structure is:
Download>
...........Enclosing Folder>
..................Specific Folder>
........................Working Folder Folder>
..................................."File"

The File would be dropped off at the same level as the Download Folder.?

"Top of the directory path" is always going to be / -- but I'm guessing you don't want to go that far!

Are you working in "List" view and want to move the item to the directory the Finder window is open at? Or any view but always in your home directory and want to move to the top folder inside home (Desktop, Downloads, etc)?

I have attached a screenshot that might make more sense.

I'd like to move the file "READ.nfo" to the "Downloads" folder. The "Downloads" folder being the top of directory opened in the finder directory listing.

SCR-20230408-lk7

Sure.

Since the selection is within a Finder window you can get its target and move the selection to that target.

** Note – this won't work with Search windows...

The macro has minimal error-checking but will display an error message if there is one.


Download: Move Selected Items in the Front Finder Window Hierarchy to the top Level of the Window v1.00.kmmacros (7.2 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

2 Likes

Wow, it works great and I learned about calling the Finder window name. My attempts were to try and count the directory levels and move the file accordingly. You solution was way more elegant than my attempt.

Thanks once again.

1 Like

I would like to use Chris' script, but instead of the error message he suggests, is it possible to still be presented with this kind of warning, so I can still make a decision one by one?
image

If this is not possible, I would like the item(s) to still be moved, but rename them if there's a duplicate in the parent folder.
So a file with the name "document.txt" would become "document (copy).txt", for example.