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?
Best Answer
You're doing many things wrong:
{}
, always reset it to{}
when finished with the temporary delimiter.input
is alist
and as such you shouldreturn
alist
and do it outside of therepeat
loop.item 2
within therepeat
loop needs to betext item 2
, however,anItem
within therepeat
loop in this use case must also betext
, so it needs to be(anItem as text)
.fileName
needs to be alist
because you are processing alist
and you need toreturn
alist
.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: