kluznic

Q: Applescript move files to folder in specific order for Automator

Hi,

 

I have a script to do a bunch of stuff that makes PDF files that I stored into different folders. I want to be able to move items using a loop

i.e. repeat with i from 1 to (count of files in folder x) -- assuming all folders also have the same number of files

          move item i of folder x to folder toCombine

          move item i of folder y to folder toCombine

          move item i of folder z to folder toCombine

          tell application "combine"

               run

          end tell

      end repeat

 

"Combine" is just a simple automator app that takes the folder contents of the folder "toCombine" and appends them in the order I want which is X > Y > Z as page 1,2 and 3. However, I see fluctuations in the order as I continue running to loop while looking at the folder toCombine, resulting in Z > Y > X and random orders.

I've added delay and even a check to see if there's item from X before moving item from Y etc.

 

Is there a workaround for this?

OS X El Capitan (10.11.4)

Posted on May 30, 2016 11:05 PM

Close

Q: Applescript move files to folder in specific order for Automator

  • All replies
  • Helpful answers

  • by Pierre L.,

    Pierre L. Pierre L. May 31, 2016 5:11 AM in response to kluznic
    Level 5 (4,484 points)
    May 31, 2016 5:11 AM in response to kluznic

    Just an idea: are you aware that item i of any folder always refers to the alphabetical order of the items?

    For example:

    X.png

  • by Jacques Rioux,

    Jacques Rioux Jacques Rioux May 31, 2016 8:14 AM in response to Pierre L.
    Level 4 (3,418 points)
    Mac OS X
    May 31, 2016 8:14 AM in response to Pierre L.

    Hi Pierre,

    Pierre L. wrote:

     

    Just an idea: are you aware that item i of any folder always refers to the alphabetical order of the items?

     

     

     

    Not always with the "Finder", I tested with a folder which contains 7 files (on EL Capitan):

    testFinder_sort.png

    The result is {"Ball.txt", "Goal.txt", "Zoo.txt", "army.txt", "defense.txt", "thing.txt", "wars.txt"}

    This seems to be sort with names that start with a capital character, and after it is the lowercase names

     

     

    But "System Events" return the list of names in alphabetical order:

    tell application "Finder" to set tFolder to (target of front Finder window) as alias

    tell application "System Events"

            return (name of items of tFolder whose visible is true)

    end tell

    --> result: {"army.txt", "Ball.txt", "defense.txt", "Goal.txt", "thing.txt", "wars.txt", "Zoo.txt"}

  • by Jacques Rioux,Solvedanswer

    Jacques Rioux Jacques Rioux May 31, 2016 6:20 PM in response to kluznic
    Level 4 (3,418 points)
    Mac OS X
    May 31, 2016 6:20 PM in response to kluznic

    Hi,

     

    Solution:

    Sort the list by name, like this:

    --------

    tell application "Finder"

          set myList1 to sort (items of folder x) by name

          set myList2 to sort (items of folder y) by name

          set myList3 to sort (items of folder z) by name

        

          repeat with i from 1 to (count myList1) -- assuming all folders also have the same number of files

                move item i of myList1 to folder toCombine

                move item i of myList2 to folder toCombine

                move item i of myList3 to folder toCombine

                tell application "combine"

                      run

                end tell

          end repeat

    end tell

    ----------

     

    Also, I tested in Automator, it returns the folder content in alphabetical order.

    Otherwise, you must sort the items by name, just add the "Sort Finder Items" action after the "get folder content" action.

  • by Pierre L.,

    Pierre L. Pierre L. May 31, 2016 10:11 AM in response to Jacques Rioux
    Level 5 (4,484 points)
    May 31, 2016 10:11 AM in response to Jacques Rioux

    Hi Jacques,

     

    You seem to be right. Thanks for that information, that's both interesting and surprising.

  • by Camelot,Helpful

    Camelot Camelot May 31, 2016 6:20 PM in response to kluznic
    Level 8 (47,305 points)
    Mac OS X
    May 31, 2016 6:20 PM in response to kluznic

    I think your actual problem is a little different from a simple filename ordering.


    The problem is that the state of each of the source folders is changing as you run your script.

     

    For example, let's say that Folder X has four files named a, b, c, and d (no ambiguity as to what's expected, case, etc.)

     

    When your loop runs the first time (i = 1), the code:

     

              move item i of folder x to folder toCombine

     

    moves file 'a' (item 1 of folder x) to the destination. Fair enough. The problem occurs on the second iteration (i = 2) where the code now picks the file c, not b as you might expect.

     

    The reason for this is simple - file 'a' no longer exists in the source directory (you moved it out), and the second file from the list of files in the folder (i.e. {"b", "c", "d"} ) is c.

     

    Jacques solution works because he, in effect, pre-computes the list of files so it doesn't matter whether they move around - item 3 of the list is still the file 'c', even if 'a' and 'b' have been moved.

  • by kluznic,

    kluznic kluznic May 31, 2016 6:24 PM in response to kluznic
    Level 1 (8 points)
    Mac OS X
    May 31, 2016 6:24 PM in response to kluznic

    Hi Camelo, Pierre and Jacques,

     

    I also realised after playing a little with Finder that there's some alphabetical order sorting.

     

    And regarding the state of each source folder, I have made sure to also take into account the item that was already moved resulting in the change to the remaining items in the folder's item numbers.

     

    Thanks guys, really appreciate it