I am trying to write something similar as http://forums.adobe.com/message/2866720#2866720 for the paragraph style context menu. I also would like to find out which paragraph style has been selected when the context menu opens. In the thread there is a "dirty" solution for this, the Edit and Apply options of the context menu also mention the name of the paragraph style. This works as long as the paragraph style is unique.
In my situation the paragraph style names are not always unique, the same name is used in different paragraph style groups. I thought of an even "dirtier" solution that might work for this. The basic idea behind this is to create a new text, asfaik Indesign will automatically apply the active paragraph style to this text and it is easy to grab this style. The code for this is below:
var myDoc = app.activeDocument; var myTextFrame = myDoc.textFrames.add(); myTextFrame.contents = "grab the paraStyle"; var myParaStyle = myTextFrame.paragraphs.appliedParagraphStyle; myTextFrame.remove();
In my situation this approach seems to work fine but I am wondering if there is something against creating a new textframe and removing it later? And will the new text always get the current selected paragraph style?
I finetuned the snippet above a bit further. In some cases the context menu will be activated (right mouseclick) from a style that is not currently selected. In thoses cases the example above doesn't give the correct information. In order to avoid this I am now applying the paragraph style of the current context menu to an intermediate textframe. The snippet below is placed inside the EventHandler of my custom context menu item.
var myDoc = app.activeDocument; //store current selection var mySelection = myDoc.selection; //create intermediate textframe var myTextFrame = myDoc.textFrames.add(); myTextFrame.contents = "grab the paraStyle"; myTextFrame.select(); //apply the para style of the context menu to the intermediate text var contextMenu = app.menus.item("Text Style List Context Menu"); //or "$ID/RtMenuStyleListItem" var applyMenuItem = contextMenu.menuItems.itemByID(8488).associatedMenuAction; //Apply "para style for context menu" applyMenuItem.invoke(); //grab the para style of the context menu var myParaStyle = myTextFrame.paragraphs.appliedParagraphStyle; //restore original selection and remove intermediate textframe myDoc.selection = mySelection; myTextFrame.remove();
Again this snippet seems to work fine. I realize this approach will only work for para styles in a Document object and not for default para styles defined in the Application (suggestions to solve this?). A further refinement could be to first check if a para style name is unique, if that is the case the solution of Jongware (see http://forums.adobe.com/message/2861568#2861568) can be used and an intermediate textframe is not required.
I know this is a dirty solution but are there any issues with this approach?