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.
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
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"
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