1 2 Previous Next 15 Replies Latest reply: Oct 30, 2012 9:48 AM by applescript_problems
acabaero Level 1 Level 1 (0 points)

Hi All!

 

Here is what I am trying to figure out. I would love some help with this script if anyone can lend a helping hand. I am currently using Hazel to do this but I think AppleScript will be alot more effective.

 

Situation:

 

- I have a folder with 4 different file types inside - the extensions are (.cop, .cof, .cos, .cr2)

- These files need to be organized in this folder structure  Captureone > Cache > Proxies

                                                                                                              > Settings50

 

- The .cop & .cof files both need to be put in the "Proxies" folder

- The .cos needs to be put in the "Settings50" folder

- The .cr2 can stay where it is.

 

 

I'm already using an AppleScript application where it automatically creates the folder structure in the current finder location, which works great. I use a program named Hazel to sort the files automatically into the folders where they need to go. Unfortunately all my computers at work don't have Hazel and I would like help to make a script. Any thoughts to add to my existing script?

 

 

#  define a list of folders - items will need to be quoted if they contain spaces, etc.

property theFolders : {"Captureone/Cache/Proxies", "Captureone/Settings50"} -- can also nest, e.g. "Inside/One"

 

try

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

on error -- no window

          set targetFolder to (choose folder)

end try

 

# build a parameter string from the folder list

set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}

set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}

 

do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders

 


MacBook Pro, Mac OS X (10.6.8)
  • 1. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    Niel Level 10 Level 10 (242,010 points)

    Here:

     

    tell application "Finder"

    move (every file of window 1 whose name extension is "cop") to folder "Captureone:Cache:Proxies" of window 1

    move (every file of window 1 whose name extension is "cof") to folder "Captureone:Cache:Proxies" of window 1

    move (every file of window 1 whose name extension is "cos") to folder "Captureone:Cache:Proxies:Settings50" of window 1

    end tell

     

    (64829)

  • 2. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    acabaero Level 1 Level 1 (0 points)

    Hi Niel,

     

    Thanks for the help. I added what you said and got this error. Any thoughts?

     

     

    #  define a list of folders - items will need to be quoted if they contain spaces, etc.

    property theFolders : {"Captureone/Cache/Proxies", "Captureone/Settings50"} -- can also nest, e.g. "Inside/One"

     

    try

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

    on error -- no window

              set targetFolder to (choose folder)

    end try

     

    # build a parameter string from the folder list

    set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}

    set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}

     

    do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders

     

    tell application "Finder"

              move (every file of window 1 whose name extension is "cop") to folder "Captureone:Cache:Proxies" of window 1

              move (every file of window 1 whose name extension is "cof") to folder "Captureone:Cache:Proxies" of window 1

              move (every file of window 1 whose name extension is "cos") to folder "Captureone:Cache:Proxies:Settings50" of window 1

    end tell

     

     

     

    Screen shot 2012-03-14 at 3.42.21 PM.png

  • 3. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    Niel Level 10 Level 10 (242,010 points)

    It's designed to work on the frontmost Finder window. If the target folder is stored in the variable, use 'of targetFolder' wherever 'of window 1' is.

     

    (64830)

  • 4. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    acabaero Level 1 Level 1 (0 points)

    Hi Niel, sorry for all the questions. This time I changed 'of window 1' to 'targetFolder' like you said. And I got this. I am saving it as an application in AppleScript then dragging it to my tool bar of my finder window. I am selecting the folder I want and hitting the application button. Is this the correct way I should launch the script to work on the folder?

     

     

    #  define a list of folders - items will need to be quoted if they contain spaces, etc.

    property theFolders : {"Captureone/Cache/Proxies", "Captureone/Settings50"} -- can also nest, e.g. "Inside/One"

     

    try

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

    on error -- no window

              set targetFolder to (choose folder)

    end try

     

    # build a parameter string from the folder list

    set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}

    set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}

     

    do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders

     

    tell application "Finder"

              move (every file of targetFolder whose name extension is "cop") to folder "Captureone:Cache:Proxies" of targetFolder

              move (every file of targetFolder whose name extension is "cof") to folder "Captureone:Cache:Proxies" of targetFolder

              move (every file of targetFolder whose name extension is "cos") to folder "Captureone:Cache:Proxies:Settings50" of targetFolder

    end tell

     

     

     

    Screen shot 2012-03-14 at 3.57.05 PM.png

  • 5. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

     

    acabaero wrote:

     

     

    Screen shot 2012-03-14 at 3.57.05 PM.png

    Because the folder "Captureone:Cache:Proxies:Settings50"  is not created.

     

    The script create the folder "Captureone:Settings50"

     

    -------------------

    try

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

    on error -- no window

         set targetFolder to (choose folder)

    end try

     

    do shell script "cd " & (quoted form of POSIX path of targetFolder) & "; mkdir -p Captureone/Cache/Proxies Captureone/Settings50"

     

    tell application "Finder" to tell targetFolder

         move (files whose name extension is in {"cop", "cof"}) to folder "Captureone:Cache:Proxies"

         move (files whose name extension is "cos") to folder "Captureone:Settings50"

    end tell

  • 6. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    acabaero Level 1 Level 1 (0 points)

    Hi Jacques,

     

    Below is my folder structure. The script in my first post works perfectly find to create the folder structure I need. The second part to sort the images is where I can't get it to work. Maybe this image will help illustrate where I need files to go. Can you please send me the full script text I should be using if you can? Thanks for the help!

     

    Screen shot 2012-03-14 at 5.09.49 PM.png

    Screen shot 2012-03-14 at 5.09.56 PM.png

  • 7. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    Jacques Rioux Level 4 Level 4 (1,960 points)

    acabaero wrote:

     

    Hi Jacques,

     

    Can you please send me the full script text I should be using if you can? Thanks for the help!

     

     

     

    Yes, here is the script

     

    -------------------

    try

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

    on error -- no window

         set targetFolder to (choose folder)

    end try

     

    do shell script "cd " & (quoted form of POSIX path of targetFolder) & "; mkdir -p Captureone/Cache/Proxies Captureone/Settings50"

     

    tell application "Finder" to tell targetFolder

         move (files whose name extension is in {"cop", "cof"}) to folder "Captureone:Cache:Proxies"

         move (files whose name extension is "cos") to folder "Captureone:Settings50"

    end tell

    --------------------

  • 8. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    acabaero Level 1 Level 1 (0 points)

    I tried your script and got this. I feel like there needs to be a pause after the folders get created and before they are sorted. The error comes up at the same time the Captureone folder gets created. Just a thought, please excuse my AppleScript ignorance. haha

     

    Screen shot 2012-03-14 at 5.31.21 PM.png

  • 9. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    Jacques Rioux Level 4 Level 4 (1,960 points)

    acabaero wrote:

     

    Screen shot 2012-03-14 at 5.31.21 PM.png

    No it was not related to the created folders.

    My syntax tell targetFolder when targetFolder is an alias doesn't work on some system.

     

    try this

     

    -------------------------

    try

              tell application "Finder" to set targetFolder to (target of the front Finder window) as string

    on error -- no window

              set targetFolder to (choose folder) as string

    end try

     

    do shell script "cd " & (quoted form of POSIX path of targetFolder) & "; mkdir -p Captureone/Cache/Proxies Captureone/Settings50"

     

    tell application "Finder"

              move (every file of folder targetFolder whose name extension is in {"cop", "cof"}) to folder "Captureone:Cache:Proxies" of folder targetFolder

              move (every file of folder targetFolder whose name extension is "cos") to folder "Captureone:Settings50" of folder targetFolder

    end tell

  • 10. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    acabaero Level 1 Level 1 (0 points)

    Thanks so much Jacques! It worked. Your a awesome!!!!!

  • 11. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    applescript_problems Level 1 Level 1 (0 points)

    Hi jacques rioux

     

    I was hoping some one can help me, i read post

    "Using AppleScript to sort files Into Specified Folders based on Extension"

    I have zero knoledge of apple script and i just started using automator. i found out the hard way that you can't sort in automator. and i would like some help to build a dynamic script so i can target the content and at the same time move files to folder according to the name that will end with underscore.

     

    for example:

    master folder name: 1

    sub folder path: 1>2>3>4>5

    insdie folder 5 theirs 3 folders: 5a, 5b, 5c

     

    i want to name my files like so: "name_5a" "name_5b" "name_5c

     

    i want the scrip to pick up all the files that ends with the name "_5a" and place them in folder 5a and repeat the process untill it finishes sorting all my files. what is the easy way to build this? the reason i want to build it dynamic is because i would like to re-use this script later for other folder structurs. i configured my phosotop to set layers to comps and i usually send them to the master folder so i would like the apple script to pick them up from their and sort all fo them according to the end of the naming convention, is this possible?

  • 12. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

     

     

    applescript_problems wrote:

     

    for example:

    master folder name: 1

    sub folder path: 1>2>3>4>5

    insdie folder 5 theirs 3 folders: 5a, 5b, 5c

     

    i want to name my files like so: "name_5a" "name_5b" "name_5c

     

    i want the scrip to pick up all the files that ends with the name "_5a" and place them in folder 5a

     

    I presume that the files are in the master folder not in the subFolders, that the files has an name extension.

     

    This script take the first two characters after the last underscore.

    It will move the file in the subfolder, according to the two characters found.

    ---------------------------

    set targetFolder to (POSIX file "") as alias --<-- drop the master folder between the double quotes

     

    tell application "Finder"

          repeat with tFile in (get files of targetFolder)

                set tName to name of tFile

                set subname to my getChar_1_2AfterLastUnderscore(tName)

                -- example : subname = "5c" -->  folder "5c" of folder "5" of targetFolder

                if subname is not "" then

                      tell folder subname of folder (text 1 of subname) of folder targetFolder

                            if exists then move tFile to it -- if exists this folder then move this file

                      end tell

                end if

          end repeat

    end tell

     

    on getChar_1_2AfterLastUnderscore(t)

          if "_" is in t then try

                set oTID to text item delimiters

                set text item delimiters to "_"

                set t to text 1 thru 2 of last text item of t

                set text item delimiters to oTID

                return t

          on error

                set text item delimiters to oTID

          end try

          return ""

    end getChar_1_2AfterLastUnderscore

    ---------------------------

    To get the exact path of the master folder :

    Drag/drop the folder between the double quotes in the first line of the script.

  • 13. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    applescript_problems Level 1 Level 1 (0 points)

    This worked perfectly. after i built the file structure, but unfortunately my setup was hypothetical, my tough was to take your script and modify it to work for my set up. but after looking at it, their was some limitation. I will create a video explaining my situation in deeper detail and send you the link hopefully you can help me next time.

    I apologize if you feel i wasted your time, it's my fault, i didn't fully describe in detail how i wanted to use the script. hopefully when i send you the video link it will help you with what i want to accomplish.

  • 14. Re: Using AppleScript to sort files Into Specified Folders based on Extension
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

    applescript_problems wrote:

     

    I will create a video explaining my situation in deeper detail and send you the link hopefully you can help me next time.

    When this is done, start a new discussion, because it has been solved.

     

    You'll have more luck than others helps you for the script in a new thread, as some may have other way to do this.

1 2 Previous Next