How to rename more condition by applesceipt?

Hi guy,
I’m just a user, not programmer. I know a little bit of logic. I can use replace text function in Automator, more than that I can’t. Already search on internet but it doesn’t work for me and I don’t know why. In all forum there have no any topics look like what I want too and here what I want to script for it.

I’d download hundred of files they came with name and random string and nearly same pattern like this.

a-place-beyond-belief_HQ8yyBoiS-SBA-300416819.xxx
before-the-fight_BU7M86YjB-SBA-300416679.xxx
pleasant-afternoon_SBA-300241220.xxx

xxx is an extension

I want to remove string from “_” to last string of filename.

Thanks for your help.
Frank

Hi
Cud use below handler

where theText = file name
theSearchString = “_”
theReplacementString = “”

→ theReplacementString cud space or anything u fancy


-- Usage
set theFileName to "a-place-beyond-belief_HQ8yyBoiS-SBA-300416819.xxx"

findAndReplaceInText(theFileName, "_", "")

-- →Output = "a-place-beyond-beliefHQ8yyBoiS-SBA-300416819.xxx"

-- Handler
on findAndReplaceInText(theText, theSearchString, theReplacementString)
    set AppleScript's text item delimiters to theSearchString
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to theReplacementString
    set theText to theTextItems as string
    set AppleScript's text item delimiters to ""
    return theText
end findAndReplaceInText

Thanks You, One208

I’m so sorry. It’s my miss communication. Actually I want to get result as filename.xxx

a-place-beyond-belief_HQ8yyBoiS-SBA-300416819.xxx -->> a-place-beyond-belief.xxx

My request is not clear. Please do it for me again.

Thanks

property nameExtension : ".xxx"

set theFileName to "a-place-beyond-belief_HQ8yyBoiS-SBA-300416819.xxx"

set theOffset to offset of "_" in theFileName
set newName to text 1 thru (theOffset - 1) of theFileName & nameExtension

log newName -- "a-place-beyond-belief.xxx"

Thanks for your script, wch1zpink

Why cannot get result?

What I’ve done wrong?
How to use this script in Automator?

please let me know.

Thanks

I changed the [format]log[/format] command to [format]return[/format]

property nameExtension : ".xxx"

set theFileName to "a-place-beyond-belief_HQ8yyBoiS-SBA-300416819.xxx"

set theOffset to offset of "_" in theFileName
set newName to text 1 thru (theOffset - 1) of theFileName & nameExtension

return newName -- "a-place-beyond-belief.xxx"

Or you can use this to select all of the files you want to rename and the files will all be renamed in one shot.

property nameExtension : ".xxx" -- Change This To The Real File Extension

set theFiles to choose file with multiple selections allowed -- Choose Files To Be Re-Named

repeat with thisFile in theFiles
	tell application "System Events" to set theFileName to name of thisFile
	
	set theOffset to offset of "_" in theFileName
	set newName to text 1 thru (theOffset - 1) of theFileName & nameExtension
	
	tell application "System Events" to set name of thisFile to newName
end repeat

Great job!

Thanks you so much, wch1zpink.

If you don’t mind, please help me one more times.

As screen captured.

how to make it work in Automator.
I only know that filename (in highlight) need to change something to get filename form above instead.

Thanks

I really happy. thanks again for your help, wch1zpink.

:smiley: