Copy link to clipboard
Copied
Hello,
Does invoking a menu with the menu string work in any language? Example.
$ID/Override All Master Page Items
P.
I should have added:
It should work as long as you provide the locale-independent form of the string.
Sometimes simply adding $ID/ in front of the English menu name does not work or is not enough.
Better look for the title string of a menu and check with:
app.findKeyStrings("menu-title-name");
for the "universal" form(s) of the string.
Regards,
Uwe
Copy link to clipboard
Copied
Hi Pickory,
it should, if the menu is available in the situation you want to use it.
E.g "Override All Master Page Items" is not available if no document is open.
See for example:
var menuAction = app.menuActions.itemByName("$ID/Override All Master Page Items");
for(x in menuAction)
{
$.writeln(x+"\t"+menuAction
);
};
// CASE 1 No document open with my German InDesign
/*
name Alle Musterseitenobjekte übergehen
title Alle Musterseitenobjekte übergehen
area Bedienfeldmenüs:Seiten
enabled false
checked false
id 6164
label
isValid true
parent [object Application]
index 101
properties [object Object]
events [object Events]
eventListeners [object EventListeners]
isValid true
*/
// CASE 2 Document open with my German InDesign.
/*
name Alle Musterseitenobjekte übergehen
title Alle Musterseitenobjekte übergehen
area Bedienfeldmenüs:Seiten
enabled true
checked false
id 6164
label
isValid true
parent [object Application]
index 101
properties [object Object]
events [object Events]
eventListeners [object EventListeners]
isValid true
*/
So before using it you could look after the value of property enabled.
Maybe also isValid if you are not sure if a specific InDesign version does provide the menu.
var menuAction = app.menuActions.itemByName("$ID/Override All Master Page Items");
if(menuAction.enabled){menuAction.invoke()};
( Just for lurkers: InDesign Server does not support menuActions )
Regards,
Uwe
Copy link to clipboard
Copied
I hope you asking the menu action work in all scripting language?
Copy link to clipboard
Copied
I should have added:
It should work as long as you provide the locale-independent form of the string.
Sometimes simply adding $ID/ in front of the English menu name does not work or is not enough.
Better look for the title string of a menu and check with:
app.findKeyStrings("menu-title-name");
for the "universal" form(s) of the string.
Regards,
Uwe
Copy link to clipboard
Copied
Hi, I have some trouble trying to use universal menu strings:
Some confrere submitted me a script idea, showing me a manual trick that he had designed to selectively get rid of specific unwanted style replacements in paragraphs while preserving other replacements:
http://robinfredericf.free.fr/misc/wadym-s_Idea.avi.mp4
http://robinfredericf.free.fr/misc/wadym-s_idea.png
I was interested in automating it in a script but his method uses the command "Redefine style" that doesn't exists in the InDesigns ExtendScript API, so I tried to «invoke()» it, following explanations that I read on adobe forum topics such as
https://forums.adobe.com/thread/1961958
http://kasyan.ho.com.ua/open_menu_item.html
https://forums.adobe.com/thread/892886
To get the required command to redefine a paragraph style from the selection, in a international reliable way, I decided at once to avoid to get the menuAction by its id or index because it seemed hazardous, those numbers could be assigned to totally different actions in different InDesign version, so i tried:
var redefineParagraphStyle = app.menus.item('$ID/ParaStylePanelPopup').menuItems.item('$ID/Redefine Style');
redefineParagraphStyle.associatedMenuAction.invoke();
[including jsx and demo idml file:]
http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd002.zip
http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd002-en.zip
I expected that the use of "$ID/" english strings would prevent the localization problems but unfortunately I was surprised when my colleague told me that he got the error «ReferenceError: Object is invalid»
So then I tried:
var redefineParagraphStyle = app.menus.item('$ID/ParaStylePanelPopup').menuItems.item('$ID/RedefineStyle');
var redefineParagraphStyle = app.menus.item('$ID/ParaStylePanelPopup').menuItems.itemByID(8458);
var redefineParagraphStyle = app.menus[113].menuItems[3];
http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd002a.zip
All methods worked on my side, tested on French localized versions of InDesign CS5 & CC 2015.4 under MacOSX 10.10.5 & Windows 7
But still the same "Object is invalid" error on my Ukrainian colleague's side, who tested the scripts on Russian localized versions of CS5.5 & CC 2015.2 (11.2.0.100х64) under Windows 7.
Copy link to clipboard
Copied
I haven't tested anything, but IIRC some menu items are only valid when the relevant panel is open. So if the paragraph styles panel is closed on your friend's computer, he may get an error.
Ariel
Copy link to clipboard
Copied
Hi Ariel,
in this case the associatedMenuAction should also work, if the panel is not open.
However it can only work, if it is enabled.
And it is only enabled if:
1. Text is selected
&&
2. Text is showing overrides on the applied paragraph style
Regards,
Uwe
Copy link to clipboard
Copied
Laubender wrote
in this case the associatedMenuAction should also work, if the panel is not open.
However it can only work, if it is enabled.
And it is only enabled if:1. Text is selected
&&2. Text is showing overrides on the applied paragraph style
Okay, sounds right! I didn't go into the specific details of the case as you have (thanks for that!) -- but the principle is important: There is such a thing as a menuAction not being enabled depending on the UI configuration (either a closed panel, or as you rightly point out in this case, no text selected, etc.). So this could be Robin's problem.
Ariel
Copy link to clipboard
Copied
Some other details:
// My German string for "Redefine Style":
var localeKeyString = "Format neu definieren";
var foundKeyStrings = app.findKeyStrings(localeKeyString);
/*
$ID/Redefine Style
$ID/RedefineStyle
$ID/RedefineObjectStyles
$ID/ObjStylesContextRedefine
*/
// My German InDesign CS6 lists 9 entries of a menu action named "$ID/Redefine Style".
// The same 9 entries are also listed with: "$ID/RedefineStyle", "$ID/RedefineObjectStyles" and "$ID/ObjStylesContextRedefine"
// Results listed below when a document is open and nothing is selected:
var menuActions = app.menuActions.itemByName( foundKeyStrings[0] ).getElements();
for(var n=0;n<menuActions.length;n++)
{
$.writeln(menuActions
.enabled+"\t"+"\t"+menuActions .id+"\t"+menuActions .area+"\t"+menuActions .title); };
/*
false 8458 Bedienfeldmenüs:Absatzformate Format neu definieren
false 8459 Bedienfeldmenüs:Zeichenformate Format neu definieren
false 8486 Bedienfeldmenüs:Absatzformate Format neu definieren
true 113158 Bedienfeldmenüs:Objektformate Format neu definieren
false 113176 Bedienfeldmenüs:Objektformate Format neu definieren
false 132102 Bedienfeldmenüs:Tabellenformate Format neu definieren
false 132126 Bedienfeldmenüs:Zellenformate Format neu definieren
false 132151 Bedienfeldmenüs:Tabellenformate Format neu definieren
false 132152 Bedienfeldmenüs:Zellenformate Format neu definieren
Bedienfeldmenüs > panel menus
Format neu definieren > Redefine Style
Absatzformate > paragraph styles
Zeichenformate > character styles
Objektformate > object styles
Tabellenformate > table styles
Zellenformate > cell styles
*/
Hope, that helps…
Uwe
Copy link to clipboard
Copied
On my side it appears to work as well when the paragraph styles panel is closed.
Copy link to clipboard
Copied
Hi Robin,
didn't you post something yesterday?
How did it go without using doScript() ?
Regards,
Uwe
Copy link to clipboard
Copied
So we tried without doScript: http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd003a.zip
// indd002-MinimumVersion-2.jsx
#target "InDesign";
#targetengine reformatToAppliedStyle
reformatToAppliedStyle();
function reformatToAppliedStyle()
{
if(app.documents.length == 0){ return; } // Do we need an alert here? No.
if(app.selection.length != 1){ return; } // Do we need an alert here? No.
if(!app.selection[0].hasOwnProperty("baselineShift"))
{ alert("Error: No text selected."); return; }
if(app.selection[0].styleOverridden == false)
{ alert("Error: No style override detected."); return; }
var menuItem = app.menus.item("$ID/ParaStylePanelPopup").menuItems.item("$ID/Redefine Style");
if(menuItem.isValid == false)
{ alert("Error: menuItem Redefine Style cannot be found."); return; }
if(menuItem.enabled == false)
{ alert("Error: menuItem Redefine Style is not enabled."); return; }
var myStyle = app.activeDocument.selection[0].insertionPoints[0].appliedParagraphStyle;
/*
// [ No Paragraph Style ] used.
NOTE: The following if statement should never be executed
if [ No Paragraph Style ] is really used on selected text,
because "Redefine Style" is never enabled with [ No Paragraph Style ]
and we already have an error condition for that case.
Nevertheless:
You cannot duplicate [ No Paragraph Style ]
*/
if(myStyle.index == 0)
{ alert("Error: No custom paragraph style applied: [ No Paragraph Style ]"); return; }
// [ Basic Paragraph Style ] used.
// You can duplicate that style, but you cannot remove the original.
// You cannot rename the duplicate to that style.
if(myStyle.index == 1)
{ alert("Error: No custom paragraph style applied: [ Basic Paragraph Style ]"); return; }
// FWIW: Styles cannot be named with [ or ] in their names.
// [ and ] are restricted to the names of the base styles.
// Maybe there are other special characters that are not working?!
var myStyleName = myStyle.name;
var myStyleCopy = myStyle.duplicate();
menuItem.associatedMenuAction.invoke();
myStyle.remove(myStyleCopy);
myStyleCopy.name = myStyleName;
}
Result:
So it says:
JavaScript Ошибка! = JavaScript Error!
Номер ошибки = Error number: 45
Сообщение об ошибке = Error report: Object is invalid
Engine: reformatToAppliedStyle
Файл = File: C:\Users\Вадим\AppData\Roaming\Adobe\InDesign\Version 11.0
\ru_RU\Scripts\Scripts Panel\wadyms-paragraphstyle-trick_indd003a.jsxСтрока = Line: 20
Источник = Source: var menuItem = app.menus.item
("$ID/ParaStylePanelPopup").menuItems.item("$ID/Redefine Style");
Copy link to clipboard
Copied
Hi Frédéric,
I spent a couple of hours on this but now I think I know why Wadym encounters the problem.
This is a bug in Russian locale. (so far I tested it only in CC 2015: the same version as Wadym has).
I tried to reference the menu item by index -- in my copy it's 97 -- and it returned quite a strange result:
var menuItem = app.menus.itemByName("$ID/ParaStylePanelPopup");
Doesn't return a valid reference:
In English locale everything is OK:
I tested it with some other locales -- Arabic, Chinese, Spanish, Polish -- and it works OK, so it happens only in Russian.
I suggest Wadym and everybody with Russian Indy not to use the menu item reference but use menuActions.itemByID instead.
Here's my version:
#target "InDesign";
#targetengine reformatToAppliedStyle
reformatToAppliedStyle();
function reformatToAppliedStyle() {
try {
if(app.documents.length == 0){ return; } // Do we need an alert here? No.
if(app.selection.length != 1){ return; } // Do we need an alert here? No.
if(!app.selection[0].hasOwnProperty("baselineShift"))
{ alert("Error: No text selected."); return; }
if(app.selection[0].styleOverridden == false)
{ alert("Error: No style override detected."); return; }
var myStyle = app.activeDocument.selection[0].insertionPoints[0].appliedParagraphStyle;
/*
// [ No Paragraph Style ] used.
NOTE: The following if statement should never be executed
if [ No Paragraph Style ] is really used on selected text,
because "Redefine Style" is never enabled with [ No Paragraph Style ]
and we already have an error condition for that case.
Nevertheless:
You cannot duplicate [ No Paragraph Style ]
*/
if(myStyle.index == 0)
{ alert("Error: No custom paragraph style applied: [ No Paragraph Style ]"); return; }
// [ Basic Paragraph Style ] used.
// You can duplicate that style, but you cannot remove the original.
// You cannot rename the duplicate to that style.
if(myStyle.index == 1)
{ alert("Error: No custom paragraph style applied: [ Basic Paragraph Style ]"); return; }
// FWIW: Styles cannot be named with [ or ] in their names.
// [ and ] are restricted to the names of the base styles.
// Maybe there are other special characters that are not working?!
var myStyleName = myStyle.name;
var myStyleCopy = myStyle.duplicate();
app.menuActions.itemByID(8458).invoke();
myStyle.remove(myStyleCopy);
myStyleCopy.name = myStyleName;
}
catch(err) {
alert(err.message + ", line: " + err.line);
}
}
Regards,
Kasyan
Copy link to clipboard
Copied
Thank you very much! So let's try it http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd007.ks01.zip
Copy link to clipboard
Copied
I owe you all an answer, I was very busy since then (happy new year by the way!) so yes — using menuActions.itemByID instead of menu item reference does make this script finally working on all Wadym's InDesign localized versions as he confirmed to me on 3rd of January. My first question (or my primary goal, as we don't use universal menu strings anymore) is basically solved since the manual trick is now successfully scripted but I am still working to improve it for some specific cases that he submitted me, where some unwanted replacement are not suppressed neither with the manual method nor with its equivalent scripted version, or where it suppresses replacements that should be preserved such as a "bold" attribute applied using a character style causing the overridden "bold" attributes disappear elsewhere.
Copy link to clipboard
Copied
This improved version http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd009.zip tested by Wadym for about a month appears to work even on those specific cases (showed in the included test file). I would be happy to thank you all for help in the article or at least in the source code of the script when publishing it on Scriptopedia.org 🙂 The current version enables as well to work on the [Basic Paragraph] style but I think I won’t boast too much about it because I had to create an alternate maybe less reliable method for the [Basic Paragraph] so I think I will rather let the most curious users discover this possibility as an undocumented feature.
Copy link to clipboard
Copied
Hi,
try this:
var menuItem =
app.menus.itemByName("$ID/ParaStylePanelPopup").
menuItems.itemByName("$ID/Redefine Style");
if(menuItem.enabled)
{
menuItem.associatedMenuAction.invoke();
};
Regards,
Uwe
Copy link to clipboard
Copied
Thank you to answer so quickly,
Checking if the menuItem is enabled before invoking it is basically what I already did in my first attempt http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd002.zip but I copied on this post only the most important line, assuming that people would check the link to see all the script, so if not the case here it is:
#target "InDesign";
$.localize = true;
$.locale = null;
//$.locale = "en";
function main() {
//var redefineParagraphStyle = app.menuActions.itemByID(132102);
/* « redefineParagraphStyle.invoke() » => Error « Script not activated. » */
/* cf. https://forums.adobe.com/thread/1961958 */
//var redefineParagraphStyle = app.menus[113].menuItems[3];
/* Marche dans InDesign CC 2015 mais dangereux car peut correspondre à une toute autre commande dans une autre version. */
/* cf. http://kasyan.ho.com.ua/open_menu_item.html https://forums.adobe.com/thread/892886 */
var redefineParagraphStyle = app.menus.item('$ID/ParaStylePanelPopup').menuItems.item('$ID/Redefine Style');
if (! redefineParagraphStyle.enabled) { throw new Error({
en: "This script is intended to be used when selecting (or placing the cursor in) some text that has a paragraph style applied "
+"and some unwanted overriden attributes that will be reverted to the style's values "
+"for all occurrences where the same overrides affect the same style in the document.",
fr: "The script est prévu pour être utilisé en sélectionnant (ou plaçant le curseur dans) du texte ayant un style de paragraphe appliqué "
+"et certains attributs affectés par des remplacements indésirables qui seront rétablis à la valeur du style "
+"pour toutes les occurrences où les mêmes remplacements affectent le même style dans le document."
}); }
var myStyle = app.activeDocument.selection[0].insertionPoints[0].appliedParagraphStyle;
var myStyleName = myStyle.name;
var myStyleCopy = myStyle.duplicate();
redefineParagraphStyle.associatedMenuAction.invoke();
myStyle.remove(myStyleCopy);
myStyleCopy.name = myStyleName;
}
try {
app.doScript(
function() { main(); },
ScriptLanguage.JAVASCRIPT,
undefined,
UndoModes.ENTIRE_SCRIPT,
File($.fileName).name
);
} catch(e) { alert(e); }
and I also already tried app.findKeyStrings() to get other variant of the locale-independent string, so in http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd002a.zip I tried to replace '$ID/Redefine Style' with '$ID/RedefineStyle' — expecting that it might be better without space.
I also presume my colleague knows that he has to select some text that is showing overrides on the applied paragraph style, before running the script, because it was first his own idea and because I explained how to use the script on the demo idml file included with the jsx in the zip. From that screenshot he sent me he seems to be correctly attempting to use the script when the error message occurs:
I wonder if other users would get the same error when trying the script wadyms-paragraphstyle-trick_indd002.jsx on the document wadyms-paragraphstyle-trick_indd002.idml, for example on a German InDesign version.
Copy link to clipboard
Copied
Hi Frédéric,
tested your script:
wadyms-paragraphstyle-trick_indd002.jsx
on:
wadyms-paragraphstyle-trick_indd002.idml
Same selection as shown in your screenshot with the error message.
No problem with my German versions of InDesign CS5.5 and CC on OSX.
The script ran as intended.
I observed one difference in error handling of CS5.5 and CC v 9.3.0.
You do not ask if a document is open or if text is selected.
If you provoke such an error CS5.5 is alerting a simple: "undefined" statement.
And that's ok, I think, because you wrapped the try/catch around the doScript statement and did no error handling on the two cases I mentioned. Surprisingly InDesign CC is alerting your full error message regardless of the individual error caught. Just picking up the message…
For individual error handling I'd suggest something like that:
// indd002-MinimumVersion-2.jsx
#targetengine reformatToAppliedStyle
app.doScript
(
reformatToAppliedStyle,
ScriptLanguage.JAVASCRIPT,
undefined,
UndoModes.ENTIRE_SCRIPT,
"Script | Undo Action"
);
function reformatToAppliedStyle()
{
if(app.documents.length == 0){ return }; // Do we need an alert here? No.
if(app.selection.length != 1){return}; // Do we need an alert here? No.
if(!app.selection[0].hasOwnProperty("baselineShift"))
{ alert("Error: No text selected."); return };
var menuItem = app.menus.item("$ID/ParaStylePanelPopup").menuItems.item("$ID/Redefine Style");
if(menuItem.isValid == false)
{ alert("Error: menuItem Redefine Style cannot be found."); return };
if(menuItem.enabled == false)
{ alert("Error: menuItem Redefine Style is not enabled."); return };
var myStyle = app.activeDocument.selection[0].insertionPoints[0].appliedParagraphStyle;
/*
// [ No Paragraph Style ] used.
NOTE: The following if statement should never be executed
if [ No Paragraph Style ] is really used on selected text,
because "Redefine Style" is never enabled with [ No Paragraph Style ]
and we already have an error condition for that case.
Nevertheless:
You cannot duplicate [ No Paragraph Style ]
*/
if(myStyle.index == 0)
{ alert("Error: No custom paragraph style applied: [ No Paragraph Style ]"); return };
// [ Basic Paragraph Style ] used.
// You can duplicate that style, but you cannot remove the original.
// You cannot rename the duplicate to that style.
if(myStyle.index == 1)
{ alert("Error: No custom paragraph style applied: [ Basic Paragraph Style ]"); return };
// FWIW: Styles cannot be named with [ or ] in their names.
// [ and ] are restricted to the names of the base styles.
// Maybe there are other special characters that are not working?!
var myStyleName = myStyle.name;
var myStyleCopy = myStyle.duplicate();
menuItem.associatedMenuAction.invoke();
myStyle.remove(myStyleCopy);
myStyleCopy.name = myStyleName;
};
Regards,
Uwe
Copy link to clipboard
Copied
Additionally you could do a test for overrides in selected text after line 22:
if(app.selection[0].styleOverridden == false)
{ alert("Error: No style override detected."); return };
Regards,
Uwe
Copy link to clipboard
Copied
Thank you both for your suggestions, so June the 12th I uploaded this http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd003.zip but then there was the summer holidays and after them we were too busy. Recently I also tried something else to avoid the use of menu item invocation http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd006.zip attempting to emulate the "Redefine Style" command using something like that:
myStyle.properties = app.activeDocument.selection[0].insertionPoints[0].properties;
It works on InDesign CC but does nothing on InDesign CS5. Moreover it seems quite hazardous because it is not really equivalent to the original manual trick (I had to surround it by additional code intended to avoid the redefinition to include attributes from character styles, nested styles, grep styles…).
So I rather suggested my colleague to try the previous wadyms-paragraphstyle-trick_indd003.jsx version:
// indd002-MinimumVersion-2.jsx
#target "InDesign";
#targetengine reformatToAppliedStyle
app.doScript
(
reformatToAppliedStyle,
ScriptLanguage.JAVASCRIPT,
undefined,
UndoModes.ENTIRE_SCRIPT,
"Script | Undo Action"
);
function reformatToAppliedStyle()
{
if(app.documents.length == 0){ return; } // Do we need an alert here? No.
if(app.selection.length != 1){ return; } // Do we need an alert here? No.
if(!app.selection[0].hasOwnProperty("baselineShift"))
{ alert("Error: No text selected."); return; }
if(app.selection[0].styleOverridden == false)
{ alert("Error: No style override detected."); return; }
var menuItem = app.menus.item("$ID/ParaStylePanelPopup").menuItems.item("$ID/Redefine Style");
if(menuItem.isValid == false)
{ alert("Error: menuItem Redefine Style cannot be found."); return; }
if(menuItem.enabled == false)
{ alert("Error: menuItem Redefine Style is not enabled."); return; }
var myStyle = app.activeDocument.selection[0].insertionPoints[0].appliedParagraphStyle;
/*
// [ No Paragraph Style ] used.
NOTE: The following if statement should never be executed
if [ No Paragraph Style ] is really used on selected text,
because "Redefine Style" is never enabled with [ No Paragraph Style ]
and we already have an error condition for that case.
Nevertheless:
You cannot duplicate [ No Paragraph Style ]
*/
if(myStyle.index == 0)
{ alert("Error: No custom paragraph style applied: [ No Paragraph Style ]"); return; }
// [ Basic Paragraph Style ] used.
// You can duplicate that style, but you cannot remove the original.
// You cannot rename the duplicate to that style.
if(myStyle.index == 1)
{ alert("Error: No custom paragraph style applied: [ Basic Paragraph Style ]"); return; }
// FWIW: Styles cannot be named with [ or ] in their names.
// [ and ] are restricted to the names of the base styles.
// Maybe there are other special characters that are not working?!
var myStyleName = myStyle.name;
var myStyleCopy = myStyle.duplicate();
menuItem.associatedMenuAction.invoke();
myStyle.remove(myStyleCopy);
myStyleCopy.name = myStyleName;
}
Expecting that your in-depth error handling would enable us to dig what’s wrong with his localized InDesign versions. But he gets those errors:
So both CC and CS5.5 screenshots basically say the same thing:
JavaScript Ошибка! = JavaScript Error!
Номер ошибки = Error number: 45
Сообщение об ошибке = Error report: Недопустимый объект = Object is invalid
Файл = File: C:\Users\Вадим\AppData\Roaming\Adobe\InDesign\Version 11.0/7.5\ru_RU\Scripts\Scripts Panel\wadyms-paragraphstyle-trick_indd003.jsx
Строка = Line: 7
Источник = Source: (
He seems to have problems with the doScript() method, so I will try without it…