Applescript / Automator Renaming a File

Discussion in 'Mac Programming' started by itsmrjon, Aug 4, 2011.

  1. itsmrjon macrumors regular

    Joined:
    Jun 11, 2011
    Location:
    Chicago
    #1
    Let me begin by thanking you for taking a look at this =)

    I'm attempting to write an app for myself that renames/organizes my TV show library (EyeTV is rather bad at this after exporting), however I have run into a minor glitch that for the life of me I haven't been able to figure out.

    This applescript is part of a larger automator workflow. However this section is intended to rename the TV Show as such:
    Name of Show - S01E01.extension
    From:
    Name of Show - Episode Name.extension

    This applescript is contained within automators "Run Applescript"
    Code:
    on run {input, parameters}
    	
    	tell application "Finder"
    		set theFile to input
    		set filePath to theFile as text
    		set TID to AppleScript's text item delimiters
    		set AppleScript's text item delimiters to ":"
    		set fileName to last text item of filePath
    		set AppleScript's text item delimiters to "."
    		set baseName to text item 1 of fileName
    		set extName to text item 2 of fileName
    		
    		set AppleScript's text item delimiters to {"s01", "s02", "s03", "s04", "s05", "s06", "s07", "s08", "s09", "s10", "s11", "s12", "s13", "s14", "s15", "s16", "s17", "s18", "s19", "s20", "s21", "s22"}
    		
    		set showName to text item 1 of baseName
    		
    		set AppleScript's text item delimiters to {"e01", "e02", "e03", "e04", "e05", "e06", "e07", "e08", "e09", "e10", "e11", "e12", "e13", "e14", "e15", "e16", "e17", "e18", "e19", "e20", "e21", "e22", "e23", "e24", "e25", "e26", "e27", "e28", "e29", "e30"}
    		
    		set episodeName to last text item of baseName
    		
    		set AppleScript's text item delimiters to episodeName
    		set trimmedName to first text item of baseName
    		
    		set AppleScript's text item delimiters to showName
    		set seasonEpisode to last text item of trimmedName
    		
    		set finalName to showName & "- " & seasonEpisode & "." & extName
    		set AppleScript's text item delimiters to TID
    		set name of theFile to finalName [COLOR="Red"]<-- this is the line giving me problems[/COLOR]
    		
    	end tell
    	
    	return finalName
    end run
    Now as it is written, finalName is returned as I wish it to be (in the format mentioned above). However the line intended to rename the file is giving me an error

    Code:
    Can't set name of {alias "SSD:....."} to "Alphas - S01E01.m4v". (-10006)
    Could someone please tell me what I am doing incorrectly with the rename?
    If you could suggest a correction that would work I would appreciate it.

    Also, feel free to use this for personal use, I know it's not the best code (quite frankly I think it's horrendous because this is the second applescript I've ever written), but if you find it useful just please cite me =)

    Thanks again!
     
  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    The input parameter is a list (even if there is only one item), so normally you would repeat through the items in that list. If your workflow really only handles one input item, then just coerce the item in the list (like you did when setting the filePath), e.g.

    Code:
    set theFile to the first item of the input
     
  3. itsmrjon thread starter macrumors regular

    Joined:
    Jun 11, 2011
    Location:
    Chicago
    #3
    oh my god I love you right now!

    Thank you sooo much!
     

Share This Page