1

I'm creating an automator script that copies files from one folder to another and then runs an AppleScript to rename the file by splitting via delimiter and resaving with the second item. For example, using file names of:

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

I'm trying to split the filename by the "$" dollar sign to create the following new files:

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

However, when I run the script, I get the following error:

The action “Run AppleScript” encountered an error. Can’t get item 2 of alias "Macintosh HD:Users:downloads:Archive:a new file$SG789-PROC.jpg".

This is the code I'm running in the Run AppleScript workflow:

on run {input, parameters}
    set AppleScript's text item delimiters to "$"
    repeat with anItem in input
        set fileName to item 2 of anItem
        return fileName
    end repeat
end run

What am I doing wrong?

0

You're doing many things wrong:

  1. If you set AppleScript's text item delimiters to other then its default {}, always reset it to {} when finished with the temporary delimiter.
  2. input is a list and as such you should return a list and do it outside of the repeat loop.
  3. item 2 within the repeat loop needs to be text item 2, however, anItem within the repeat loop in this use case must also be text, so it needs to be (anItem as text).
  4. fileName needs to be a list because you are processing a list and you need to return a list.

That said, here's an example Automator workflow with an AppleScript action coded in an example manner of how I'd write the code. Note though, that while it returns a list of the filenames after the $ delimiter, I'm not sure what good it's going to do you as it all depends on additional code not currently present in this example.

Example Automator Workflow

AppleScript code for the Run AppleScript action:

on run {input, parameters}
    try
        set AppleScript's text item delimiters to {"$"}
        set theFileNameList to {}
        repeat with thisItem in input
            set the end of theFileNameList to text item 2 of (thisItem as text)
        end repeat
        set AppleScript's text item delimiters to {}
        return theFileNameList
    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
  • Ah! What you pointed out makes sense. This is my first foray into AppleScript and I was cobbling together what I understood from other examples. You mentioned you're not sure what good it's going to do me to get back the list. The next step in the workflow is a Rename Text, since I then need to add the same prefix to all files. Would returning the list be correct or is there something I'm missing? – Yazmin Jul 21 '17 at 11:50
  • @Yazmin, Since you did not include you entire Automator workflow, what type of workflow it is and all relevant information and the question you asked, "What am I doing wrong?" has been answered, I'd suggest you mark my answer appropriately as either helpful (an up-vote) and or the accepted answer (click the check mark), then in a new question continue your quest to do whatever it is you're actually trying to do. You said "The next step in the workflow is a Rename Text" and I'm unaware of that Automator action. – user3439894 Jul 21 '17 at 14:29
  • @Yazmin, I said "I'm not sure what good it's going to do you as it all depends on additional code not currently present in this example." because this answer was meant to point out what was wrong in your code, not fix a broken workflow what has not been presented in it entirety. You certainly can add additional code, as needed/required to do more that what it's presently doing but in all fairness that's outside the scope of the question you asked, "What am I doing wrong?" in reference to the AppleScript code in your Run AppleScript action that you presented. – user3439894 Jul 21 '17 at 14:29
  • @Yazmin, You asked "Would returning the list be correct or is there something I'm missing?". What's returned is dependent on what's needed and how it's going to be used and without the complete picture I can't say and why you should start a new question with includes the entire Automator workflow, type of workflow, all relevant settings and information, etc. – user3439894 Jul 21 '17 at 14:29

You must log in to answer this question.

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