How to ask for variable and pass to shell script in automator?

Discussion in 'Mac Programming' started by zBernie, May 1, 2014.

  1. zBernie macrumors newbie

    Joined:
    Aug 3, 2013
    #1
    I am really pulling my hair out trying to do something that you think would be simple to do in automator, but after searching net extensively, and finding only half baked solutions, I think what should be a simple task in automator is impossible.

    All I want to do is prompt for a variable at which point I enter a date. Then pass that date to a shell script. Sounds simple enough. I have a screenshot at the link below which shows what I am trying to do. Here is a text description of the workflow actions:

    Ask for Text (Get date variable)
    Get Value of Variable (the_date)
    Get Selected Finder Items (Get file names to operate on)
    Run Shell Script (as arguments):

    for i in "$@"
    do
    /usr/bin/SetFile -m "03/13/2009 09:00" "$i"
    done

    This works to set the file date to 03/13/2009, which I want to replace with the_date variable, or whatever.

    Here's the workflow:

    https://www.flickr.com/gp/zbernie/vwah0u
     
  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    In an Automator workflow, the output of any given action is the input to the following action. Your posted workflow is doing something like:

    1. Selected files or folders are passed to the workflow (it is a service);
    2. The Ask for Text action will use the input files or folders as default text for the answer, so you are essentially throwing away the original input;
    3. The value of the variable the_date (whatever that is) is added to the list of items;
    4. The selected Finder items are added to the list (this would normally result in duplicate items, but since the original input was replaced with the text returned by the dialog, this has the accidental effect of just restoring the input items);
    5. the resulting list of items (the text, the variable, and the selected items) are then passed to the shell script.

    The service workflow should look more like:

    1. Selected files or folders are passed to the workflow;
    2. Set Value of Variable (input) - stash the input so that it doesn't get lost;
    3. Ask for Text - ignore the input so that the previous list of items are not used in the dialog;
    4. Get Value of Variable (input) - get the original input items back
    5. Run Shell Script - the arguments passed to the shell script is now a list consisting of the text (from the dialog) and the file items originally input to the service.

    Either way, you just need to keep track of the order of the arguments (you can always look at the results of any particular action to see what the output is). In the case of the my example workflow, you can just strip off the first item and use it as the date for the remaining items.
     

Share This Page