0
[JS - CS3] Not able to add 'Superscript' style to a character within a string
New Here
,
/t5/indesign-discussions/js-cs3-not-able-to-add-superscript-style-to-a-character-within-a-string/td-p/1160595
Jun 19, 2008
Jun 19, 2008
Copy link to clipboard
Copied
Hello fellow experts...
I'm stuck with trying to change the style of a character from Normal to Superscript!
b Situation:
I have a string - 'myCourseTitle' - that has both CharacterStyles & ParagraphStyles applyed and could include the following character/Word:
> '®' (Character)
'OperateIT' (Word)
b Requirements:
I am wanting to add the style 'Superscript' to both the '®' characters and 'IT' from the words 'OperateIT', while keeping their initial CharacterStyles & ParagraphStyles!
b My Starting Block:
if (myCourseTitleField.editContents == ""){
var myCourseTitle = "-no title has been defined-";
}else{
var myCourseTitle = myCourseTitleField.editContents;
// The contents should now be checked for '®' characters and 'OperateIT'
// And set to Superscript if found!
if (myCourseTitle.indexOf("®") != 0){
alert("Registered Trade Mark '®' found in Course Title at position:"+myCourseTitle.indexOf("®")+"\rThis will be set to 'Superscript' formatting", "WARNING '®' within Course Title",)
}
}
I have tried many scripts, including the attached sample 'FindChangeByList.jsx' script - but to no avail!
Can anyone help me - point me in the right direction to start looking?
Thanks in advance
,
Lee
I'm stuck with trying to change the style of a character from Normal to Superscript!
b Situation:
I have a string - 'myCourseTitle' - that has both CharacterStyles & ParagraphStyles applyed and could include the following character/Word:
> '®' (Character)
'OperateIT' (Word)
b Requirements:
I am wanting to add the style 'Superscript' to both the '®' characters and 'IT' from the words 'OperateIT', while keeping their initial CharacterStyles & ParagraphStyles!
b My Starting Block:
if (myCourseTitleField.editContents == ""){
var myCourseTitle = "-no title has been defined-";
}else{
var myCourseTitle = myCourseTitleField.editContents;
// The contents should now be checked for '®' characters and 'OperateIT'
// And set to Superscript if found!
if (myCourseTitle.indexOf("®") != 0){
alert("Registered Trade Mark '®' found in Course Title at position:"+myCourseTitle.indexOf("®")+"\rThis will be set to 'Superscript' formatting", "WARNING '®' within Course Title",)
}
}
I have tried many scripts, including the attached sample 'FindChangeByList.jsx' script - but to no avail!
Can anyone help me - point me in the right direction to start looking?
Thanks in advance
,
Lee
TOPICS
Scripting
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Explorer
,
/t5/indesign-discussions/js-cs3-not-able-to-add-superscript-style-to-a-character-within-a-string/m-p/1160596#M276421
Jun 19, 2008
Jun 19, 2008
Copy link to clipboard
Copied
Hi Lee,
In the example, you're trying to apply InDesign formatting to a JavaScript string (from an InDesign dialog box text field). That won't work, because the JavaScript string doesn't know anything about InDesign text objects.
I'm assuming, however, that what you want is to change the formatting of the text on your page. To do that, you can use the findText method on the text, story, or document. Here's an example (the relevant part is the "mySnippet" function--the rest is just setting up an example):
Thanks,
Ole
In the example, you're trying to apply InDesign formatting to a JavaScript string (from an InDesign dialog box text field). That won't work, because the JavaScript string doesn't know anything about InDesign text objects.
I'm assuming, however, that what you want is to change the formatting of the text on your page. To do that, you can use the findText method on the text, story, or document. Here's an example (the relevant part is the "mySnippet" function--the rest is just setting up an example):
main();
function main(){
mySetup();
mySnippet();
}
function mySnippet(){
//Clear find text preferences.
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "®";
app.changeTextPreferences.appliedCharacterStyle = app.documents.item(0).characterStyles.item("superscript");
app.documents.item(0).changeText();
//Reset find/change text preferences.
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
//Reset find/change GREP preferences.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
//There's probably a way to do this in a single pass, but I'm short on time...
app.findGrepPreferences.findWhat = "\\l(?<=)IT";
app.changeGrepPreferences.appliedCharacterStyle = app.documents.item(0).characterStyles.item("superscript");
app.documents.item(0).changeGrep();
app.findGrepPreferences.findWhat = "\\l";
app.findGrepPreferences.appliedCharacterStyle = app.documents.item(0).characterStyles.item("superscript");
app.changeGrepPreferences.appliedCharacterStyle = app.documents.item(0).characterStyles.item("[None]");
app.changeGrepPreferences.position = Position.normal;
app.documents.item(0).changeGrep();
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
}
//mySetup simply takes care of setting up the example document.
function mySetup(){
var myDocument = app.documents.add();
var myPage = app.activeWindow.activePage;
//Create a text frame on page 1.
var myTextFrame = myPage.textFrames.add();
//Set the bounds of the text frame.
myTextFrame.geometricBounds = myGetBounds(myDocument, myPage);
//Fill the text frame with placeholder text.
myTextFrame.contents = TextFrameContents.placeholderText;
myTextFrame.insertionPoints.item(0).contents = "OperateIT®\r";
myTextFrame.paragraphs.item(-1).insertionPoints.item(0).contents = "OperateIT®\r";
var myHeadingStyle = myDocument.paragraphStyles.add({name:"heading"});
var mySuperscriptStyle = myDocument.characterStyles.add({name:"superscript", position:Position.superscript});
}
function myGetBounds(myDocument, myPage){
var myPageWidth = myDocument.documentPreferences.pageWidth;
var myPageHeight = myDocument.documentPreferences.pageHeight
if(myPage.side == PageSideOptions.leftHand){
var myX2 = myPage.marginPreferences.left;
var myX1 = myPage.marginPreferences.right;
}
else{
var myX1 = myPage.marginPreferences.left;
var myX2 = myPage.marginPreferences.right;
}
var myY1 = myPage.marginPreferences.top;
var myX2 = myPageWidth - myX2;
var myY2 = myPageHeight - myPage.marginPreferences.bottom;
return [myY1, myX1, myY2, myX2];
}
Thanks,
Ole
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
_Lee_Fielding_
AUTHOR
New Here
,
/t5/indesign-discussions/js-cs3-not-able-to-add-superscript-style-to-a-character-within-a-string/m-p/1160597#M276422
Jun 20, 2008
Jun 20, 2008
Copy link to clipboard
Copied
Thanks for the detailed scripting.
I have tried your script and it seems to work fine on a normal document (new blank file) or the 'mySetup' from yourself...
...but...
unfortunatly when I apply the 'mySnippet' function onto my document I get the following error:
> Error Number: 30477
Error String: Invalid value for set property 'appliedCharacterStyle', Expected String, CharacterStyle or Nothing.......
If I copy the text out of my document and pasted it into a blank new file it works????
BUT - the origional 'Character Styling' has been changed to the newly added 'superscript' Character Style!
This isn't good for me as I need the text to keep the origional Character Style - it is used (Character Style) to search for in other scripts, so when I change the style - the search will not find it any more!
:-(
What is happening - and why does he not like the formatting im my Document?
Thanks in advance.
Lee
I have tried your script and it seems to work fine on a normal document (new blank file) or the 'mySetup' from yourself...
...but...
unfortunatly when I apply the 'mySnippet' function onto my document I get the following error:
> Error Number: 30477
Error String: Invalid value for set property 'appliedCharacterStyle', Expected String, CharacterStyle or Nothing.......
If I copy the text out of my document and pasted it into a blank new file it works????
BUT - the origional 'Character Styling' has been changed to the newly added 'superscript' Character Style!
This isn't good for me as I need the text to keep the origional Character Style - it is used (Character Style) to search for in other scripts, so when I change the style - the search will not find it any more!
:-(
What is happening - and why does he not like the formatting im my Document?
Thanks in advance.
Lee
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Explorer
,
LATEST
/t5/indesign-discussions/js-cs3-not-able-to-add-superscript-style-to-a-character-within-a-string/m-p/1160598#M276423
Jun 20, 2008
Jun 20, 2008
Copy link to clipboard
Copied
Hi Lee,
Ah, I see. Here's a new "mySnippet":
Thanks,
Ole
Ah, I see. Here's a new "mySnippet":
function mySnippet(){
//Clear find text preferences.
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "®";
app.changeTextPreferences.appliedCharacterStyle = app.documents.item(0).characterStyles.item("superscript");
app.documents.item(0).changeText();
//Reset find/change text preferences.
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
//Reset find/change GREP preferences.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
//There's probably a way to do this in a single pass, but I'm short on time...
//First step: change any lowercase character followed by "IT" to superscript.
app.findGrepPreferences.findWhat = "\\l(?lt;=)IT";
app.changeGrepPreferences.position = Position.superscript;
app.documents.item(0).changeGrep();
//Second step: change any lowercase character formatted as superscript to normal.
app.findGrepPreferences.findWhat = "\\l";
app.changeGrepPreferences.position = Position.normal;
app.documents.item(0).changeGrep();
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
}
Thanks,
Ole
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

