I need help with what should be a simple script task. I want to move files in a folder with subfolders (all of them .mbox files) up in the folder hierarchy two levels regardless of how deep in the hierarchy they are. In other words they need to go into the parent of their parent or container or their container. Preferably the script would also delete the parent.
I should say that I have just upgraded to Catalina and suspect this is why I’m having problems. The script keeps saying it can’t get the parent or the container of the folder (I’ve tried using both terms.)
I have seen posts where other people are having problems with AppleScript in Catalina.
Here is what I have so far, sort of based on another script was was working way back in Lion, that also doesn’t work under Catalina:
tell application “Finder”
set selectedItem to selection
set folderContainer to container of container of selecteditem
move selecteditem to foldercontainer
end tell
Result:
error “Can’t get container of {document file "Mutual Bank.mbox" of folder "Mutual Bank" of folder "Banking" of folder "Sorted" of folder "Desktop" of folder "Karen2" of folder "Users" of startup disk of application "Finder"}.” number -1728 from «class ctnr» of {«class docf» “Mutual Bank.mbox” of «class cfol» “Mutual Bank” of «class cfol» “Banking” of «class cfol» “Sorted” of «class cfol» “Desktop” of «class cfol» “Karen2” of «class cfol» “Users” of «class sdsk»}
Obviously this would only move one file, IF it worked, then I would have to add a command to delete the parent and also a repeat loop.
Model: Mac Mini late 2012
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14
The selection property returns a list even if only one item is selected, and so you have to specify an item in the list, as in the following:
tell application "Finder"
set selectedItem to selection -- a list of selected files and folders
set selectedItem to item 1 of selectedItem -- the first selected item
set folderContainer to container of container of selectedItem
move selectedItem to folderContainer
end tell
BTW, to move up two levels, I think you have to use container of container of container (which seems to work and is included in the following script) or consider a different approach.
The move command will work with multiple items, making a repeat loop unnecessary. So, to move all selected items:
tell application "Finder"
set selectedItem to selection -- a list of selected files and folders
if selectedItem = {} then return
set folderContainer to container of container of container of (item 1 of selectedItem)
try
move selectedItem to folderContainer
on error errorMessage
display dialog "The Finder reported the following error: " & linefeed & linefeed & errorMessage buttons {"OK"} cancel button 1 default button 1
end try
end tell
I’m not sure what folder you want to delete and have not included that, and I’ve included some basic error correction. The issue with Catalina and the move command AFAIK is with System Events and not Finder.
Even if you selected single folder, on new systems the selection property of Finder returns it as list: {someFolder}. So, to get it you should retrieve item 1 of the list.
You don’t need delete the moved folder, because after moving it 2 levels up, it already is deleted on its old location. So, the script of Peavine is what you need. Only, you need the container of container instead of container of container of container.
Do not take the example of impolite beginners. In this case, it would be appropriate to thank Peavine for the help, and tell users if the proposed solution works for you too.
Maybe, you want instead move the contents of the chosen folder, and not the folder itself?
Feel free to tell us.
Thank you peavine for pointing me in the right direction. I did find, however, that a repeat function was needed to move multiple files.
KniazidisR, I think I might not have been clear about what I was moving. I am moving contents of folders, not the folders themselves. This will leave SOME empty folders, depending on if they are at the lowest level in the hierarchy. I realized after my first post that I need to keep the folders higher in the hierarchy, though.
Here is what has worked for moving the files:
tell application “Finder”
set myList to selection – a list of selected files, mbox files in my case
repeat with selectedItem in myList
set folderContainer to container of container of selectedItem
move selectedItem to folderContainer
end repeat
end tell
Now what I need is an add-on to the above script to remove empty folders after the above has ran.
This may be TMI, but in case anyone wonders why I am doing this, I recently upgraded my email from Mac Entourage to Outlook 2016 on Catalina OS. Exporting Entourage archive results in every mbox inside the .rge package being inside a folder with a name identical to the mbox, so that when the mboxes are imported into Outlook, after being converted to PST by a 3rd party app, the emails I had sorted into subfolders end up in redundant subfolders. It’s not the end of the world but it’s irritating and seems to affect Outlook’s ability to find some emails. I figured that if I could edit the hierarchy of the mboxes before the PST conversion, I might be able to re-import the emails into Outlook without the redundant subfolders.
I ran several tests with my second script in Post #2 and verified that this is the case. Also, the AppleScript dictionary confirms this.
There’s certainly nothing wrong with using a repeat loop to move files, and it does offer some flexibility in doing other stuff (like checking for existing files), but it’s normally faster to move multiple files with a single move command.
To remove a folder if it’s empty save the reference to the enclosing folder of the selected item.
After each move operation check if the folder is empty. If so, delete it
tell application "Finder"
set myList to selection -- a list of selected files, mbox files in my case
repeat with selectedItem in myList
set enclosingFolder to container of selectedItem
set folderContainer to container of container of selectedItem
move selectedItem to folderContainer
if (count items of enclosingFolder) is 0 then delete enclosingFolder
end repeat
end tell
Okay, thanks, I must have done something wrong when I tried that.
StefanK,
Since I was already successful with moving the mbox files, I put the following in a separate script, script based on your suggestion:
tell application “Finder”
set myList to selection – a list of folders that may or may not be empty
repeat with selectedFolder in myList
if (count items of selectedFolder) is 0 then delete selectedFolder
end repeat
end tell
This worked for me. Now the only problem is there are some mboxes with names identical folders on the same level (except for the .mbox extension), so I’m pretty sure there will be a problem when the mbox converter converts the mboxes to folders (that’s what it does). Either the mboxes or the folders will probably be overwritten or the conversion may just fail, so I probably will need to prepend or append something either to every folder name or every mbox name. Maybe just a space at the end. Ugh! I’m sure this can be done with an Applescript, but from this point I may just use Automator to finish this last step. This whole process is hopefully a once-in-a-lifetime thing that will need to be done.
The following script appends " Backup" to every selected folder. It may break if you use it to rename both a parent and child folder in one running of the script, because the path to a child folder may have changed due to the renaming of the parent folder. This is fixable and can be avoided for one-time use by selecting folders in the Finder from child to parent.
tell application "Finder"
set myList to selection -- a list of folders that may or may not be empty
repeat with selectedFolder in myList
set newName to (name of selectedFolder) & " Backup"
set name of selectedFolder to newName
end repeat
end tell
This probably goes without saying, but I would certainly keep a rock-sold backup of everything before doing all of this.
Thanks, your script worked great. Now I just need to see what the mbox converter does with the result and what happens when I import the conversion into Outlook. I should probably figure out where the Outlook database is stored so I can back it up before I re-import my emails in case something goes really wonky!
Okay, guys, I’m ecstatic to report that, with your help, the mbox conversion and re-import into Outlook went smoothly and my email is now all straightened out with no redundant subfolders! Thanks so much!