Tuesday, January 21, 2020

#1 2020-01-05 07:18:22 pm

Karen B
Member
Registered: 2020-01-05
Posts: 6

Hi, I'm new. Could use help with simple script to move files!

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


Filed under: Move, Parent, container, .mbox

Offline

 

#2 2020-01-05 09:10:28 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 298

Re: Hi, I'm new. Could use help with simple script to move files!

Karen. Welcome to the forum.

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:

Applescript:

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:

Applescript:

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.

Last edited by peavine (2020-01-05 09:51:02 pm)


2018 Mac mini - macOS Catalina

Offline

 

#3 2020-01-06 01:59:01 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 876

Re: Hi, I'm new. Could use help with simple script to move files!

Hi, Karen B.

1) 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.

2) 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.

3) 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.

Last edited by KniazidisR (2020-01-06 02:13:39 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4, Ram 4 GB
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#4 2020-01-06 04:45:29 am

Karen B
Member
Registered: 2020-01-05
Posts: 6

Re: Hi, I'm new. Could use help with simple script to move files!

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.

Last edited by Karen B (2020-01-06 05:06:27 am)


Filed under: entourage, Outlook, mbox, PST

Offline

 

#5 2020-01-06 07:52:41 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 298

Re: Hi, I'm new. Could use help with simple script to move files!

Karen B wrote:

Thank you peavine for pointing me in the right direction. I did find, however, that a repeat function was needed to move multiple files.


Karen. Just as a point of information, the move command will move multiple files at one time. By way of reference:

The move command moves one or more items to the specified location....
move : items - The items to copy. To copy more than one item, use a list of items.


https://help.apple.com/applescript/mac/ … pscrpt1146

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.

Last edited by peavine (2020-01-06 09:54:16 am)


2018 Mac mini - macOS Catalina

Offline

 

#6 2020-01-06 08:03:20 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11641
Website

Re: Hi, I'm new. Could use help with simple script to move files!

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

Applescript:

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

Last edited by StefanK (2020-01-06 08:04:06 am)


regards

Stefan

Offline

 

#7 2020-01-06 10:01:55 am

Karen B
Member
Registered: 2020-01-05
Posts: 6

Re: Hi, I'm new. Could use help with simple script to move files!

Peavine,

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.

Offline

 

#8 2020-01-06 11:06:25 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 298

Re: Hi, I'm new. Could use help with simple script to move files!

Karen B wrote:

...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.



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.

Applescript:

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.

Last edited by peavine (2020-01-06 02:32:14 pm)


2018 Mac mini - macOS Catalina

Offline

 

#9 2020-01-06 01:30:03 pm

Karen B
Member
Registered: 2020-01-05
Posts: 6

Re: Hi, I'm new. Could use help with simple script to move files!

Peavine,

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!

Offline

 

#10 2020-01-07 12:21:53 am

Karen B
Member
Registered: 2020-01-05
Posts: 6

Re: Hi, I'm new. Could use help with simple script to move files!

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!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)