4

I'm trying to do the following in an Automator workflow:

  1. Ask for Finder items from folder A
  2. In folder A, add a suffix to the file names
  3. Copy them to folder B
  4. In folder B, with an AppleScript, split the file name by a delimiter and then rename the file with the second half of the text split
  5. In folder B, add a prefix to the file names
  6. In folder B, add a suffix to the file names

Here is a sample of the file names I'm trying to rename:

  • a new file$SG789.jpg
  • file_2$123-456.jpg
  • file_name$LG123.jpg
  • this_file$558-432.jpg

The delimiter I'm splitting by is the "$" dollar sign to create the following new files:

  • SG789.jpg
  • 123-456.jpg
  • LG123.jpg
  • 558-432.jpg

These are the file name formats I'd like for the automator to continue processing.

Thanks to another user, I was able to get this far. However, my workflow is failing at step 5. I'm getting the following warning:

The action "Rename Finder Items: Add Text" was not supplied with the required data.

Here is the workflow setup in Automator:

enter image description here

With the AppleScript below:

 on run {input, parameters}
     try
        set AppleScript's text item delimiters to "$"
        set output to {}
        repeat with anItem in input
            set the end of output to text item 2 of (anItem as text)
        end repeat
        set AppleScript's text item delimiters to {}
        return output
    on error eStr number eNum
        display dialog eStr & " number " & eNum buttons {"OK"} ¬
            default button 1 with icon caution
        set AppleScript's text item delimiters to {}
        return
    end try
end run

I thought by returning output, the next workflow would pick up and continue with the required changes. I'm seeing that is not the case.

How do I return my output so that the next workflow item can use it and continue with the file renaming?

  • You said, "I thought by returning output, the next workflow would pick up and continue with the required changes. I'm seeing that is not the case." and what's being returned by the AppleScript action is just a list of filenames, not the fully qualified pathnames, and while it could be modified to return the FQP, it still will not work with the next action because it's just a list of text strings, not filesystem objects as was passed to the AppleScript action. In the AppleScript action the FSO's were coerced to text strings in order to process as text items, i.e. (anItem as text). – user3439894 Jul 22 '17 at 16:13
  • You said "How do I return my output so that the next workflow item can use it and continue with the file renaming?", I do not know if there is a way to achieve what you are trying to do with the AppleScript action. That said, why are you modifying the original files before copying them, that just doesn't make sense. I'd swap Rename Finder Items: Add Text with Copy Finder Items. Anyway, it's not clear to me why you're going through so many different steps if all you're trying to do is rename a file e.g. a new file$SG789.jpg to SG789.jpg. – user3439894 Jul 22 '17 at 16:20
  • I'd probably opt for doing this with a bash script, IMO it would be much easier and more efficient. Another option is to get the FSO's, pass them to the AppleScript action and programmatically do everything else that needs to be done to the FSO's within the AppleScript code. (Which of course will require additional coding.) – user3439894 Jul 22 '17 at 16:20
  • @user3439894 Thank you for the explanation. It makes sense. – Yazmin Jul 22 '17 at 17:26
  • @user3439894 The reason I'm modifying the originals is because I need to keep a record of what files were processed, since my workflow strips out the name and just leaves what looks like random characters. If you have an alternative suggestion to keeping track of the original files already proccessed, I'm open to suggestions. – Yazmin Jul 22 '17 at 17:27
5

It is still not totally clear what it is you're trying to accomplish, however, here is an example workflow that you might find useful.

First Automator Workflow

  • Broken into two working segments:

First Segment:

  • Ask for Finder Items
  • Rename Finder Items: Add Text
  • Copy Finder Items
  • Run Shell Script
    • Settings: Shell: bin/bash and Pass input: as arguments
      • Replace the default code with the code below.
      • This is what's used to remove up to and including the $ in the basename of the filename.

Code for Run Shell Script action:

cd "$(dirname "$1")"
for f in "$@"
do
    mv "$(basename "$f")" "$(printf "$(basename "$f")" | sed -E 's/^.*\$//')"
done
echo "$(ls -1)"
  • Note: The echo "$(ls -1)" command is not really necessary in this use case and was used in this example just to show the filenames had been changed and to show that in the Results pane.

  • Update: New code for Run Shell Script action:

    Use this code instead of the original code, above. I've modified it to be a bit more efficient having to call basename once, instead of twice. (I have not updated the code in the Run Shell Script action in the original image below.)

    cd "$(dirname "$1")"
    for f in "$@"; do
        n="$(basename "$f")"
        mv "$n" "$(printf "$n" | sed -E 's/^.*\$//')"
    done
    echo "$(ls -1)"
    
  • Note: See also the second Automator workflow, shown after the original image below, which negates the need for the Second Segment:, as its Run Shell Script action is coded to handle the renaming that takes place in the second segment, adding prefix- and swapping -proc for -suffix.


Second Segment:

  • Ask for Finder Items
    • Options: [√] Ignore this action's input
      • By checking this option you'll see, in the image below, there is a wanted disconnect between the previous action and this one.
  • Rename Finder Items: Add Text
  • Rename Finder Items: Add Text

See the image below for other applicable settings of Automator actions, as shown in the OP.

As you can see in the image below, all actions completed successfully and did as programed, with the Results pane of each action showing the results of it's action.


First Automator Workflow



Second Automator Workflow

To be used instead of the First Automator Workflow.

  • Ask for Finder Items
  • Rename Finder Items: Add Text
  • Copy Finder Items
  • Run Shell Script
    • Settings: Shell: bin/bash and Pass input: as arguments
      • Replace the default code with the code below the image.

Second Automator Workflow

  • The results of the first three actions in this second Automator workflow are the exact same as shown in the first workflow above, therefore I've left the Results pane hidden for these actions. However, the Results pane for the Run Shell Script action shows the wanted results.

Code for Run Shell Script action in the Second Automator Workflow:

p='prefix-'
s='-suffix'
t='-proc'

cd "$(dirname "$1")"
for f in "$@"; do
    n="$(basename "$f")"
    if [[ $n =~ ^.*\$.*${t}\.${n##*.} ]]; then
        mv "$n" "${p}$(printf "${n%.*}" | sed -E -e "s/$t/$s/" -e 's/^.*\$//').${n##*.}"
    fi
done
echo "$(ls -1)"
  • Update Note: I've modified the code by placing the variable assignments outside of the for loop, as that is the only code you should need/want to modify as necessary (and they don't need to be within the for loop). I have not updated the image of the Automator workflow to reflect this update.

You could, if needed/wanted, add a different second segment to this second workflow, but the code in this Run Shell Script action eliminates the need for the two Rename Finder Items: Add Text actions that were in the second segment of the first (original), workflow.

Note: I will update this with an explanation of what the code is doing in Run Shell Script action, as soon as I can.

  • The second workflow automation definitely does the job. I also see why you felt I had too many steps, considering the rest of the renaming could have been done in a script. Thank you. – Yazmin Jul 24 '17 at 14:38
  • @Yazmin, I haven't had time yet to update the answer with an explanation of what the code in the bash script is doing, but will as soon as I get a chance. Suffice it to say you should only have to modify the value of the three variables p, s and t as needed/wanted. The remaining code should not need to be modified. – user3439894 Jul 24 '17 at 15:36
  • @Yazmin, Update Note: I've modified the code, in the second Automator workflow, by placing the variable assignments outside of the for loop, as that is the only code you should need/want to modify as necessary (and they don't need to be within the for loop). I have not updated the image of the Automator workflow to reflect this update. – user3439894 Jul 24 '17 at 16:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .