Script that converts selected text to a GREP find - Experimental script
I started working on this script some time ago and it's since fallen to the wayside - I'm not using it. But I was tinkering around some unused and unfinished script projects
I've taken it as far as I can go in terms of a learning curve for me - if anyone wants it or wants to tinker with it and make it better feel free.
All it was meant to do is give a starting point to a GREP search for people not familiar with GREP to give them a leg up on the starting point of the GREP.
But I feel it could be more robust.
#target indesign
(function () {
if (app.documents.length === 0) {
alert("No document open.");
return;
}
if (app.selection.length === 0 || !app.selection[0].hasOwnProperty("contents")) {
alert("Please select some text.");
return;
}
var txt = app.selection[0].contents;
var grep = "";
var i = 0;
function isDigit(c) { return /\d/.test(c); }
function isLower(c) { return /[a-z]/.test(c); }
function isUpper(c) { return /[A-Z]/.test(c); }
function isLetter(c) { return /[A-Za-z]/.test(c); }
function isSpace(c) { return /\s/.test(c); }
while (i < txt.length) {
var c = txt[i];
// DIGITS (collapse runs)
if (isDigit(c)) {
var start = i;
while (i < txt.length && isDigit(txt[i])) i++;
var count = i - start;
grep += (count === 1) ? "\\d" : "\\d{" + count + "}";
continue;
}
// LOWERCASE LETTERS
if (isLower(c)) {
var start = i;
while (i < txt.length && isLower(txt[i])) i++;
var count = i - start;
grep += (count === 1) ? "[a-z]" : "[a-z]{" + count + "}";
continue;
}
// UPPERCASE LETTERS
if (isUpper(c)) {
var start = i;
while (i < txt.length && isUpper(txt[i])) i++;
var count = i - start;
grep += (count === 1) ? "[A-Z]" : "[A-Z]{" + count + "}";
continue;
}
// SPACES
if (isSpace(c)) {
while (i < txt.length && isSpace(txt[i])) i++;
grep += "\\s+";
continue;
}
// ESCAPED GREP METACHARACTERS
if (/[\\.^$*+?()[\]{}|]/.test(c)) {
grep += "\\" + c;
i++;
continue;
}
// EVERYTHING ELSE (commas, hyphens, slashes, etc.)
grep += c;
i++;
}
// Push to Find GREP
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = grep;
alert("Generated GREP:\n\n" + grep);
})();
This is another iteration of the same thing
#target indesign
(function () {
if (app.documents.length === 0) {
alert("No document open.");
return;
}
if (app.selection.length === 0 || !app.selection[0].hasOwnProperty("contents")) {
alert("Please select some text.");
return;
}
var txt = app.selection[0].contents;
var grep = "";
var i = 0;
function isDigit(c) { return /\d/.test(c); }
function isLower(c) { return /[a-z]/.test(c); }
function isUpper(c) { return /[A-Z]/.test(c); }
function isLetter(c) { return /[A-Za-z]/.test(c); }
function isSpace(c) { return /\s/.test(c); }
while (i < txt.length) {
var c = txt[i];
/* =========================
NUMBERS (smart handling)
========================= */
if (isDigit(c)) {
var start = i;
while (i < txt.length && /[\d,.]/.test(txt[i])) i++;
var token = txt.substring(start, i);
// Comma-grouped number
if (/^\d{1,3}(,\d{3})+$/.test(token)) {
grep += "\\d{1,3}(,\\d{3})*";
continue;
}
// Decimal number
if (/^\d+(\.\d+)?$/.test(token)) {
grep += "\\d+(\\.\\d+)?";
continue;
}
// Plain digits
grep += "\\d{" + token.replace(/\D/g, "").length + "}";
continue;
}
/* =========================
LETTERS
========================= */
if (isLower(c)) {
var start = i;
while (i < txt.length && isLower(txt[i])) i++;
grep += "[a-z]{" + (i - start) + "}";
continue;
}
if (isUpper(c)) {
var start = i;
while (i < txt.length && isUpper(txt[i])) i++;
grep += "[A-Z]{" + (i - start) + "}";
continue;
}
/* =========================
SPACES
========================= */
if (isSpace(c)) {
while (i < txt.length && isSpace(txt[i])) i++;
grep += "\\s+";
continue;
}
/* =========================
ESCAPED METACHARACTERS
========================= */
if (/[\\.^$*+?()[\]{}|]/.test(c)) {
grep += "\\" + c;
i++;
continue;
}
/* =========================
EVERYTHING ELSE
========================= */
grep += c;
i++;
}
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = grep;
alert("Generated GREP:\n\n" + grep);
})();

