Remove delimiter section from filename with Automator

Discussion in 'Mac Programming' started by ywickham, Jul 20, 2017.

  1. ywickham macrumors newbie

    Joined:
    Jul 20, 2017
    #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:

    Code:
    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?
     
  2. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    The error message is telling you that your items are aliases, not text. You also have to actually get the text items, so your script would look something like:

    Code:
    on run {input, parameters}
        set output to {} -- this will be the output
        set tempTID to AppleScript's text item delimiters
        set AppleScript's text item delimiters to "$"
        repeat with anItem in input
            set namePieces to text items of (anItem as text)
            set the end of output to item 2 of namePieces
        end repeat
        set AppleScript's text item delimiters to tempTID
        return output
    end run
     
  3. ywickham thread starter macrumors newbie

    Joined:
    Jul 20, 2017

Share This Page