Rename Script

I made a rename script for my job to batch rename specific folders.

Typically such a folder is named AB12345_789 and now I want them named like this 789_AB12345.

They are all in the same folder.

set theFolderList to choose folder with multiple selections allowed

repeat with theItem in theFolderList
	
	tell application "Finder"
		set theFolderName to name of theItem
	end tell
	
	if theFolderName contains "_" then
		
		set AppleScript's text item delimiters to "-"
		
		set theNameItems to every text item of theFolderName
		set preFix to item -1 of theNameItems
		set sufFix to item 1 of theNameItems
		
		set AppleScript's text item delimiters to ""
		
		set theNewFolderName to preFix & "_" & sufFix
		
		tell application "Finder"
			set the name of theItem to theNewFolderName
		end tell
	end if
	
end repeat

I made this script, but I’m wondering if there isn’t a better way to do this without text delimiters.

My final goal wuld be to make a studio application where you for instance say take the last x charachters and place them in front of the filename.

Any thoughts?

Hmm strange:

I just tested this script on real folders and it is bugging.

When I tested it on 5 fake folders I made for testing the script while writing it it worked just fine. Now when applying it to +/- 100 folders it does very strange things.

it gives names back like ab12345_789_ab12345_789 and also it misinterpreted “_” with “-”

Can someone help me out on this one please

Shouldn’t this be

set AppleScript's text item delimiters to "_"

Hi,

something like this


set theFolderList to choose folder with multiple selections allowed

set b to button returned of (display dialog "what do you want to do?" buttons {"Cancel", "Swap text separated by.", "Move text from position."})
set swap to b = "Swap Characters separated by."
if swap then
	set sep to text returned of (display dialog "enter delimiter" default answer "_")
else
	set sep to text returned of (display dialog "enter character position" default answer "")
	try
		set sep to sep as integer
	on error
		display dialog "entered character is no number" buttons {"Cancel"}
		return
	end try
end if

repeat with theItem in theFolderList
	set {name:Nm} to (info for theItem)
	if swap then
		if Nm contains sep then
			set {TID, text item delimiters} to {text item delimiters, sep}
			tell text items of Nm to set theNewFolderName to item 2 & sep & item 1
			set text item delimiters to TID
		else
			set theNewFolderName to ""
		end if
	else
		if sep < (count Nm) then
			tell Nm to set theNewFolderName to text sep thru -1 & text 1 thru (sep - 1)
		else
			set theNewFolderName to ""
		end if
	end if
	
	tell application "Finder"
		if theNewFolderName is not "" then set the name of theItem to theNewFolderName
	end tell
	
end repeat

Ha how stupid can one be. Thx that does the trick.

@Stefan

The delimiter selection doesn’t work in your example and the swap text button doesn’t do the trick either.

Thanks for the support though.

There is only one small error:

change the line

set swap to b = "Swap Characters separated by."

to

set swap to b = "Swap text separated by."

Thanks, I changed the title of the button and forgot to change the comparison string