Saturday, November 27, 2021
  • Index
  •  » Automator
  •  » How to rename more condition by applesceipt?

#1 2021-07-20 01:00:53 am

Fsmith
Member
Registered: 2021-07-19
Posts: 4

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

Offline

 

#2 2021-07-20 04:27:26 am

One208
Member
Registered: 2021-03-15
Posts: 42

Re: How to rename more condition by applesceipt?

Hi
Cud use below handler

where theText = file name
theSearchString = "_"
theReplacementString = ""

→ theReplacementString cud space or anything u fancy

Applescript:


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

Last edited by One208 (2021-07-20 04:31:04 am)

Offline

 

#3 2021-07-20 07:33:27 am

Fsmith
Member
Registered: 2021-07-19
Posts: 4

Re: How to rename more condition by applesceipt?

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

Offline

 

#4 2021-07-20 12:07:40 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 69

Re: How to rename more condition by applesceipt?

Applescript:

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"

Offline

 

#5 2021-07-20 12:21:55 pm

Fsmith
Member
Registered: 2021-07-19
Posts: 4

Re: How to rename more condition by applesceipt?

Thanks for your script, wch1zpink


Why cannot get result?

51325061544_aeac658792.jpg


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

please let me know.

Thanks

Offline

 

#6 2021-07-20 12:37:34 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 69

Re: How to rename more condition by applesceipt?

Fsmith wrote:

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

log

command to

return

Applescript:

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.

Applescript:

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

Last edited by wch1zpink (2021-07-20 01:03:30 pm)

Offline

 

#7 2021-07-20 01:05:18 pm

Fsmith
Member
Registered: 2021-07-19
Posts: 4

Re: How to rename more condition by applesceipt?

Great job!

Thanks you so much, wch1zpink.

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

As screen captured.

51325403790_9deb7ee83f_c.jpg

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.

big_smile

Last edited by Fsmith (2021-07-21 05:27:54 am)

Offline

 
  • Index
  •  » Automator
  •  » How to rename more condition by applesceipt?

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)