Folder Rename Script?

I would like to know if it is possible to do a script that could add a space (or any other static character such as an underscore) in between words that are capitalized?

Example (a folder is named):
TheRockyMountains

to turn into:
The Rocky Mountains or The_Rocky_Mountains

Sorry if this is noob basics but I have been googling for days now with no results

This should work for you

tell application "Finder"
	set theFolder to choose folder
	set theName to name of theFolder
	set theSpacer to space
	set name of theFolder to (do shell script "/usr/bin/php -r \"echo preg_replace('/(?<=[a-z])(?=[A-Z])/','" & theSpacer & "','" & theName & "');\"")
end tell

works great! thank you very much

one question … since I’ll be using this choosing multiple folders at a time is there a way to eliminate the “choose folder” prompt? I tried changing the line “set theFolder to choose folder” to “set theFolder to selection” but that didnt work.

Thanks

tell application "Finder"
	set theItems to selection
	set theSpacer to space
	repeat with anItem in theItems
		set theName to name of anItem
		set name of anItem to (do shell script "/usr/bin/php -r \"echo preg_replace('/(?<=[a-z])(?=[A-Z])/','" & theSpacer & "','" & theName & "');\"")
	end repeat
end tell

Thanks so much James!! This really is going to save me tons of time. Much appreciated

FWIW, when it’s not part of a bigger script, I just use Name Mangler for this sort of thing.

Task: Find and Replace
Find: ([a-z])([A-Z])
Check: Query is a regular expression
Replace: $1_$2

That’s asking for trouble with quotes; I would use quoted form on the variables and pass them to PHP as arguments.

I’ve been using Name Mangler for quite a few years now … I’m assuming you’re referring to using the advanced section … unfortunately I’m not grasping how to use NCDL. I’d love to but its just not clicking.

EDIT
Okay, I get what youre saying now … and it works … I would just like to understand how and why it works. :wink: