1 2 3 4 Previous Next 55 Replies Latest reply: Feb 6, 2014 4:41 AM by Mike9586
kashlan Level 1 Level 1 (0 points)
Hello,

I have a folder full of hundreds of Video clips. I am interested in a script that will

1. look at the files sequentially
2. For each file read the creation date
3. If a Folder with that creation date does not exist, create a folder with that creation date (YYYY-MM-DD)
4. Move the file to the folder with the file's creation date

I've never used automator. Is this easy to do using that program?

The reason I want to do this is to avoid having to manually re-organize the videos by date, if I were to import the contents of the existing folder of video clips into iMovie.

Appreciate any input you can provide.

Thanks.

MacBook Pro, Mac OS X (10.5.7)
  • 1. Re: Relocating files based on creation date
    red_menace Level 6 Level 6 (14,615 points)
    Automator is designed to take small, pre-written routines (actions) and string them together into a workflow. There isn't a default action to do something like this, but the Run AppleScript action can be used. Actually, an AppleScript would probably be the way to go, but I just happened to have an Automator action lying around for some reason.

    Tested workflow:
    1) *Ask for FInder Items* {Type: Folders}
    2) *Get Folder Contents* (be careful with getting subfolders - this action will go into packages)
    3) *Run AppleScript:*
    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    font-weight: normal;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px; height: 340px;
    color: #000000;
    background-color: #FFEE80;
    overflow: auto;"
    title="this text can be pasted into an Automator 'Run AppleScript' action">
    on run {input, parameters} -- create folders and move
    (*
    make new folders from file creation dates (if needed), then move document files into their respective new folders
    if no container is specified (missing value), the new folder will be created in the containing folder of the item
    if the container is not a valid path (and not missing value), one will be asked for
    input: a list of Finder items (aliases) to move
    output: a list of the Finder items (aliases) moved
    *)

    set output to {}
    set SkippedItems to {} -- this will be a list of skipped items (errors)
    set TheContainer to "" -- a Finder path to a destination folder, or missing value for the source folder

    if TheContainer is not missing value then try -- check the destination path
    TheContainer as alias
    on error
    set TheContainer to (choose folder with prompt "Where do you want to move the items?")
    end try

    tell application "Finder" to repeat with AnItem in the input -- step through each item in the input
    if TheContainer is not missing value then -- move to the specified folder
    set {class:TheClass, name:TheName, name extension:TheExtension} to item AnItem
    else -- move to the source folder
    set {class:TheClass, name:TheName, name extension:TheExtension, container:TheContainer} to item AnItem
    end if
    if TheClass is document file then try -- just documents
    set TheDate to text 1 thru 10 of (creation date of AnItem as «class isot» as string) -- YYYY-MM-DD
    try -- check if the target folder exists
    get ("" & TheContainer & TheDate) as alias
    on error -- make a new folder
    make new folder at TheContainer with properties {name:TheDate}
    end try
    -- duplicate AnItem to the result
    move AnItem to the result
    set the end of output to (result as alias) -- the new file alias
    on error -- permissions, etc
    -- set the end of SkippedItems to (AnItem as text) -- the full path
    set the end of SkippedItems to TheName -- just the name
    end try
    end repeat

    ShowSkippedAlert for SkippedItems
    return the output -- pass the result(s) to the next action
    end run


    to ShowSkippedAlert for SkippedItems
    (*
    show an alert dialog for any items skipped, with the option to cancel the workflow
    parameters - SkippedItems [list]: the items skipped
    returns nothing
    *)
    if SkippedItems is not {} then
    set {AlertText, TheCount} to {"Error with AppleScript action", count SkippedItems}
    if TheCount is greater than 1 then
    set theMessage to (TheCount as text) & space & " items were skipped:"
    else
    set theMessage to "1 " & " item was skipped:"
    end if
    set {TempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
    set {SkippedItems, AppleScript's text item delimiters} to {SkippedItems as text, TempTID}
    if button returned of (display alert AlertText message (theMessage & return & SkippedItems) ¬
    alternate button "Cancel" default button "OK") is "Cancel" then error number -128
    end if
    return
    end ShowSkippedAlert
    </pre>
  • 2. Re: Relocating files based on creation date
    kashlan Level 1 Level 1 (0 points)
    Thanks a TON for your help with this request. It worked like a charm and flawlessly. I sorted 1000+ video clips that were all in one folder exactly as desired.

    I hope others get to find your post. Bet there are many others with a similar need.

    Deeply appreciated!!!!
  • 3. Re: Relocating files based on creation date
    jono1515 Level 1 Level 1 (0 points)
    Sweet, I've been wishing I knew how to put something like this together for awhile now, saved me a lot of work. Thanks.
  • 4. Re: Relocating files based on creation date
    RavenMac Level 1 Level 1 (0 points)
    This is fantastic! However, if I needed to modify the code to make folders on "year" only and place all images based on that, where would the change need to be made? (forgive the question, AppleScript is still rather new to me).
  • 5. Re: Relocating files based on creation date
    rock4Him Level 1 Level 1 (0 points)

    I'm guessing you could replace the line that starts with:

     

    set TheDate to text 1 thru 10 of ...

     

    to

     

    set TheDate to text 1 thru 4 of

     

    and that would give you folders based on the year only.

  • 6. Re: Relocating files based on creation date
    mehighpenet Level 1 Level 1 (0 points)

    Thank you soo much for this post... saved me a ton of time!

  • 7. Re: Relocating files based on creation date
    objetty Level 1 Level 1 (0 points)

    I'm glad I came across your post.  I did a lot of searching before I found something that will help me and this looks like it will.  I am new to Mac and helpless with AppleScript.  I have succesfully created some Automator actions for renaming files, etc. 

     

    My goal is to take many files in one folder and have them placed into folders based on the "Date Created" field with that date being the name of the folder in the format of 2012_month_day (month and day as two digits).  For example today is 2012_01_20.

     

    I've tried cutting and pasting the information in your post to a blank AppleScript but get error after error when I try to run the script.

     

    Would you be so kind as to give me more basic instructions in how to turn your post into a valid AppleScript?

     

    Thanks in advance.

     

    iMac OS X (10.5.8),  3.06 GHz Intel Core 2 Duo,  4GB 1067 MHz DDR3

  • 8. Re: Relocating files based on creation date
    MrTcGill Level 1 Level 1 (0 points)

    Brilliant script. Thanks so much :-)

    Give your self a well deserved pat on the back.

  • 9. Re: Relocating files based on creation date
    red_menace Level 6 Level 6 (14,615 points)

    oops, didn't see your post.  The script part is between the <pre ...> and </pre> tags (the text formatting shows up in posts predating the current forums), so if that part is being included the Script Editor won't know what it is.  If you are wanting to use it as a script outside of Automator, just get rid of the {input, parameters} part in the run handler, and set the input variable by using something like choose file with multiple selections allowed.  You will also need to replace the alternate button "Cancel" terminology with buttons {"Cancel", "OK"} in the showSkippedAlert handler, since that term only works in Automator.

  • 10. Re: Relocating files based on creation date
    Amien90 Level 1 Level 1 (0 points)

    Thanks fot this script. Searched along time for something like this .. but it doesnt quite work. Might be a OS issue.

     

    The script works, but all pictures are moved to 2013-01-01 instead of the creation date. any idea?

     

    i think it's in this part : «class isot»

  • 11. Re: Relocating files based on creation date
    Amien90 Level 1 Level 1 (0 points)

    checked again .. seems like the YYYY-MM-DD goes ok, but it gets the current date instead of the date of file creation

  • 12. Re: Relocating files based on creation date
    biggerboy Level 1 Level 1 (0 points)

    How can this be modified to work as an Image Capture plug-in? Would love to have the photos downloaded to the Pictures folder already organized into folders....

  • 13. Re: Relocating files based on creation date
    red_menace Level 6 Level 6 (14,615 points)

    You should be able to just use the Run AppleScript action in an Image Capture Plugin document - the plugin automatically receives the files, so it is not necessary to use the Ask for Finder Items or Get Folder Contents actions to get the input items.

  • 14. Re: Relocating files based on creation date
    biggerboy Level 1 Level 1 (0 points)

    It worked! Thanks. One last question - possible to just have the pictures go in sorted in the same folder they're in (Pictures) instead of having to select the folder? If not, then I can live with it.

     

    Thanks!

1 2 3 4 Previous Next