6 Replies Latest reply: Dec 7, 2012 6:36 AM by Jacques Rioux
ludwip Level 1 Level 1 (0 points)

Hi Everyone,

 

I'm new to AppleScript as well as to Automator.

 

I have following issue.

 

Before starting I'd like to give an example the root of the main folder as well as subfolders

 

mainFolder/Subfolder1/Subfolder1.1/file1

mainFolder/Subfolder1/Subfolder1.1/file2

mainFolder/Subfolder1/Subfolder1.2/file1

 

mainFolder/Subfolder2/Subfolder2.1/file1

mainFolder/Subfolder2/Subfolder2.2/file1

mainFolder/Subfolder2/Subfolder2.2/file2

 

I need to move files from within Subfolders1.1, 1.2, to Subfolder1 , Subfolders2.1, 2.2  to Subfolder2 and than delete Subfolders1.1, 1.2 and Subfolders2.1, 2.2

I found number of AppleScripts as well as Automator workflow to move files from Subfolders to main directory but not to parent subfolder.

 

Is there any way to automate this task?

 

Thanks


OS X Mountain Lion (10.8)
  • 1. Re: How to move files from subfolders level up with AppleScript or Automator?
    Niel Level 10 Level 10 (242,135 points)

    Open subfolder1 and run a script such as:

     

    tell application "Finder"

    repeat with this_item in (get every item of entire contents of (folder of window 1))

    move this_item to parent of parent of this_item

    end repeat

    end tell

     

    sort the window by kind, and delete the folders.

     

    (72389)

  • 2. Re: How to move files from subfolders level up with AppleScript or Automator?
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

     

    If you use the Niel's script, replace every item by every file, otherwise the folders will be included in the list.

     

     

    Here's another way to do this :

     

    set x to choose folder
    tell application "System Events"
          repeat with p in (get path of folders of x)
                set this_Folder to p as alias
                move (files of folders of this_Folder whose visible is true) to this_Folder
                delete folders of this_Folder
          end repeat
    end tell
    
  • 3. Re: How to move files from subfolders level up with AppleScript or Automator?
    ludwip Level 1 Level 1 (0 points)

    Hi Niel,

     

    First of all thank you for your help.

     

    I was trying to run a script but following error message occur:

     

     

    "error "Finder got an error: Can’t get folder \"Subfolder1.1\" of folder \"Subfolder1\" of folder \"Main folder\" of folder \"Desktop\" of folder \"ludwip\" of folder \"Users\" of startup disk." number -1728 from folder "Subfolder1.1" of folder "Subfolder1" of folder "Subolder test" of folder "Desktop" of folder "Ludwik" of folder "Users" of startup disk

     

     

    Instead of moving contents of Subfolders 1.1 and 1.2 to Subfolder1 and delete them afterwards AppleScript moves entire Subfolders 1.1 and 1.2 into Subfolder1. All the contents remain in Subfolders 1.1 and 1.2.


    Also is there any chance for the script to repeat itself in every Subfolder1, Subfolder2, Subfolder3 etc. instead of me copying the script to each of the Subfolders?"

     

    Thanks again for your help,

  • 4. Re: How to move files from subfolders level up with AppleScript or Automator?
    ludwip Level 1 Level 1 (0 points)

    Hi Jacques Rioux,

     

    I've tried your script but it doesn't seem to work for me at all.

    Maybe I've done something wrong?

    Thanks

     

     

    Jacques Rioux wrote:

     

    Hi,

     

    If you use the Niel's script, replace every item by every file, otherwise the folders will be included in the list.

     

     

    Here's another way to do this :

     

    set x to choose folder
    tell application "System Events"
          repeat with p in (get path of folders of x)
                set this_Folder to p as alias
                move (files of folders of this_Folder whose visible is true) to this_Folder
                delete folders of this_Folder
          end repeat
    end tell
    
  • 5. Re: How to move files from subfolders level up with AppleScript or Automator?
    ludwip Level 1 Level 1 (0 points)

    Hi Jacques Rioux,

     

    I think I know what I've done wrong before.

    I've chosen subfolders instead of main folder.

    Script works fine for the first few folders and than following error comes up:

    error "System Events got an error: AppleEvent timed out." number -1712

     

    Any idea why?

    Thank

  • 6. Re: How to move files from subfolders level up with AppleScript or Automator?
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi ludwip,

     

     

    ludwip wrote:

     

    Script works fine for the first few folders and than following error comes up:

    error "System Events got an error: AppleEvent timed out." number -1712

     

     

    If you get this error, there must be a lot of files to move at the same time.

    You should use the "with timeout" statement, because the command fails to complete in the allotted time (the default is two minutes).

     

     

    set x to choose folder
    tell application "System Events"
          with timeout of 0 seconds -- no AppleEvent timed out 
                repeat with p in (get path of folders of x)
                      set this_Folder to p as alias
                      move (files of folders of this_Folder whose visible is true) to this_Folder
                      delete folders of this_Folder
                end repeat
          end timeout
    end tell
    

     

    Also, you must consider that the script does not check names, an error will occur if the script tries to move two files with the same name.