Smart Title Script
Hello,
This is a script from a previous post in 2011. It works great! But I have the following request: How to apply it to a specific paragraph style like "Heading 1" instead of selecting the text?
//DESCRIPTION: Converts selected text to title case smartly
var ignoreWords = ["a", "an", "and", "the", "to", "with", "in", "on", "as", "of", "or", "at", "into", "that",
"by", "from", "their", "then", "for", "are", "not","cannot", "be", "is", "which", "can"];
var intCaps = ["PineRidge","InDesign","NJ","UMC", "FCCLA", "SkillsUSA", "d’Oeuvres", "VAT", "VIES",];
// or by creating text files named ignoreWords.txt and intCaps.txt in the same folder as the script
ignoreWords = getIgnoreFile(ignoreWords);
intCaps = getIntCaps(intCaps);
try {
myText = app.selection[0].texts[0].contents;
} catch(e) {
exit();
}
theWordRanges = myText.split("/");
for (var i = theWordRanges.length - 1; i >= 0; i--) {
theWords = theWordRanges.toLowerCase().split(" ");
//First word must have a cap, but might have an internal cap
myNewText = "";
for (var j = 0; theWords.length > j; j++) {
k = isIn(intCaps,theWords)
if (k > -1) {
myNewText = myNewText + intCaps + " ";
continue;
} else {
if ((isIn(ignoreWords,theWords) > -1) && (j != 0)) {
myNewText = myNewText + theWords + " ";
} else {
myNewText = myNewText + InitCap(theWords) + " ";
}
}
}
theWordRanges = myNewText.substring(0,myNewText.length - 1)
}
app.selection[0].texts[0].contents = theWordRanges.join("/");
// +++++++ Functions Start Here +++++++++++++++++++++++
function getIgnoreFile(theWords) {
var myFile = File(File(getScriptPath()).parent.fsName + "/ignoreWords.txt");
if (!myFile.exists) { return theWords }
// File exists, so use it instead
myFile.open("r");
var importedWords = myFile.read();
myFile.close();
return importedWords.split("\n"); // Could filter these, but what's the point?
}
function getIntCaps(theWords) {
var myFile = File(File(getScriptPath()).parent.fsName + "/intCaps.txt");
if (!myFile.exists) { return theWords }
// File exists, so use it instead
myFile.open("r");
var importedWords = myFile.read();
myFile.close();
return importedWords.split("\n"); // Could filter these, but what's the point?
}
function getScriptPath() {
// This function returns the path to the active script, even when running ESTK
try {
return app.activeScript;
} catch(e) {
return e.fileName;
}
}
function isIn(aList,aWord) {
for (var i = 0; aList.length > i; i++) {
if (aList.toLowerCase() == aWord) {
return i;
}
}
return -1;
}
function InitCap(aWord) {
if (aWord.length == 1) {
return (aWord.toUpperCase());
}
return (aWord.substr(0,1).toUpperCase() + aWord.substring(1,aWord.length))
}
