How to write script to apply character style?

Hi,
I'm on a Macbook pro and without a number pad so I can't assign a shortcut for the character styles. I did hear of a roundabout solution where you can assign a shortcut to a script which applies the character styles. Can anyone write this script?
My character style is called 'Features bold'.
I'm new to scripting, sorry if this is a dumb question.
Cheers

Hi,
1. Save this code as .jsx file inside your Script Panel folder
2. Apply a shortcut using Edit:Keyboard shortcuts... menu
Option to choose:
suggest to choose an option "context": "Text", so you can use more shortcuts which are busy with other contexts.
#target indesign
if (app.selection && app.selection[0].hasOwnProperty ("baselineShift") ) {
  var cName = "Features bold";
  var mCstyle = app.activeDocument.characterStyles.item(cName);
  if (mCstyle.isValid)
       app.selection[0].applyCharacterStyle(mCstyle);
  else
       alert ("CharStyle: " + cName + " not found");
else
  alert ("Some text supposed to be selected...");
Jarek

Similar Messages

  • InDesign crash while applying character style

    Hi all,
    A)I have problem with applying character style to selected text.
    when character style is applied to selected text indesign get crashes.
    my code snippet is as follow
    input parameters
    1.RangeData paraRange // start and end index of paragraph
    2.storyUIDRef // uidref of current story
              code snippet
              // valid text model
              InterfacePtr<ITextModel> textModel(storyUIDRef, UseDefaultIID());
              if (!textModel)
       break;
              //wax strand
              InterfacePtr<IWaxStrand> waxStrand(((IWaxStrand*)textModel->QueryStrand(kFrameListBoss, IID_IWAXSTRAND)));
              if (waxStrand == nil)
       break;
              //WaxIterator
              K2::scoped_ptr<IWaxIterator> waxIterator(waxStrand->NewWaxIterator());
              if (waxIterator == nil)
        break;
              //first wax line of paragraph
              IWaxLine* waxLine = waxIterator->GetFirstWaxLine(paraRange.Start(nil));
              //iterate each waxline
              while (waxLine != nil && (waxLine->TextOrigin() < paraRange.End()))
        //Iterate for all runs
       K2::scoped_ptr<IWaxRunIterator> waxRunIter( waxLine->QueryWaxRunIterator() ) ;
       IWaxRun * waxRun = waxRunIter->GetFirstRun();
       while (waxRun)
       int32 startPos  = waxRun->TextOrigin();
       int32 endPos = startPos + waxRun->GetCharCount();
       RangeData textRange(startPos ,endPos);
       ISelectionManager* selMgr = m_activeContext->GetContextSelection();
       if(!selMgr)
        break;
       //get valid textselection suite
      InterfacePtr<ITextSelectionSuite> suite(selMgr, UseDefaultIID());
                                  if (!suite)
       break;
                                  //select  text
      suite->SetTextSelection(storyRef, textRange, Selection::kScrollIntoView, nil);
                                  //created style from selection using  " SnpManipulate::TextStyleCreateStyleFromSelection" method.
    my code get crashed at highlighted text( int32 startPos  = waxRun->TextOrigin();).
    B)my next question is how to get textstyle ranges of a selected paragraph.
    please help me.
    Thanks
    Tahir

    A) Make sure that the all parcels of the text is composed before you iterate it, see ITextParcelListComposer. I don't quite understand what you are trying to achieve. You want to apply a character style to a range of text, but the code iterates wax lines and iterate the runs of each line  and sets the current selection to be the run on each iteration. Why not just:
    InterfacePtr<ICommand> icmd(textmodelCmds->ApplyStyleCmd(index, length,styleUID,kCharAttrStrandBoss,kFalse));
    B) Walk the kCharAttrStrands to the get information:
    InterfacePtr<IStrand> strand((IStrand*)textmodel->QueryStrand(kCharAttrStrandBoss,IStrand::kDefaultIID));
    then call stand->GetRunLength() a number of times until, you are through the selected part of the text / paragraph

  • A script to apply cell style to entire rows containing a specific text

    Hi,
    I am using InDesign CC 2014
    Does anyone know how to apply a cellstyle based on text contents found in a cell in javascript?
    I need to search the document and change cell style in entire row to "cellStyle01" if the text "someText" appear in cell.
    I've found similar script here in forum which apply table style, but don't know how to change it to apply cell style!
    var curDoc = app.activeDocument;
    var allTables = curDoc.stories.everyItem().tables.everyItem();
    app.findTextPreferences = app.changeTextPreferences = null;
    app.findTextPreferences.findWhat = "someText";
    var allFounds = allTables.findText();
    app.findTextPreferences = app.changeTextPreferences = null;
    for ( var i = 0; i < allFounds.length; i++ ) {
    var curFound = allFounds[i];
    if ( curFound.length == 1 ) {
    var curFoundParent = curFound[0].parent; 
    // curFoundParent.parent.appliedTableStyle = curDoc.tableStyles.itemByName( "tableStyle" ); 
    curFoundParent.parent.appliedCellStyle = curDoc.cellStyles.itemByName("cellStyle01"); 
    Thanks!

    Hi,
    Try this,
    var doc = app.activeDocument, 
        _tables = doc.stories.everyItem().textStyleRanges.everyItem().getElements(), 
        i, j, k, l, a, _rows,_cells, rowlen; 
    for(i =0;i<_tables.length;i++) 
        for(j =0;j<_tables[i].tables.length;j++) 
            _rows = _tables[i].tables[j].rows; 
            for(k =0;k<_rows.length;k++) 
                _cells = _rows[k].cells; 
                for(l =0;l<_cells.length;l++) 
                    if(_cells[l].contents == "someText") 
                        rowlen = _cells[l].parent.cells.length; 
                        while(rowlen--) 
                              _cells[rowlen].appliedCellStyle = doc.cellStyles.item("cellStyle01"); 
    Regards,
    Chinna

  • Applescript-Apply Character Style to Select Words in InDesign CC

    I'm trying to apply a character style to the first several words of a paragraph. The number of words varies, but it is essentially every word before a comma.
    Here's what I have so far:
    tell application "Adobe InDesign CC 2014"
      tell document 1
      tell page 2
      tell (item 1 of page items whose label is "HabitsContent")
      tell paragraph 2
      repeat with i from 1 to count of characters
      if contents of character i = "," then
      exit repeat
      end if
      end repeat
      set applied character style of characters 1 thru i to "Italic"
      end tell
      end tell
      end tell
      end tell
    end tell
    Viewing the log of the script, it finds the comma; however, the script ends up italicizing the entire paragraph. Any ideas?

    For anyone else who has a similar question, here's what works:
    tell application "Adobe InDesign CC 2014"
    tell paragraph 2 of (item 1 of page items whose label is "HabitsContent") of page 2 of document 1
    set a to paragraph 1 as list
    set a to text item 1 of a
    set old to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {","}
    set font style of characters 1 thru (count of text item 1 of a) to "Italic"
    set AppleScript's text item delimiters to old
    end tell
    end tell

  • Getting ALL applied Character Styles in a paragraph

    I'm trying to get all applied Character Styles of a paragraph in a InDesign CS2 Document.
    With myParagraph.appliedCharacterStyle I just get the first but not all applied Charcter Styles.
    Is there a possibility to get them?
    greets
    st

    If they're applied directly and not by nested style, then you should examine the appliedCharacterStyle of each TextStyleRange in the paragraph.
    If they're applied by nested style, you have to do a lot more work.
    Dave

  • [CS5 JS] Problem in Applying character styles xml tag names

    Hi,
    e.g. 1
    <italic>This is a italic text with <sup>superscript value</sup></italic>
    e.g.2
    <sup>This is a superscript text with <bold>Bold superscript</bold></sup>
    <italic>  mapped to character style "italic" and
    <sup> mapped to character style "sup"
    In my java script I already mapped the xml tags to specific character styles. In some of the cases (see e.g.1 and e.g.2) I need to map it to other character styles.
    In e.g.1 I need to apply a new character styles "italicSup" for the text "superscript value" same way
    In e.g.2 I need to apply a new character styles "BoldSup" for the text "Bold superscript".
    Can any know how to do this?
    Green4ever

    FYI, I am using
    Win Xp-SP2, CS5 7.0.3

  • Script to apply a style to currently selected text

    I can only find example scripts that work on complete files. What I want, is a script that works on currently selected text.
    If I can create a macro that applies a specific paragraph style to all currently selected paragraphs, or a character style to currently selected text, I can put that style under a shortcut key.
    That way, "emphasis" or "strong" can be put under ctrl-b, for example.
    If there is another way to achieve the same (in RH 9), I would be interested to hear it as well.
    Kind regards,
    Erwin Timmerman

    I had a quick look at Macro Express, but it seems that it only records key strokes and mouse clicks etc. This means that it only works if I have the style pod constantly open (which I do), always in the exact same position, with the same filter styles selected, and with the same CSS loaded, positioned at the same location in the scroll bar.
    Am I correct in assuming this, or is there a better way to program it?
    I am not sure this would be possible all the time. What's more, this script should work across multiple computers with different layouts, and with writers who don't really want to hack to get it working.
    With character styles there would be the possibility to automatically switch to HTML view, cut, write <span...>, paste, write </span>, go back to design view.
    But paragraph styles? That would be harder to do in HTML view, especially for multiple selected lines.

  • [JS InDesign CS3] Style groups, begone! (or: How do I take paragraph and character styles out of style groups?)

    Sorry for this question (and my terrible English, by the way), I'm a javascript noob and I know when I've reached my limits.
    Well, I'm trying to take a set of paragraph and character styles out of the style groups they are placed in, in hundreds of InDesign documents (that cannot be treated as a book). As far as I've tried (thanks to the invaluable help of previous posts in this forum) I've been able to move a style into a group and change it's position inside the group, inside the root level or even between groups. But it doesn't matter how I try, I don't know which move reference should I try in case I want to take every style out of their style group and place them after their original group folder, at the [Root] style level .
    I have tried:
    var doc=app.activeDocument;
    var pGroups=doc.paragraphStyleGroups;
    for (i=pGroups.length-1; i>=0; i--){
         var pStylesInGroup=pGroups[i].paragraphStyles;
         for (j=pStylesInGroup.length-1; j>=0; j--){
    // Here I am, trying to move a style outside the folder that contains it, and failing miserably.
              pStylesInGroup[j].move (LocationOptions.after, pGroups[i]);
    It didn't work, the script sent an invalid parameter value in the reference field, so I cant use the group folder itself as reference.
    Tried other (obviously wrong) solutions, like use the first available style as reference. Therefore, my second script was
    var doc=app.activeDocument;
    var pStyles=doc.allParagraphStyles;
    var pGroups=doc.paragraphStyleGroups;
    for (i=pGroups.length-1; i>=0; i--){
         var pStylesInGroup=pGroups[i].paragraphStyles;
         for (j=pStylesInGroup.length-1; j>=0; j--){
    // Now I try placing the styles after the [Basic Paragraph], that is, the second paragraph style in the document style list
              pStylesInGroup[j].move (LocationOptions.after, pStyles[1]);
    It didn't work either, another invalid parameter in the reference field. Similar results with my other attempts (I even tried "[Root]" as literal with similar luck).
    So, my question is: Which command (or script, in case my whole approach is utterly wrong) should I use to achieve my goal?
    Thanks in advance to whoever decides to spend more than a minute thinking about my humble worries, and my apologies for shamelessly ripping some of your lines of code for my purposes.

    Okay, tried a few things and got really weird results!
    1. You can move a style around inside its group.
    2. You can move a style out of a group "to" another, but it will appear 'inside' that style. I got my test style as a sub-item of [Basic Paragraph], using index #1. With index #0 ([No Paragraph Style]) InDesign crashed.
    3. You can duplicate the style, but then you get a copy in the same group. Still no luck.
    4. Finally! What is the parent of a paragraph style?
    Document | Application | ParagraphStyleGroup
    "Application" is easy -- that's when you make a style global. So what's the difference between 'Document' and 'paragraphStyleGroup'? Simple -- well, when you finally get it...
    pStylesInGroup[j].move (LocationOptions.AT_END, doc);
    moves the style out of the group and to the end of the list in the document. I don't think it's possible to move it directly to a specific position into the main style list -- you first have to move it out of a group, then move it around in its own list.
    Fortunately, it returns its new position as a ParagraphStyle again, so if needed, you can use
    newStyle = pStylesInGroup[j].move (LocationOptions.AT_END, doc);
    newStyle.move (LocationOptions.AFTER, pStyles[1]);
    -- I didn't really try that out, but it should work.

  • Finding MANUALLY applied character styles

    Hi all,
    We're in the process of switching to an XML workflow, and as one of our interim steps we're applying tags to paragraph and character styles in some of our documents. I'm aware of the option to map styles to tags and this should work well for paragraph styles. However, we're looking for a way to only map the character styles that have been manually applied.
    As an example, we have a paragraph style for instructions that has a bold number nested at the beginning of each paragraph. In other places, the same character style may be used to bold a specific word, but it is applied manually. Is it possible to map to tags ONLY the character styles that were applied manually? We do not want tags identifying character styles that were applied via Nested Styles or GREP Styles.
    I am assuming this would take some sort of script, which is why I'm posting in the scripting forum. Does anyone know if this is possible?
    Thanks in advance,
    Matthew

    Another InDesign oddity!
    If you fill a frame full of placeholder text and assign a "Bold" character style to, for example, a Nested Style:1 word, and a GREP style \<\w{4,6}\>, then use regular (or GREP) Find Text to search for the character style, it'll only highlight words that are at the start of the paragraph. Now that is wrong, wrong, wrong, because
    (a) it should not find any of them -- if you click your cursor into one of these bolded words, the Character Style panel says it's [None].
    or
    (b) it should find all of them, not just those that happen to be at the start of a paragraph. (BTW -- it's not the Nested Style it picks up; when using only GREP styles it'll also find words-at-the-start).
    It is funny, if things like this tickle you, that in a script "findText" works exactly the same. However: if you 'found' bold text and then interrogate what character style it has applied, it will only return your "bold" style if it was applied manually.
    See this script -- test with a placeholder text, aforementioned Nested/GREP styles, and a few hand-picked words that you applied "bold" to yourself. It will only report the ones you marked.
    app.findTextPreferences = null;
    app.findTextPreferences.appliedCharacterStyle = "bold";
    list = app.activeDocument.findText();
    r = [];
    while (list.length)
         next = list.pop();
         if (next.appliedCharacterStyle == app.activeDocument.characterStyles.item("bold"))
              r.push (next.contents);
    alert (r.join("\r"));

  • With Grep SUPERSCRİPT apply character style

    I want the script with
    contained in document SUPERSCRIPT
    apply a character style
    app.findGrepPreferences = app.changeGrepPreferences = null;      
    app.findGrepPreferences.findWhat = Position.SUPERSCRIPT; //??
    //app.changeGrepPreferences.changeTo = "";
    app.changeGrepPreferences.appliedCharacterStyle = "myChar";
    app.activeDocument.changeGrep(); 
    app.findGrepPreferences = app.changeGrepPreferences = null;

    Try this,
    app.findGrepPreferences = app.changeGrepPreferences = null;     
    app.findGrepPreferences.position = Position.SUPERSCRIPT;
    app.changeGrepPreferences.appliedCharacterStyle = "myCar1";
    app.activeDocument.changeGrep();
    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findGrepPreferences.position = Position.SUBSCRIPT;
    app.changeGrepPreferences.appliedCharacterStyle = "myCar2";
    app.activeDocument.changeGrep();
    app.findGrepPreferences = app.changeGrepPreferences = null;
    Vandy

  • Script for applying object style to only tif, or eps or...(by extension)

    We need to apply object style to only eps files in doc, or tif.... How to do this? Maybe someone have a script?
    thanks

    @kajzica – I don't have a script for that, but it is scriptable. You surely mean that you want to apply the object styles to the container frame of the images, aren't you?
    A few minutes later – try the following ExtendScript (JavaScript) code:
    You could edit the names of the two object styles at the beginning of the script code.
    OR: you could first run the script, the script will add two object styles to the document that you can edit afterwards.
    The script will sort the EPS and the TIFs from the other image types.
    Make sure that all graphics are up-to-date and linked correctly!!
    //ApplyObjectStylesTo_ContainersOf_TIF_EPS.jsx
    //Uwe Laubender
    * @@@BUILDINFO@@@ ApplyObjectStylesTo_ContainersOf_TIF_EPS.jsx !Version! Thu Dec 12 2013 13:15:30 GMT+0100
    //Edit your style names here. Change the name between the two " " only!!
    //OR: edit your object styles in InDesign after running the script.
    var styleNameForEPS = "EPS-Containers-Only";
    var styleNameForTIF = "TIF-Containers-Only";
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
    app.doScript(_ApplyObjectStylesToContainers, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Apply object styles to containers for TIF and EPS graphics");
    function _ApplyObjectStylesToContainers(){
    var d=app.documents[0];
    var allGraphicsArray = d.allGraphics;
    if(!d.objectStyles.itemByName(styleNameForEPS).isValid){
        d.objectStyles.add({name:styleNameForEPS});
    if(!d.objectStyles.itemByName(styleNameForTIF).isValid){
        d.objectStyles.add({name:styleNameForTIF});
    for(var n=0;n<allGraphicsArray.length;n++){
        //The EPS case:
        if(allGraphicsArray[n].getElements()[0].constructor.name === "EPS"){
            allGraphicsArray[n].parent.appliedObjectStyle = d.objectStyles.itemByName(styleNameForEPS);
        //The TIF case
        if(allGraphicsArray[n].getElements()[0].constructor.name === "Image" && allGraphicsArray[n].getElements()[0].imageTypeName === "TIFF"){
            allGraphicsArray[n].parent.appliedObjectStyle = d.objectStyles.itemByName(styleNameForTIF);
    }; //END: function _ApplyObjectStylesToContainers()
    Uwe

  • InDesign erroneously applies character style to imported text

    For a year, I had been importing text into InDesign with little trouble until this problem starting happening and I can't figure out why.
    Let's say I have a CS3 file with defined character and paragraph styles.
    If I draw an empty text box, InDesign shows the character style "superscript" and paragraph style "ext_all" are applied. I can select "Break Link to Style," but if I import text into that box, the "superscript" character style is applied.
    The following happens whether I create a text box first or not:
    If I import a styled Word file, all the text comes in styled "superscript" until it reaches a Word character style.
    If I import an InDesign Tagged Text file, all the text comes in styled "superscript" until it reaches a character tag.
    If I import a very simple one sentence rtf file, all the text is styled "superscript."
    Anyone run into this? I've tried googling, but can't find a discussion about it. Thanks in advance!

    Perhaps you've inadvertently set those styles as the defaults? That would happen if you click on them with nothing selected. For the current document switch to the text tool and make sure there is no cursor active or text frame selected, then check to see that the character style is set to [None] and the paragraph style to what you would like to be your default. Also check the settings in the character and paragraph panels.
    To make sure this doesn't carry forward into any more new documents, if indeed that was the problem, close all documents and repeat the above steps. With no documents open your selections will become the defaults for all new work until changed.
    Peter

  • [JS][CS3]Applying Character Style to First Word

    I am a noob as far as Javascript  is concerned, and I was hoping for a little guidance.
    Here's my scenario: I have a book that has been typeset all in one file; the main content is all in one story. I want to select the first word of every text frame on each page and apply a character style. My goal is to utilize this character style to indicate page breaks in the XML--I'll map the style to a specific tag.
    So far I have this poor specimen:
    var myDocument = app.activeDocument;
    var myPage = myDocument.pages.item(1);
    var myTextFrame = myPage.textFrames.item(0);
    if( app.activeDocument.stories[0].paragraphs[0].words[0] != null )
    myTextFrame.paragraphs.item(0).words.item(0).appliedCharacterStyle =
    myDocument.characterStyles.item('foo');
    I know, I haven't set up any sort of loop yet (which will be trickier given the fact that there will be blank versos, etc). What I'm trying to do in this is to select the first word in the first paragraph on the second page--which it does in fact do. Problem is, the first paragraph on the second page starts on the first page, so I'm selecting that a word on the first page, not the second page.
    As I said, lots of work to do but a little help on getting to the right word would be most appreciated.

    Well, you were on the right track, but testing 'myText' fails if there is no text frame on a page because it fails one line earlier
    >var myTextFrame = myPage.textFrames.item(0);
    just before this, you should test the number of text frames:
    if (myPage.textFrames.length > 0)
      ... your stuff ..
    Only use this if you are absolutely positive there is just a single text frame on each page! A slightly better way would be to always loop over each textframe:
    for (frames=0; frames<myPage.textFrames.length; frames++)
      var myTextFrame = myPage.textFrames.item(frames);
      ..etc.
    in which case you also don't have to test its length first (the loop will not be executed if there are zero text frames).
    [Techy] Since you have only one continuous story, an even better way would be to loop over the textframes of that story alone. You have to identify the story somehow, and I usually click the text cursor in the one I need:
    myStory = app.selection[0].parentStory;
    or you can rely on the fact that the story starts in the only text frame on page 1:
    myStory = app.activeDocument.pages[0].stories[0];
    (I think that oughta work.) Then, each frame of this story up till the end can be found in the array
    myStory.textContainers
    which are usually text frames. You can loop over these using
    for (frame=0; frame<myStory.textContainers.length; frame++)
      myTextFrame = myStory.textContainers[frame];
    ..etc.

  • How to spot color in a character style?

    Dear all,
    How do I use change default color in a character style to a spot color say "Red"?
    Thanks,
    Billy

    Hi
    Sorry that I didn't make myself understood. Below are the details for your information:
    We're running the XML-first workflow under InDesign CS4. When importing the XML file (with many paragraph/character styles [pstyle & cstyle]) into the pre-defined InDesign template, we find difficulties in recognizing whether there are any new paragraph/character styles that have not been defined in the existing InDesign template. It would be great if the non-defined styles could be shown in a special (spot) color, or placed into a new group. Would you please check the feasibility and advise us?
    Billy

  • How do I make no a character style color

    I gave a red color to a character style
    That you get a script
    Click I get no color
    Click again when I get back to the original color
    is it possible

    Your current code in main_pkg wouldn't throw a NO_DATA_FOUND exception. Looks like you just want to check if any row exist in the table. You don't need to use cursors for that.
    Modify your main_pkg so that it can have only the SELECT statement, OR you can just eliminate the package in this case, and move your select statement to the calling program.

Maybe you are looking for

  • Re: Column is not getting displayed in the BI report

    Hello Gurus, I have developed a BI report with more number of parameters. Case 1: When i run my report , I am able to see data for particular condition but one column doesn't show any thing. All the rows in the column are blank. Case 2 I tried to put

  • Apps from Ipod Touch won't start

    Hello everyone, Since about a day and a half I have been having problems with my Ipod Touch. All of the apps I got via the App store fail to start up. That is, they start up, the opening screen of the app quickly flashes and I am returned to the home

  • Airplay Mirroring FAIL ON NEW IPAD AND EARLY 2O11 MBP HELP! But pass with IPHONE 4 S!!!

    Ok let me start by saying I have a Iphone 4s and is mirroring perfectly with my APPLE TV but on the OTHER HAND I also have the NEW IPAD that can play youtube videos perfectly and see photos through mirroring/airplay BUT it can't scroll to my home scr

  • LR Web Galleries, need help

    I use LR's web galleries for my clients to view their proofs on line, I am on a Mac and my clients who are on a PC see white specs in the very black areas of the photos, I tested this out and it is only when viewed on a PC that this appears, not on M

  • 3D Texturing Help

    Hello I'm currently using CS6 Ext. and I'm making a gun finish for Counter-Strike:Global Offensive. There are some guides on making these but I think asking on the Adobe Forums would bring a better answer. I'd like to put a graphic I made over a 3D o