Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

FIND CHANGE SCRIPT BY QUERIES

Explorer ,
May 21, 2025 May 21, 2025

This script find change GREP proprieties. I am trying to do the same with colors by name but can´t find right proprieties, functions and variables. Can you help me?

var workFolder = Folder.desktop + "/FC_Moderna";

// Função para limpar preferências de GREP
function clearGrepPreferences() {
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findChangeGrepOptions = NothingEnum.NOTHING;
$.writeln("Preferências de GREP limpas.");
////alert("Preferências de GREP limpas.");
}

// Função para carregar e executar queries de GREP
function runGrepQueries() {
if (app.documents.length > 0) {
$.writeln("Documento ativo encontrado.");
////alert("Documento ativo encontrado.");
var grepFolder = new Folder(workFolder + "/GREP");
var grepFiles = grepFolder.getFiles("*.xml");

for (var i = 0; i < grepFiles.length; i++) {
clearGrepPreferences();
var grepFile = grepFiles[i];
var grepQuery = loadGrepQuery(grepFile);
$.writeln("Carregando query de GREP: " + grepFile.name);
//alert("Carregando query de GREP: " + grepFile.name);
app.findGrepPreferences.findWhat = grepQuery.findWhat;
app.changeGrepPreferences.changeTo = grepQuery.changeTo;

// Aplicar configurações de formato de busca
if (grepQuery.findFormat) {
var findStyle = findCharacterStyle(grepQuery.findFormat);
if (findStyle && findStyle.isValid) {
app.findGrepPreferences.appliedCharacterStyle = findStyle;
$.writeln("Estilo de caractere de busca aplicado: " + findStyle.name);
} else {
$.writeln("Estilo de caractere de busca não encontrado: " + grepQuery.findFormat);
}
}

// Aplicar configurações de formato de troca
if (grepQuery.changeFormat) {
var changeStyle = findCharacterStyle(grepQuery.changeFormat);
if (changeStyle && changeStyle.isValid) {
app.changeGrepPreferences.appliedCharacterStyle = changeStyle;
$.writeln("Estilo de caractere de troca aplicado: " + changeStyle.name);
} else {
$.writeln("Estilo de caractere de troca não encontrado: " + grepQuery.changeFormat);
}
}

try {
var foundItems = app.activeDocument.findGrep();
$.writeln("Itens encontrados: " + foundItems.length);
for (var j = 0; j < foundItems.length; j++) {
$.writeln("Item encontrado: " + foundItems[j].contents);
}
app.activeDocument.changeGrep();
$.writeln("Query de GREP executada.");
//alert("Query de GREP executada.");
} catch (e) {
$.writeln("Erro ao executar query de GREP: " + e.message);
//alert("Erro ao executar query de GREP: " + e.message);
}
clearGrepPreferences();
}
} else {
$.writeln("Nenhum documento ativo encontrado.");
//alert("Nenhum documento ativo encontrado.");
}
}

// Função para carregar e interpretar uma query de GREP de um arquivo XML
function loadGrepQuery(file) {
var xmlString = readFile(file);
var xml = new XML(xmlString);
var findWhat = xml.Description.FindExpression.@value.toString();
var changeTo = xml.Description.ReplaceExpression.@value.toString();
var findFormat = xml.Description.FindFormatSettings.TextAttribute.(@type == "cstyle").@value.toString();
var changeFormat = xml.Description.ReplaceFormatSettings.TextAttribute.(@type == "cstyle").@value.toString();
var includeLockedLayers = xml.Description.FindChangeOptions.IncludeLockedLayers.@value.toString();
var includeLockedStories = xml.Description.FindChangeOptions.IncludeLockedStories.@value.toString();
var includeMasterPages = xml.Description.FindChangeOptions.IncludeMasterPages.@value.toString();
var includeHiddenLayers = xml.Description.FindChangeOptions.IncludeHiddenLayers.@value.toString();
var includeFootnotes = xml.Description.FindChangeOptions.IncludeFootnotes.@value.toString();
var kanaSensitive = xml.Description.FindChangeOptions.KanaSensitive.@value.toString();
var widthSensitive = xml.Description.FindChangeOptions.WidthSensitive.@value.toString();
var replaceCondMode = xml.Description.ReplaceFormatSettings.TextAttribute.(@type == "changecondmode").@value.toString();

return {
findWhat: findWhat,
changeTo: changeTo,
findFormat: findFormat,
changeFormat: changeFormat,
includeLockedLayers: includeLockedLayers,
includeLockedStories: includeLockedStories,
includeMasterPages: includeMasterPages,
includeHiddenLayers: includeHiddenLayers,
includeFootnotes: includeFootnotes,
kanaSensitive: kanaSensitive,
widthSensitive: widthSensitive,
replaceCondMode: replaceCondMode
};
}

// Função para encontrar um estilo de caractere, incluindo aqueles em pastas
function findCharacterStyle(styleName) {
var allCharacterStyles = app.activeDocument.allCharacterStyles;
for (var i = 0; i < allCharacterStyles.length; i++) {
if (allCharacterStyles[i].name === styleName) {
$.writeln("Estilo de caractere encontrado: " + allCharacterStyles[i].name);
return allCharacterStyles[i];
}
}

// Verificar se o estilo está em uma pasta
var styleParts = styleName.split(":");
if (styleParts.length > 1) {
var folderName = styleParts[0];
var styleNameInFolder = styleParts[1];
var folder = app.activeDocument.characterStyleGroups.itemByName(folderName);
if (folder.isValid) {
var styleInFolder = folder.characterStyles.itemByName(styleNameInFolder);
if (styleInFolder.isValid) {
$.writeln("Estilo de caractere encontrado na pasta: " + styleInFolder.name);
return styleInFolder;
}
}
}

// Verificar em todas as pastas de estilos de caractere
var allCharacterStyleGroups = app.activeDocument.characterStyleGroups;
for (var j = 0; j < allCharacterStyleGroups.length; j++) {
var group = allCharacterStyleGroups[j];
var styleInGroup = group.characterStyles.itemByName(styleName);
if (styleInGroup.isValid) {
$.writeln("Estilo de caractere encontrado no grupo: " + styleInGroup.name);
return styleInGroup;
}
}

$.writeln("Estilo de caractere não encontrado: " + styleName);
return app.activeDocument.characterStyles.itemByName(styleName);
}

// Função para ler o conteúdo de um arquivo
function readFile(file) {
file.open("r");
var content = file.read();
file.close();
return content;
}

// Executar todas as queries
runGrepQueries();
alert("FEITO");

TOPICS
Scripting
137
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 21, 2025 May 21, 2025
Translate
Community Expert ,
May 21, 2025 May 21, 2025
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 24, 2025 May 24, 2025
LATEST

Hey there,

i don't see the point of using a script here.

Scripts are awesome, but there are many things that can be done without them, be it on a full document or a story.
Hence, I would strongly advise you to go for a more versatile way to solve your problem.

 

Since you're talking about colors there is a solution that works great

 

Multi/ Find change : (Free plugin)
https://www.automatication.com/product/mfc/

Basically, everything that can be done with the common F/C window, you can do it here as well.

The added value of using this plugin is that you can run a full bunch of queries all at once.

 

You will need first to save your F/C color query, if it's not already done

Once you start the plugin, you will see the whole list of you queries being available.

Create new folders, put the queries in them, select the range of research. Hit it! Done.

 

Fred

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines