Remove delimiter section from filename with Automator 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?
You're doing many things wrong:


If you set AppleScript's text item delimiters to other then its default {}, always reset it to {} when finished with the temporary delimiter.
input is a list and as such you should return a list and do it outside of the repeat loop. 
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). 
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.



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 &amp; " number " &amp; eNum buttons {"OK"} ¬
            default button 1 with icon caution
        set AppleScript's text item delimiters to {}
        return
    end try
end run