chris_james_87

Q: Move file into folder of same name in AppleScript

I've been having an issue with the script below. Basically, on running the script, I want it to create folders based on the file name and move the individual files into the correlating folders. However, at the minute I'm having to choose which folder the script should run in.

 

set theFolder to choose folder

tell application "Finder"

  set fileList to name of every file of theFolder whose name extension is "m4v"

end tell

 

set text item delimiters to "."

 

repeat with theFile in fileList

  get every text item of theFile

  set masterFile to item 1 of result

  tell application "Finder"

  make new folder at theFolder with properties {name:masterFile}

  move (every file of folder theFolder whose name contains masterFile) to result

  end tell

end repeat

 

Ideally, I would like it to run automatically but the folder is on an external hard drive. (\Volumes\Lacie\Movies). and I've been having trouble using the POSIX and "as alias" to correctly recreate the desired file path.

 

Any help would be greatly appreciated. Thanks!

MacBook Pro, OS X El Capitan (10.11.3)

Posted on Apr 10, 2016 7:28 AM

Close

Q: Move file into folder of same name in AppleScript

  • All replies
  • Helpful answers

  • by Hiroto,

    Hiroto Hiroto Apr 10, 2016 8:06 PM in response to chris_james_87
    Level 5 (7,306 points)
    Apr 10, 2016 8:06 PM in response to chris_james_87

    Hello

     

    You might try something like the following AppleScript script which is a mere wrapper of shell script.

     

     

    set d to (choose folder)'s POSIX path
    do shell script "/bin/bash -s <<'EOF' - " & d's quoted form & " 2>&1
    cd \"$1\" || exit
    shopt -s nullglob
    for f in *.m4v
    do
        m=${f%.*}
        [[ -d $m ]] || mkdir -p \"$m\" || continue
        mq=$(sed 's/[^[:alnum:]]/\\\\&/g' <<< \"$m\")   # quote meta characters for regex
        find -E . -depth 1 -type f -iregex '.*/'\"$mq\"'.[[:alnum:]]+$' -print0 | xargs -0 -J% mv % \"$m\"
    done
    EOF"
    

     

     

     

    E.g., Given that you choose /path/to/directory as source folder -

     

    input state:

     

    /path/to/directory/abc.m4v
    /path/to/directory/abc.txt
    /path/to/directory/abcd.m4v
    /path/to/directory/abcd.txt
    

     

     

    output state:

     

    /path/to/directory/abc/abc.m4v
    /path/to/directory/abc/abc.txt
    /path/to/directory/abcd/abcd.m4v
    /path/to/directory/abcd/abcd.txt
    

     

     

     

    Briefly tested under OS X 10.6.8 but no warranties of any kind. Please make sure you have complete backup of the original directories and files before running this sort of script.

     

    Good luck,

    H

  • by Camelot,

    Camelot Camelot Apr 11, 2016 10:00 PM in response to chris_james_87
    Level 8 (47,290 points)
    Mac OS X
    Apr 11, 2016 10:00 PM in response to chris_james_87

    Are you looking for suggestions to fix/improve your script, or asking how to get it to run automatically?


    The latter is simple - convert it to a Folder Action and apply it to your folder - it will run any time files are dropped in the folder, and can automatically process those files.

     

    However, there are problems with your script - you rely too much on 'the result', and the line:

     

      move (every file of folder theFolder whose name contains masterFile) to result


    is prone to name conflict errors - let's say you have 'video 1.m4v' through 'video 20.m4v' in your base directory.

    This script will, presumably, create a folder 'video 1' then proceed to move the files video 1.m4v, video 10.m4v, video 11.m4v', etc. since all this file names contain 'video 1'. Somehow I doubt that's your intention.

     

    Instead, it's better to deal directly with the objects in question - that means keeping a reference to the files from the get-go, rather than just their names, e.g.:

     

    set theFolder to choose folder -- could be a specific folder, or a folder action

     

    tell application "Finder"

      -- track the files, not the files' names.

      set fileList to every file of theFolder whose name extension is "csv"

     

      -- standard TIDs stuff

      set oldTIDs to my text item delimiters

      set my text item delimiters to "."

     

      repeat with theFile in fileList

      set fileName to name of theFile

      set masterFile to text item 1 of fileName

      set newFolder to make new folder at theFolder with properties {name:masterFile}

      move theFile to newFolder

      end repeat

     

      -- clean up TIDs

      set my text item delimiters to oldTIDs

    end tell


    Note how I'm using specific variables such as fileName, newFolder, etc. to track where I am in the list, and that I move the specific file I'm iterating on, rather than rely on some arbitrary name-matching. I also cleaned up the text item delimiters stuff since that's just asking for trouble.

     

    Now, this doesn't answer the original question of what it is you're asking for - are you asking for this to run automatically? is a Folder Action a viable answer? or are you just looking for ways to manage external volumes?

  • by rakeshsingh93,

    rakeshsingh93 rakeshsingh93 May 26, 2016 10:43 PM in response to Camelot
    Level 1 (12 points)
    Mac OS X
    May 26, 2016 10:43 PM in response to Camelot

    Great job thanks for it

     

    but i am facing problem to arrange a same file one is client.tif and another is final.tif in one folder . the unique name is same.

    like i have a tiff  ex:   000MI_EDI27_ARROSAGE_Chromie_Client001.tif

                                      000MI_EDI27_ARROSAGE_Chromie_Final001.tif    

                                      000MI_EDI27_ARROSAGE_Chromie_Client002.tif    

                                      000MI_EDI27_ARROSAGE_Chromie_Final002.tif

                                      000MM_EDI27_Jardin_Clemence_Chromie_Client001.tif

                                      000MM_EDI27_Jardin_Clemence_Chromie_Final001.tif

                                      000MM_EDI27_Jardin_Clemence_Chromie_Client002.tif

                                      000MM_EDI27_Jardin_Clemence_Chromie_Final002.tif

    i want to arrange all these file in same name folder arrange according to unite id  name (Prefix)

     

    So please support and provide the applescript  to do this task

     

    Thanks

  • by Camelot,

    Camelot Camelot May 26, 2016 10:59 PM in response to rakeshsingh93
    Level 8 (47,290 points)
    Mac OS X
    May 26, 2016 10:59 PM in response to rakeshsingh93

    This is pretty straightforward.

     

    Just replace the line:

     

      set newFolder to make new folder at theFolder with properties {name:masterFile}


    with:


    tell application "Finder"

      try

      set newFolder to folder masterFile of theFolder

      on error

      set newFolder to make new folder at theFolder with properties {name:masterFile}

      end try

    end tell


    The idea here is that the script tries to grab the appropriate folder. If it exists, all is well and the script continues. If it fails, it throws an error and follows the path to create the folder.

  • by rakeshsingh93,

    rakeshsingh93 rakeshsingh93 May 27, 2016 10:35 PM in response to Camelot
    Level 1 (12 points)
    Mac OS X
    May 27, 2016 10:35 PM in response to Camelot

    Thanks for Reply but this code is generate a error message i have  a script its work properly but when its run they create a one folder and move the only one

    Type of file which in folder.

    like : -     

                                       00MI_EDI27_ARROSAGE_Chromie_Client001.tif                                  

                                      000MI_EDI27_ARROSAGE_Chromie_Final001.tif    

                                      000MI_EDI27_ARROSAGE_Chromie_Client002.tif    

                                      000MI_EDI27_ARROSAGE_Chromie_Final002.tif

                                      000MM_EDI27_Jardin_Clemence_Chromie_Client001.tif

                                      000MM_EDI27_Jardin_Clemence_Chromie_Final001.tif

                                      000MM_EDI27_Jardin_Clemence_Chromie_Client002.tif

                                      000MM_EDI27_Jardin_Clemence_Chromie_Final002.tif

     

    Create One folder named : 00MI_EDI27_ARROSAGE_Chromie_Folder and which have only move this file

     

                                       00MI_EDI27_ARROSAGE_Chromie_Client001.tif                                        

                                      000MI_EDI27_ARROSAGE_Chromie_Final001.tif    

                                      000MI_EDI27_ARROSAGE_Chromie_Client002.tif    

                                      000MI_EDI27_ARROSAGE_Chromie_Final002.tif

    We want to continue these operation to all file simulate when i run this apple script.(one time for processing)

    my Script is _ 

    tell application "Finder"

      set theFolder to folder choose folder with prompt "/Users/king/Desktop/Burda France/Softproof"

      set theFile to first document file of theFolder

      set theFileName to name of theFile

      set AppleScript's text item delimiters to "_"

      set newFolderName to text item 1 of theFileName & "_" & text item 2 of theFileName & "_" & text item 3 of theFileName

      set AppleScript's text item delimiters to ""

      set theNewFolder to (make new folder at theFolder with properties {name:newFolderName})

      set moveList to {}

      set moveList to (every document file of theFolder whose name starts with newFolderName)

      if moveList is not {} then

      repeat with theDocument in moveList

      move theDocument to theNewFolder

      end repeat

      end if

    end tell

  • by rakeshsingh93,

    rakeshsingh93 rakeshsingh93 May 27, 2016 10:36 PM in response to rakeshsingh93
    Level 1 (12 points)
    Mac OS X
    May 27, 2016 10:36 PM in response to rakeshsingh93

    Screen Shot 2016-05-28 at 11.06.03 am.png

  • by Hiroto,

    Hiroto Hiroto May 29, 2016 12:57 AM in response to rakeshsingh93
    Level 5 (7,306 points)
    May 29, 2016 12:57 AM in response to rakeshsingh93

    Hello

     

    You may try something like this.

     

     

    set d to choose folder
    _main({d})
    on _main(argv)
        (*
            list argv : list of source directories
        *)
        script o
            property ff : {} -- list of aliases
            property gg : {} -- list of aliases to be moved per prefix
            property nn : {} -- list of file names
            property mm : {} -- list of prefixes
            property mm1 : {} -- list of distinct prefixes
            
            repeat with d in argv -- per source directory
                set d to d's contents
                -- retrieve list of aliases and names for document files
                tell application "Finder"
                    tell item d's document files to set {ff, nn} to {it as alias list, name}
                end tell
                
                -- retrieve prefixes from file names
                (*  E.g.
                        name   = AAA_BBB_CCC_DDD.tif
                        prefix = AAA_BBB_CCC
                *)
                try
                    set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"_"}}
                    repeat with i from 1 to count my nn
                        set {n, m} to {my nn's item i, ""}
                        if (count n's text items) > 1 then set m to "" & n's text items 1 thru -2
                        set my mm's end to m
                        if m ≠ "" and m is not in my mm1 then set my mm1's end to m
                    end repeat
                    set AppleScript's text item delimiters to astid0
                on error errs number errn
                    set AppleScript's text item delimiters to astid0
                    error errs number errn
                end try
                
                -- distribute files per prefix
                repeat with m in my mm1 -- per distinct prefix
                    set m to m's contents
                    -- prepare destination for this prefix
                    try
                        set dst to (d as string) & m & ":" as alias
                    on error number -43 -- dst not present
                        tell application "Finder" to set dst to (make new folder at d with properties {name:m}) as alias
                    end try
                    -- collect sources for this prefix
                    repeat with i from 1 to count my mm
                        if my mm's item i = m then set my gg's end to my ff's item i
                    end repeat
                    -- move collected sources to destination
                    if gg ≠ {} then tell application "Finder" to move gg to dst with replacing
                    -- reset this collection
                    set gg to {}
                end repeat
            end repeat
        end script
        tell o to run
    end _main
    

     

     

     

    Script is briefly tested under OS X 10.6.8 but no warranties of any kind. Please make sure you have complete backup of the original directories before running this sort of script.

     

    Regards,

    H

  • by rakeshsingh93,

    rakeshsingh93 rakeshsingh93 May 29, 2016 11:22 PM in response to Hiroto
    Level 1 (12 points)
    Mac OS X
    May 29, 2016 11:22 PM in response to Hiroto

    Hi,

    Thanks a lot for support.  

  • by rakeshsingh93,

    rakeshsingh93 rakeshsingh93 Jun 8, 2016 6:13 AM in response to Hiroto
    Level 1 (12 points)
    Mac OS X
    Jun 8, 2016 6:13 AM in response to Hiroto

    hi,

     

    I have tried this script its work successfully. but  i want to use this script as a folder action

    it means can i change the choose folder option with on adding folder items to this_folder after receiving these_items

    i am trying to do but when i upload this script on folder action script its not work(execute).

     

    So If you have any idea to how to change this Script option to folder action script please support and guide us.

     

    Thanks

    Rakesh

  • by Camelot,

    Camelot Camelot Jun 8, 2016 9:20 AM in response to rakeshsingh93
    Level 8 (47,290 points)
    Mac OS X
    Jun 8, 2016 9:20 AM in response to rakeshsingh93

    i am trying to do but when i upload this script on folder action script its not work(execute).

     

    Without seeing how you changed the script it's impossible to tell what went wrong, but looking at it, you just need to change:

     

    set d to choose folder 

    to:

     

    on adding folder items to this_folder after receiving these_items

      repeat with d in these_items

      tell application "System Events"

      set p to properties of d

      if type identifier of p is "public.folder" then

      _main({d})

      end if

      end tell

      end repeat

    end adding folder items to


     

    This loops through the added items, checking to see if they are folders. If they are, it passes that folder to the _main() handler.

    Note that most of the work here goes into identifying whether the newly-added items are folders or something else.

  • by rakeshsingh93,

    rakeshsingh93 rakeshsingh93 Jun 10, 2016 12:11 AM in response to Camelot
    Level 1 (12 points)
    Mac OS X
    Jun 10, 2016 12:11 AM in response to Camelot

    hi,

    Thanks for support.

    i have tried it but it's not working properly.

    when i use this script to folder action utility and upload on a folder action it not work.

    my script is here please check and revert me your valuable support.

    on adding folder items to this_folder after receiving these_items

       

        repeat with d in these_items

           

            tell application "System Events"

               

                set p to properties of d

               

                if type identifier of p is "public.folder" then

                   

                    _main({d})

                   

                end if

               

            end tell

           

        end repeat

       

        on_main(argv)

        (*

            list argv : list of source directories

        *)

        script o

            property ff : {} -- list of aliases

            property gg : {} -- list of aliases to be moved per prefix

            property nn : {} -- list of file names

            property mm : {} -- list of prefixes

            property mm1 : {} -- list of distinct prefixes

           

            repeat with d in argv -- per source directory

                set d to d's contents

                -- retrieve list of aliases and names for document files

                tell application "Finder"

                    tell item d's document files to set {ff, nn} to {it as alias list, name}

                end tell

               

                -- retrieve prefixes from file names

                (*  E.g.

                        name   = AAA_BBB_CCC_DDD.tif

                        prefix = AAA_BBB_CCC

                *)

                try

                    set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"_"}}

                    repeat with i from 1 to count my nn

                        set {n, m} to {my nn's item i, ""}

                        if (count n's text items) > 1 then set m to "" & n's text items 1 thru -2

                        set my mm's end to m

                        if m ≠ "" and m is not in my mm1 then set my mm1's end to m

                    end repeat

                    set AppleScript's text item delimiters to astid0

                on error errs number errn

                    set AppleScript's text item delimiters to astid0

                    error errs number errn

                end try

               

                -- distribute files per prefix

                repeat with m in my mm1 -- per distinct prefix

                    set m to m's contents

                    -- prepare destination for this prefix

                    try

                        set dst to (d as string) & m & ":" as alias

                    on error number -43 -- dst not present

                        tell application "Finder" to set dst to (make new folder at d with properties {name:m}) as alias

                    end try

                    -- collect sources for this prefix

                    repeat with i from 1 to count my mm

                        if my mm's item i = m then set my gg's end to my ff's item i

                    end repeat

                    -- move collected sources to destination

                    if gg ≠ {} then tell application "Finder" to move gg to dst with replacing

                    -- reset this collection

                    set gg to {}

                end repeat

            end repeat

        end script

        tell o to run

        end_main

    end adding folder items to

     

    This is the script which i use on Folder action utility.

     

    Thanks

    Rakesh