[ID CC-CC2021] Поменять местами слова и символы

Bebs

Топикстартер
15 лет на форуме
Сообщения
461
Реакции
223
Была здесь как-то тема "Скрипт в одну строку". Это из той же серии. Простые сценарии поиска-замены, записанные скриптом ChainGREP
1. Поменять местами слова
Код:
main();
function main() {
    if (app.layoutWindows.length == 0) return;
    if (app.selection.length != 1 || !app.selection[0].hasOwnProperty('changeGrep')) {
        alert('Please select only one textframe or text range!');
        return;
    }
    var changeObject = app.selection[0];
    if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
    var doc = app.documents[0];
    var style;
    var options = app.findChangeGrepOptions.properties;
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[Поменять местами слова]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"(\\w+) (\\w+)"});
        app.changeGrepPreferences.properties = ({changeTo:"$2 $1"});
        changeObject.changeGrep();
    } catch (e) {alert(e)}
    app.findChangeGrepOptions.properties = options;
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
};

function getStyleByString(root, string, property) {
    stringResult = string.match (/^(.*?[^\\]):(.*)$/);
    var cStyleName = (stringResult) ? stringResult[1] : string;
    cStyleName = cStyleName.replace (/\\:/g, ':');
    remainingString = (stringResult) ? stringResult[2] : '';
    var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
    var cStyle = root[newProperty].itemByName(cStyleName);
    if (remainingString.length > 0 && cStyle.isValid) cStyle = getStyleByString (cStyle, remainingString, property);
    return cStyle;
};
2. Поменять местами символы
Код:
main();
function main() {
    if (app.layoutWindows.length == 0) return;
    if (app.selection.length != 1 || !app.selection[0].hasOwnProperty('changeGrep')) {
        alert('Please select only one textframe or text range!');
        return;
    }
    var changeObject = app.selection[0];
    if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
    var doc = app.documents[0];
    var style;
    var options = app.findChangeGrepOptions.properties;
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[Поменять местами символы]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"(.)(.)"});
        app.changeGrepPreferences.properties = ({changeTo:"$2$1"});
        changeObject.changeGrep();
    } catch (e) {alert(e)}
    app.findChangeGrepOptions.properties = options;
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
};

function getStyleByString(root, string, property) {
    stringResult = string.match (/^(.*?[^\\]):(.*)$/);
    var cStyleName = (stringResult) ? stringResult[1] : string;
    cStyleName = cStyleName.replace (/\\:/g, ':');
    remainingString = (stringResult) ? stringResult[2] : '';
    var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
    var cStyle = root[newProperty].itemByName(cStyleName);
    if (remainingString.length > 0 && cStyle.isValid) cStyle = getStyleByString (cStyle, remainingString, property);
    return cStyle;
};
 
  • Спасибо
Реакции: Oleg Butrin

Bebs

Топикстартер
15 лет на форуме
Сообщения
461
Реакции
223
Еще один маленький скрипт, созданный при помощи ChainGREP.
При внесении правки в верстку нередко приходится вносить такую правку
Снимок экрана 2019-10-05 в 17.28.42.png

Снимок экрана 2019-10-05 в 17.30.54.png

Это, конечно несложно, но с маленьким скриптом, для которого сделано клавишное сокращение быстрее.
Код:
main();
function main() {
    if (app.layoutWindows.length == 0) return;
    if (app.selection.length != 1 || !app.selection[0].hasOwnProperty('changeGrep')) {
        alert('Please select only one textframe or text range!');
        return;
    }
    var changeObject = app.selection[0];
    if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
    var doc = app.documents[0];
    var style;
    var options = app.findChangeGrepOptions.properties;
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[,.]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"(;|,)(\\s)([\\l\\u])"});
        app.changeGrepPreferences.properties = ({changeTo:".$2$3"});
        changeObject.changeGrep();
    } catch (e) {alert(e)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[Caps]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"\\<([\\l\\u])"});
        app.changeGrepPreferences.properties = ({capitalization:1634493296});
        changeObject.changeGrep();
    } catch (e) {alert(e)}
    app.findChangeGrepOptions.properties = options;
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
};

function getStyleByString(root, string, property) {
    stringResult = string.match (/^(.*?[^\\]):(.*)$/);
    var cStyleName = (stringResult) ? stringResult[1] : string;
    cStyleName = cStyleName.replace (/\\:/g, ':');
    remainingString = (stringResult) ? stringResult[2] : '';
    var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
    var cStyle = root[newProperty].itemByName(cStyleName);
    if (remainingString.length > 0 && cStyle.isValid) cStyle = getStyleByString (cStyle, remainingString, property);
    return cStyle;
};
 
  • Спасибо
Реакции: PorosyuK и Oleg Butrin