Copy link to clipboard
Copied
Having a problem here.
When multiple paragraphs are selected the start of a paragraph is set to a lowercase
how can I fix this?
//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))
}
Hi,
Looks like it is a 'paragraph mark' problem which can be seen as 1st char in some strings.
modify these two lines:
...
theWordRanges = myText.split("\r");
...
app.selection[0].texts[0].contents = theWordRanges.join("\r");
...
Jarek
Copy link to clipboard
Copied
Hi,
Looks like it is a 'paragraph mark' problem which can be seen as 1st char in some strings.
modify these two lines:
...
theWordRanges = myText.split("\r");
...
app.selection[0].texts[0].contents = theWordRanges.join("\r");
...
Jarek
Copy link to clipboard
Copied
Worked great - thanks
Copy link to clipboard
Copied
Hello,
How can we apply this script to a specific paragraph style like "Heading 1".
Right now works on selection.
try {
myText = app.selection[0].texts[0].contents;
} catch(e) {
exit();
Copy link to clipboard
Copied