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:
--------------------------------------------------------
# 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.
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?
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.
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
--------------------------------------------------------
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 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.
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.)
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.
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.
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.
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.
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.