Find and replace an unregistered opentype feature
Hi,
I am trying to make a script to find and replace the same character and applying a opentype feature.
This is not difficult using GREP and ChainGREP script.
My problem is the opentype feature I want to apply is not registered in Indesign
I am trying to use a "Character Variant" opentype feature.
This feature works if use this script:
app.selection[0].opentypeFeatures=[["cv01",1]];
but if You apply in the same text selection another Character Variant you remove the first one.
This is the reason I want a script to find and replace using this feature.
Using chainGREP script I got this:
//This script was auto generated by chainGREP.jsx
//chainGREP.jsx is provided by Gregor Fellenz https://www.publishingx.de/
//Download at https://www.publishingx.de/download/chain-grep
main();
function main() {
if (app.layoutWindows.length == 0) return;
if (app.selection.length == 0) {
var scope = app.documents[0];
}
else {
var w = new Window ('dialog', localize(({en:"Scope of Find/Change", de:"Bereich der Ersetzung", fr:"Portée de Rechercher/Remplacer", ja_JP:"検索置換の範囲", nl:"Zoeken in"})) );
var scopePanel = w.add ('Panel', undefined, localize(({en:"Scope of Find/Change", de:"Bereich der Ersetzung", fr:"Portée de Rechercher/Remplacer", ja_JP:"検索置換の範囲", nl:"Zoeken in"})) );
scopePanel.alignment = 'fill';
scopePanel.alignChildren = 'fill';
scopePanel.margins = [10,20,10,10];
with (scopePanel) {
rButScopeDoc = add( 'radiobutton', undefined, localize(({en:"Document", de:"Dokument", fr:"Document", ja_JP:"ドキュメント", nl:"Document"})) );
rButScopeDoc.value = true;
rButScopeStory= add( 'radiobutton', undefined, localize(({en:"Story (of Selection)", de:"Textabschnitt", fr:"Article (contenant la sélection)", ja_JP:"ストーリー", nl:"Artikel (van selectie)"})) );
}
var uiCtrlGroup = w.add ('group');
with (uiCtrlGroup) {
var cancelBtn = uiCtrlGroup.add ('button', undefined, localize(({en:"Cancel", de:"Abbrechen", fr:"Annuler", ja_JP:"キャンセル", nl:"Annuleren"})));
var okButton = uiCtrlGroup.add ('button', undefined, localize(({en:"Run", de:"Starten", fr:"Démarrer", ja_JP:"実行", nl:"Start"})) );
cancelBtn.onClick = function () {
w.close(2);
}
okButton.onClick = function () {
if (rButScopeDoc.value) scope = app.documents[0];
else if (rButScopeStory.value) scope = app.selection[0].parentStory;
w.close(1);
}
}
if(w.show() != 1) {
return
}
}
var changeObject = scope;
if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
var doc = app.documents[0];
var style;
var scriptVersion = app.scriptPreferences.version;
app.scriptPreferences.version = 16.2;
var options = app.findChangeGrepOptions.properties;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
// Query [[b]] -- If you delete this comment you break the update function
try {
app.findChangeGrepOptions.properties = ({kanaSensitive:true, widthSensitive:true});
app.findGrepPreferences.properties = ({findWhat:"g"});
app.changeGrepPreferences.properties = ({changeTo:"g", pointSize:60, otfStylisticSets:8, fillColor:"C=15 M=100 Y=100 K=0"});
changeObject.changeGrep();
} catch (e) {alert(e + ' at line ' + e.line)}
app.findChangeGrepOptions.properties = options;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.scriptPreferences.version = scriptVersion;
};
function getStyleByString(docOrGroup, string, property) {
if (string == '[No character style]') return docOrGroup[property][0];
if (string == '[No paragraph style]') return docOrGroup[property][0];
if (string == 'NormalParagraphStyle') return docOrGroup[property][1];
stringResult = string.match (/^(.*?[^\\]):(.*)$/);
var styleName = (stringResult) ? stringResult[1] : string;
styleName = styleName.replace (/\\:/g, ':');
remainingString = (stringResult) ? stringResult[2] : '';
var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
var styleOrGroup = docOrGroup[newProperty].itemByName(styleName);
if (remainingString.length > 0 && styleOrGroup.isValid) styleOrGroup = getStyleByString (styleOrGroup, remainingString, property);
return styleOrGroup;
};
Them I tried to modify the script using Character Variant Opentype Feature:
//This script was auto generated by chainGREP.jsx
//chainGREP.jsx is provided by Gregor Fellenz https://www.publishingx.de/
//Download at https://www.publishingx.de/download/chain-grep
main();
function main() {
if (app.layoutWindows.length == 0) return;
if (app.selection.length == 0) {
var scope = app.documents[0];
}
else {
var w = new Window ('dialog', localize(({en:"Scope of Find/Change", de:"Bereich der Ersetzung", fr:"Portée de Rechercher/Remplacer", ja_JP:"検索置換の範囲", nl:"Zoeken in"})) );
var scopePanel = w.add ('Panel', undefined, localize(({en:"Scope of Find/Change", de:"Bereich der Ersetzung", fr:"Portée de Rechercher/Remplacer", ja_JP:"検索置換の範囲", nl:"Zoeken in"})) );
scopePanel.alignment = 'fill';
scopePanel.alignChildren = 'fill';
scopePanel.margins = [10,20,10,10];
with (scopePanel) {
rButScopeDoc = add( 'radiobutton', undefined, localize(({en:"Document", de:"Dokument", fr:"Document", ja_JP:"ドキュメント", nl:"Document"})) );
rButScopeDoc.value = true;
rButScopeStory= add( 'radiobutton', undefined, localize(({en:"Story (of Selection)", de:"Textabschnitt", fr:"Article (contenant la sélection)", ja_JP:"ストーリー", nl:"Artikel (van selectie)"})) );
}
var uiCtrlGroup = w.add ('group');
with (uiCtrlGroup) {
var cancelBtn = uiCtrlGroup.add ('button', undefined, localize(({en:"Cancel", de:"Abbrechen", fr:"Annuler", ja_JP:"キャンセル", nl:"Annuleren"})));
var okButton = uiCtrlGroup.add ('button', undefined, localize(({en:"Run", de:"Starten", fr:"Démarrer", ja_JP:"実行", nl:"Start"})) );
cancelBtn.onClick = function () {
w.close(2);
}
okButton.onClick = function () {
if (rButScopeDoc.value) scope = app.documents[0];
else if (rButScopeStory.value) scope = app.selection[0].parentStory;
w.close(1);
}
}
if(w.show() != 1) {
return
}
}
var changeObject = scope;
if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
var doc = app.documents[0];
var style;
var scriptVersion = app.scriptPreferences.version;
app.scriptPreferences.version = 16.2;
var options = app.findChangeGrepOptions.properties;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
// Query [[b]] -- If you delete this comment you break the update function
try {
app.selection[0].hasOwnProperty("opentypeFeatures")
app.findChangeGrepOptions.properties = ({kanaSensitive:true, widthSensitive:true});
app.findGrepPreferences.properties = ({findWhat:"g"});
app.changeGrepPreferences.properties = ({changeTo:"g", pointSize:60, opentypeFeatures:[["cv02",1]], fillColor:"C=15 M=100 Y=100 K=0"});
changeObject.changeGrep();
} catch (e) {alert(e + ' at line ' + e.line)}
app.findChangeGrepOptions.properties = options;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.scriptPreferences.version = scriptVersion;
};
function getStyleByString(docOrGroup, string, property) {
if (string == '[No character style]') return docOrGroup[property][0];
if (string == '[No paragraph style]') return docOrGroup[property][0];
if (string == 'NormalParagraphStyle') return docOrGroup[property][1];
stringResult = string.match (/^(.*?[^\\]):(.*)$/);
var styleName = (stringResult) ? stringResult[1] : string;
styleName = styleName.replace (/\\:/g, ':');
remainingString = (stringResult) ? stringResult[2] : '';
var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
var styleOrGroup = docOrGroup[newProperty].itemByName(styleName);
if (remainingString.length > 0 && styleOrGroup.isValid) styleOrGroup = getStyleByString (styleOrGroup, remainingString, property);
return styleOrGroup;
};
This script doesn't work of course but I don't know how to fix it.
I am using Adobe Source Code Pro font (free font) to test this script.
Please, if anyone could help would be great.
Regards
Sami
