6 Replies Latest reply: Feb 21, 2008 5:31 PM by Hiroto
Casey D Level 1 Level 1 (25 points)
Here is the scenario...

1. A job is created with a unique name, i.e. Order0802172
2. new subfolders need to be created with their names prefixed by the parent folder, i.e. Order0802172_RawImages, Order0802172_Layouts, Order0802172_Completed, Order0802172_CorrectedImages, etc.

I'm trying to work the new variable function of Automator, but I'm thinking it's limited and I should just stick to AppleScript. Can anyone offers any help on this?

MacBook Pro 2.33 C2D, Mac OS X (10.5.2), 3 GB RAM
  • casdvm Level 2 Level 2 (300 points)
    I agree, this is a bit much for Automator, except that it does simplify Folder Actions, so you should be able to take this script here and put it inside of a *Run AppleScript* action, for a nice Folder Action:

    set a to choose folder
    set newsublist to {"_RawImages", "_Layouts", "_Completed", "_CorrectedImages"}
    tell application "Finder"
    set a_name to a's name
    repeat with nf in newsublist
    make new folder in a with properties {name:(a_name & nf)}
    end repeat
    end tell

    Or, you could just use this whenever you need it.
  • Casey D Level 1 Level 1 (25 points)
    That's a great start, thanks!

    Hmmm, I guess I need to build on that concept. As an example I would be great to create subfolders, i.e. _Images folder containing _Raw, _Corrected, _Cropped.

    Watched folder labeled Orders (where base folder labeled "1234" get drop into)

    Result:

    Orders
    _1234Images
    ___1234ImagesRaw
    ___1234ImagesCorrected
    ___1234ImagesCropped
    _1234Layouts
    _1234Working
    _1234Completed

    Also I can't figure out how to have the script take the base folder dropped onto it, it prompts me instead. I can't remember the language to use instead of selected folder. Can there be a watched folder, that automagically moves/copies the dropped folder to the appropriate spot?

    And if at all possible grab template files from another folder for things like InDesign templates and tucking them into the appropriate folders...

    Asking alot I know. Sorry
  • casdvm Level 2 Level 2 (300 points)
    Casey:

    Sorry if my previous post was unclear. Allow me to try again. Actually, after some research, as well as some trial and error, it is clear that this need of your will most likely NOT function within a Watched Folder. The problem is that you are creating new folders, so if place your script or workflow there, every new folder that is created as a result of your script, will trigger the script, and so on and so on. Although that may be cool to watch, you would soon have a drive completely filled with empty folders.

    So, let's try a different approach, again within Automator. Another nice thing about Automator is that anything you save as an application automatically also functions as a droplet. For instance, this is a modification of my original script:

    on run {input, parameters}
    set fol_ders to input
    set newsublist to {"_RawImages", "_Layouts", "_Completed", "_CorrectedImages"}
    tell application "Finder"
    repeat with a in fol_ders
    set a_name to a's name
    repeat with nf in newsublist
    make new folder in a with properties {name:(a_name & nf)}
    end repeat
    end repeat
    end tell
    end run

    In Automator, simply paste this script into a *Run AppleScript* action, save the workflow as an application, and you can drop as many folders as you wish onto the icon, and all the little subfolders defined in this portion of the script:

    set newsublist to {"_RawImages", "_Layouts", "_Completed", "_CorrectedImages"}

    will be created in the dropped folder. You can add as many folder labels to this list as you wish, and every time you drop a folder (or group of folders) onto the application icon, all the subfolders will be made.

    As for the InDesign templates, you need to volunteer a bit more information about what you specifically want for that, but yes, it is certainly a doable process.
  • Casey D Level 1 Level 1 (25 points)
    That worked a little better, taking on the name of the folder(s) dragged onto the Automator app. My greater challenge is to create subfolders within those subfolders listed. I suppose I need a way to identify each of the items listed in the sub_list, setting each one as a named variable, then somehow create subfolders within them as well. Just not sure how to go about it...

    ----------------------------
    THE GOAL:

    1234
    ...1234_PDFs
    ...1234_Photos
    ......1234_Touchups
    ......1234_Originals
    ...1234_Products
    ......1234_TriFold
    ......1234_ThankYouCard
    ......1234_PrayerCard
    ......1234_Portrait
    ......1234_Memorial
    ......1234_DVD
    ......1234_8pgMemorialBooklet
    ...1234_Theme

    ----------------------------
    THE SCRIPT:

    on run {input, parameters}
    set fol_ders to input
    set sub_list to {"_PDFs", "_Photos", "_Products", "_Theme"}

    WITH

    set photossublist to item 1 of sub_list
    set productssublist to item 2 of sub_list

    OR

    set photossublist to {"_TouchUps", "_Originals"}
    set productssublist to {"_TriFold", "_ThankYouCard", "_PrayerCard", "_Portrait", "_Memorial", "_DVD", "_8pgMemorialBooklet"}

    tell application "Finder"
    repeat with a in fol_ders
    set a_name to a's name
    repeat with nf in sub_list
    make new folder in a with properties {name:(a_name & nf)}
    end repeat
    end repeat
    end tell
    end run

    ----------------------------
    For example, the subfolder labeled 1234_Photos would contain subfolders labeled Touchups and Originals...
  • casdvm Level 2 Level 2 (300 points)
    Casey:

    I believe this is closer to what you are attempting. I am not proud of the code, nor did I do a good job of using descriptive variables. Anyway, here it is:

    on run {input, parameters}
    set fol_ders to input
    set newsublist to {{"_Sub01", {"_RawImages", "_Layouts", "_Completed"}}, {"_Sub02", {"_something", "_CorrectedImages"}}}
    tell application "Finder"
    repeat with a in fol_ders
    set a_name to a's name
    repeat with nf in newsublist
    set fold01 to make new folder in a with properties {name:(a_name & (nf's item 1))}
    repeat with nnf in (nf's item 2)
    set aa_name to fold01's name
    make new folder in fold01 with properties {name:(aa_name & nnf)}
    end repeat
    end repeat
    end repeat
    end tell
    end run

    The variable newsublist is now a long list of lists. Each list is a 2-item list of folder names. The first item is the name of a sub-folder for the original folder dropped onto the droplet. The script then repeats through the second item in the list to create sub-folders inside of the first sub-folder.

    As long as you are careful about how you construct the lists, this should be nearly there for you.
  • Hiroto Level 5 Level 5 (5,955 points)
    Hello Casey D,

    Try something like the following script.
    (Please copy code from this web page, not from subscribed email text, for I espaped some characters for posting.)

    I don't know and can't test Automator, so this is plain AppleScript's droplet. Save it as application (bundle) and drag-drop the target root folders onto it.

    As you see, you can define your directory structure in the template text given inline, where each non-empty line corresponds to a directory's partial path under the drag-dropped root directory and the '%' in template is to be replaced by the name of the root directory. (Partial path for sub-directory is to be delimited by colon as in HFS path)

    ---
    Not sure but if you need code for Automator, you may perhaps change the open handler in the following script:

    on open aa
    --(current code omited)
    end open

    to this sort of run handler:

    on run {input, parameters}
    set aa to input -- pass input to aa
    --(current code omitted)
    end run



    Hope this may help,
    H



    --SCRIPT
    on open aa
    set template to "
    %_PDFs
    %_Photos
    %Photos:%Touchups
    %Photos:%Originals
    %_Products
    %Products:%TriFold
    %Products:%ThankYouCard
    %Products:%PrayerCard
    %Products:%Portrait
    %Products:%Memorial
    %Products:%DVD
    %Products:%8pgMemorialBooklet
    %_Theme
    "
    repeat with a in aa
    set a to a's contents
    tell application "Finder"
    set _isContainer to item a's class is in {folder, disk}
    set n to a's name
    end tell
    if _isContainer then
    set t1 to replace("%", n, template)
    set rp to a as Unicode text
    repeat with p in t1's paragraphs
    set p to p's contents
    if p is not "" then mkDir(rp & p)
    end repeat
    end if
    end repeat
    end open

    on mkDir(p)
    (*
      text p : full path of directory. e.g. "aaa:bbb:ccc" or "aaa:bbb:ccc:"
      return alias: alias of (created) directory. e.g. alias "aaa:bbb:ccc:"
    *)
    try
    return p as alias
    on error
    if p ends with ":" then set p to p's text 1 thru -2
    do shell script "mkdir -p " & quoted form of POSIX path of p
    return p as alias
    end try
    end mkDir

    on replace(x, y, t)
    (*
      string x, y : searching string and replacing string respectively
      string t : source string
      return : modified string such that x is replaced by y in t
    *)
    local astid, astid0, tt
    set astid to a reference to AppleScript's text item delimiters
    try
    set astid0 to astid's contents
    set astid's contents to {x}
    set tt to t's text items
    set astid's contents to {y}
    set t to "" & tt
    set astid's contents to astid0
    on error errs number errn
    set astid's contents to astid0
    error errs number errn
    end try
    return t
    end replace
    --END OF SCRIPT