GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Rename to and from CamelCase

jscript
scripting
regex

#1

Rename TO CamelCase:

EDIT: greatly simplified the code to make it more useful as a learning tool.

The following rename script is fully explained on this page about DO12 rename scripting.

The script's goal is to convert names such as the-empire-strikes-back to TheEmpireStrikesBack, but it's really an excuse to showcase a cool DO12 feature that allows you to add your own rename parameters in the rename panel's chrome.


The preset is attached (unzip before importing).
Comments welcome!

The code:

// This optional function handles parameter input
// the fields are added to the rename interface
function OnGetCustomFields(getFieldData)
{
	// define checkboxes with an initial state
	getFieldData.fields.capitalizeFirstLetter = false
	getFieldData.fields.splitOnDashes = true
	getFieldData.fields.splitOnUnderscores = true
	getFieldData.fields.splitOnDots = true
	getFieldData.fields.splitOnSpaces = false

	// set the labels for the checkboxes
	getFieldData.field_labels("capitalizeFirstLetter") = "CapitalizeFirstLetter"
	getFieldData.field_labels("splitOnDashes") = "split-on-dashes"
	getFieldData.field_labels("splitOnUnderscores") = "split_on_underscores"
	getFieldData.field_labels("splitOnDots") = "split.on.dots"
	getFieldData.field_labels("splitOnSpaces") = "split on spaces"
}

// This is the renaming function
function OnGetNewName(getNewNameData)
{
	// these two fields inherit any renames already done by other
	// means (e.g. capitalization)
	var stem = getNewNameData.newname_stem
	var ext = getNewNameData.newname_ext

	// build a delimiter string reflecting the state of the checkboxes
	var delimiters = (getNewNameData.custom.splitOnDashes ? "-" : "") +
					 (getNewNameData.custom.splitOnUnderscores ? "_" : "") +
					 (getNewNameData.custom.splitOnDots ? "." : "") +
					 (getNewNameData.custom.splitOnSpaces ? " " : "")

	if (delimiters == '') return false
	
	var delimiterRegex = new RegExp("[" + delimiters + "]", 'g')
	var newstem = stem.replace(delimiterRegex, '')
	if (! getNewNameData.custom.capitalizeFirstLetter) {
		newstem = newstem.slice(0,1).toLowerCase() + newstem.slice(1)
		}
	return newstem + ext
}

CamelCase.zip (994 Bytes)


Rename FROM CamelCase:

See post #7, below.


Button: cbr to cbz
DO12 - Rename Script: custom label bug?
#2

Hi playful,

I used a regex for similar function before, but your script is more comfortable (esp. the own option-buttons).

As I needed to change the script a bit for my needs and really poor in scripting, two questions:

  1. How can I remove 2 spaces?
  2. I want to keep " - " with a space before and after.

#3

@Sasa It's a pleasure to help. :slight_smile:
Attaching a new preset handling:

  1. The dash modification
  2. Multiple spaces (thought this worked before but I may be looking at the wrong version)

If I've misunderstood, please let me know.


CamelCase Sasa.zip (1.05 KB)


#4

Wow, that was fast. Thank you all, today renaming got more comfortable for me. :slight_smile:


#5

@Sasa always happy to help if you need regex tips, it's almost a hobby. :slight_smile:


#6

Great script. I didn't know you could expand the rename window with your own options.

I came here when I searched for a script that does the opposite. I have a lot files with a PascalCase name. I want to split these names at the capital letters. Preferably with a space as a delimiter (but I now know I can add checkboxes for other options).
Can you help me out how to do that, based on this script?


#7

Hi Amorax,

A simple regex should do it:

Mode: Regular Expressions
Old name: (.*[a-z])([A-Z].*)#
New name: \1 \2
:ballot_box_with_check: Case sensitive
:ballot_box_with_check: Ignore extension

Split CamelCase.orp (201 Bytes)



What happened to the 'Add Space Before Capitals' rename script?
#8

:thumbsup: Thanks for your quick respond.