Batch removing part of filenames using Automator
All you are doing is returning a list of the last part of the names (if there is more than one delimiter character, the second part) to the next action.
Text item delimiters aren't really needed if you are just using the first occurrence of a character, so you can do something like:
on run {input, parameters}
set output to {}
repeat with anItem in input
set {theFolder, oldName, extension} to getNamePieces from anItem
try
set here to offset of "_" in oldName
set newName to text 1 thru (here - 1) of oldName
tell application "Finder" to set name of anItem to (newName & extension)
set end of output to (theFolder & newName & extension) as alias
on error errmess -- don't rename if error (delimiter not found, duplicate file, etc)
log errmess
set end of output to anItem as alias
end try
end repeat
return output -- for next action
end run
to getNamePieces from someItem
tell application "System Events" to tell disk item (someItem as text)
set |container| to path of container
set {|name|, extension} to {name, name extension}
end tell
if extension is not "" then
set |name| to text 1 thru -((count extension) + 2) of |name| -- just the name part
set extension to "." & extension
end if
return {container, |name|, extension}
end getNamePieces