Skip to content

Commit

Permalink
ref #include helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pchiorean committed Sep 30, 2021
1 parent b5d8404 commit f54c8a1
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 826 deletions.
23 changes: 3 additions & 20 deletions export/PrepareForExport.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Prepare for export v2.2.3 (2021-09-29)
Prepare for export v2.2.4 (2021-09-30)
(c) 2020-2021 Paul Chiorean (jpeg@basement.ro)
Hides some layers and moves items with special colors to separate spreads.
Expand All @@ -26,6 +26,8 @@
SOFTWARE.
*/

// @include '../lib/IsIn.jsxinc';

if (!(doc = app.activeDocument)) exit();

app.doScript(prepareForExport, ScriptLanguage.JAVASCRIPT, undefined,
Expand Down Expand Up @@ -176,23 +178,4 @@ function prepareForExport() {
}
}
}

/**
* Matches a string against elements of an array, using wildcards and case sensitivity.
* @param {String} searchValue - String to be matched
* @param {Array} array - An array of strings; wildcards: '*' (zero or more characters), '?' (exactly one character)
* @param {Boolean} [caseSensitive=false] - Case sensitivity; default false
* @returns {Boolean} - True for match, false for no match
*/
function isIn(searchValue, array, caseSensitive) {
caseSensitive = (caseSensitive === undefined) ? false : caseSensitive;
for (var i = 0, n = array.length; i < n; i++) {
if (RegExp('^' + array[i]
.replace(/[|^$(.)[\]{+}\\]/g, '\\$&') // Escape regex tokens, pass '*' and '?'
.replace(/\*/g, '.*').replace(/\?/g, '.') + // '*' and '?' wildcards
'$', caseSensitive ? '' : 'i' // Case sensitivity flag
).test(searchValue)) return true;
}
return false;
}
}
124 changes: 9 additions & 115 deletions export/QuickExport.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Quick export v2.12 (2021-09-23)
Quick export v2.12.1 (2021-09-30)
(c) 2020-2021 Paul Chiorean (jpeg@basement.ro)
Exports open .indd documents or a folder with several configurable PDF presets.
Expand All @@ -26,15 +26,17 @@
SOFTWARE.
*/

/* eslint-disable max-statements-per-line, no-useless-escape */
// @include '../lib/ProgressBar.jsxinc';
// @include '../lib/Report.jsxinc';

// Initialisation

var doc, settings, baseFolder, subfolder, suffix, exp, name, progressBar, maxCounter;
var ADV = ScriptUI.environment.keyboardState.altKey;
var WIN = (File.fs === 'Windows');
var VER = '2';
var forbiddenFilenameCharsRE = /[#%^{}\\<>*?\/$!'":@`|=]/g;
var forbiddenFilenameCharsRE = /[#%^{}\\<>*?\/$!'":@`|=]/g; // eslint-disable-line no-useless-escape
var regexTokensRE = /[|^$(.)[\]{*+?}\\]/g;
var script = (function () { try { return app.activeScript; } catch (e) { return new File(e.fileName); } }());
var settingsFile = File(Folder.userData + '/' + script.name.slice(0, script.name.lastIndexOf('.')) + '.prefs');
var presets = app.pdfExportPresets.everyItem().name.sort();
Expand Down Expand Up @@ -352,10 +354,9 @@ if (folderMode) {
while ((name = names.shift())) docs.push(app.documents.itemByName(name));
}
// Init progress bar
for (i = 0, n = docs.length; i < n; i++) pbWidth = Math.max(pbWidth, decodeURI(docs[i].name).length);
progressBar = new ProgressBar('Exporting', pbWidth + 10);
maxCounter = docs.length * ((ui.preset1.isOn.value ? 1 : 0) + (ui.preset2.isOn.value ? 1 : 0));
progressBar.reset(maxCounter);
for (i = 0, n = docs.length; i < n; i++) pbWidth = Math.max(pbWidth, decodeURI(docs[i].name).length);
progressBar = new ProgressBar('Exporting', maxCounter, pbWidth + 10);
// Documents loop
while ((doc = docs.shift())) {
if (folderMode) {
Expand Down Expand Up @@ -510,7 +511,7 @@ function doExport(/*bool*/asSpreads, /*bool*/split, /*string*/preset) {
var pdfName = filename + '.pdf';
var unique = folder + '/' + pdfName;
var baseRE = RegExp('^' +
filename.replace(/[|^$(.)\[\]{*+?}\\]/g, '\\$&') + // Escape regex tokens
filename.replace(regexTokensRE, '\\$&') + // Escape regex tokens
(suffix ? '[ _-]*' : '[ _-]+') +
'\\d+.*.pdf$', 'i');
var pdfFiles = Folder(folder).getFiles(function (f) {
Expand All @@ -520,7 +521,7 @@ function doExport(/*bool*/asSpreads, /*bool*/split, /*string*/preset) {
// Find the last index
var fileIndex;
var fileIndexRE = RegExp('^' +
filename.replace(/[|^$(.)\[\]{*+?}\\]/g, '\\$&') + // Escape regex tokens
filename.replace(regexTokensRE, '\\$&') + // Escape regex tokens
(suffix ? '[ _-]*' : '[ _-]+') +
'(\\d+)([ _-]*v *\\d*)?([ _-]*copy *\\d*)?([ _-]*v *\\d*)?$', 'i');
var fileLastIndex = 0;
Expand Down Expand Up @@ -741,110 +742,3 @@ function cleanupAndExit() {
app.pdfExportPreferences.viewPDF = old.viewPDF;
exit();
}

/**
* A simple progress bar.
* @param {String} title - Palette title (a counter will be appended)
* @param {Number} maxValue - Number of steps
* @param {Number} [maxWidth] - Max message length (characters); if ommitted, no message is shown
* @param {Number} value - Updated value
* @param {String} [message] - Message; if maxWidth is omitted on creation, no message is shown
* @example
* var progress = new ProgressBar(title, [maxWidth]);
* progress.reset(maxValue);
* progress.update(value, [message]);
* progress.close();
*/
function ProgressBar(title, maxWidth) {
var pb = new Window('palette', title);
pb.bar = pb.add('progressbar');
if (maxWidth) { // Full progress bar
pb.msg = pb.add('statictext { properties: { truncate: "middle" } }');
pb.msg.characters = Math.max(maxWidth, 50);
pb.layout.layout();
pb.bar.bounds = [ 12, 12, pb.msg.bounds[2], 24 ];
} else { // Mini progress bar
pb.bar.bounds = [ 12, 12, 476, 24 ];
}
this.reset = function (maxValue) {
pb.bar.value = 0;
pb.bar.maxvalue = maxValue || 0;
pb.bar.visible = !!maxValue;
pb.show();
if (app.windows.length > 0) {
var AW = app.activeWindow;
pb.frameLocation = [
(AW.bounds[1] + AW.bounds[3] - pb.frameSize.width) / 2,
(AW.bounds[0] + AW.bounds[2] - pb.frameSize.height) / 2
];
}
};
this.update = function (value, message) {
pb.bar.value = value;
if (maxWidth) {
pb.msg.visible = !!message;
if (message) pb.msg.text = message;
}
pb.text = title + ' \u2013 ' + value + '/' + pb.bar.maxvalue;
pb.show(); pb.update();
};
this.hide = function () { pb.hide(); };
this.close = function () { pb.close(); };
}

/**
* Displays a message in a scrollable list with optional filtering and/or compact mode.
* Inspired by this snippet by Peter Kahrel:
* http://web.archive.org/web/20100807190517/http://forums.adobe.com/message/2869250#2869250
* @version 2.0 (2021-09-12)
* @author Paul Chiorean <jpeg@basement.ro>
* @license MIT
* @param {String|String[]} message - Message to be displayed (string or array)
* @param {String} title - Dialog title
* @param {Boolean} [showFilter] - Shows a filtering field; wildcards: '?' (any char), space and '*' (AND), '|' (OR)
* @param {Boolean} [showCompact] - Sorts message and removes duplicates
*/
function report(message, title, showFilter, showCompact) {
var search, list;
var w = new Window('dialog', title);
// Convert message to array
if (message.constructor.name !== 'Array') message = message.split(/\r|\n/g);
if (showCompact && message.length > 1) { // Sort and remove duplicates
message = message.sort();
for (var i = 1, l = message[0]; i < message.length; l = message[i], i++) {
if (l === message[i]) { message.splice(i, 1); i--; }
if (message[i] === '') { message.splice(i, 1); i--; }
}
}
if (showFilter && message.length > 1) { // Add a filtering field
search = w.add('edittext { characters: 40, helpTip: "Wildcards: \'?\' (any character), space and \'*\' (AND), \'|\' (OR)" }');
search.onChanging = function () {
var str, line, i, n;
var result = [];
if (this.text === '') {
list.text = message.join('\n'); w.text = title; return;
}
str = this.text.replace(/[.[\]{+}]/g, '\\$&'); // Pass through '^*()|?'
str = str.replace(/\?/g, '.'); // '?' -> any character
if (/[ *]/g.test(str)) str = '(' + str.replace(/ +|\*/g, ').*(') + ')'; // space or '*' -> AND
str = RegExp(str, 'gi');
for (i = 0, n = message.length; i < n; i++) {
line = message[i].toString().replace(/^\s+?/g, '');
if (str.test(line)) result.push(line.replace(/\r|\n/g, '\u00b6').replace(/\t/g, '\\t'));
}
w.text = str + ' | ' + result.length + ' record' + (result.length === 1 ? '' : 's');
if (result.length > 0) list.text = result.join('\n'); else list.text = '';
};
}
list = w.add('edittext', undefined, message.join('\n'), { multiline: true, scrolling: true, readonly: true });
list.characters = (function () {
var width = 50;
for (var i = 0, n = message.length; i < n; width = Math.max(width, message[i].toString().length), i++);
return width;
}());
list.minimumSize.width = 600; list.maximumSize.width = 1024;
list.minimumSize.height = 100; list.maximumSize.height = 1024;
w.add('button { text: "Close", properties: { name: "ok" } }');
w.ok.active = true;
w.show();
}
57 changes: 4 additions & 53 deletions file/SpreadsToFiles.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Spreads to files v1.7.10 (2021-09-28)
Spreads to files v1.7.11 (2021-09-30)
(c) 2020-2021 Paul Chiorean (jpeg@basement.ro)
Saves the spreads of the active document in separate files.
Expand All @@ -26,6 +26,8 @@
SOFTWARE.
*/

// @include '../lib/ProgressBar.jsxinc';

if (!(doc = app.activeDocument)) exit();
if (!doc.saved) { alert('Document is not saved.'); exit(); }
if (doc.spreads.length === 1) { alert('Document has only one spread.'); exit(); }
Expand All @@ -37,7 +39,7 @@ var forbiddenFilenameCharsRE = /[#%^{}\\<>*?\/$!'":@`|=]/g; // eslint-disable-li
var oldUIL = app.scriptPreferences.userInteractionLevel;
var currentPath = doc.filePath;
var baseName = (/\./g.test(doc.name) && doc.name.slice(0, doc.name.lastIndexOf('.'))) || doc.name;
var progressBar = new ProgressBar('Saving');
var progressBar = new ProgressBar('Saving', doc.spreads.length);

// Detect or ask for a suffix
var defaultSufx = '-123456789abcdefghijklmnopqrstuvwxyz'.slice(0, doc.spreads.length + 1);
Expand All @@ -46,7 +48,6 @@ if (/\d\s*x\s*\d/i.test(detectedSufx)) detectedSufx = null; // Exclude '0x0' suf
if (ADV) suffix = getSuffix(null);
else suffix = detectedSufx ? String(detectedSufx) : getSuffix(null);
// Main loop
progressBar.reset(doc.spreads.length);
for (spread = 0; spread < doc.spreads.length; spread++) {
// Filter out current spread
r = [];
Expand Down Expand Up @@ -101,53 +102,3 @@ function getSuffix(str) {
}
return str.slice(0, doc.spreads.length + 1);
}

/**
* A simple progress bar.
* @param {String} title - Palette title (a counter will be appended)
* @param {Number} maxValue - Number of steps
* @param {Number} [maxWidth] - Max message length (characters); if ommitted, no message is shown
* @param {Number} value - Updated value
* @param {String} [message] - Message; if maxWidth is omitted on creation, no message is shown
* @example
* var progress = new ProgressBar(title, [maxWidth]);
* progress.reset(maxValue);
* progress.update(value, [message]);
* progress.close();
*/
function ProgressBar(title, maxWidth) {
var pb = new Window('palette', title);
pb.bar = pb.add('progressbar');
if (maxWidth) { // Full progress bar
pb.msg = pb.add('statictext { properties: { truncate: "middle" } }');
pb.msg.characters = Math.max(maxWidth, 50);
pb.layout.layout();
pb.bar.bounds = [ 12, 12, pb.msg.bounds[2], 24 ];
} else { // Mini progress bar
pb.bar.bounds = [ 12, 12, 476, 24 ];
}
this.reset = function (maxValue) {
pb.bar.value = 0;
pb.bar.maxvalue = maxValue || 0;
pb.bar.visible = !!maxValue;
pb.show();
if (app.windows.length > 0) {
var AW = app.activeWindow;
pb.frameLocation = [
(AW.bounds[1] + AW.bounds[3] - pb.frameSize.width) / 2,
(AW.bounds[0] + AW.bounds[2] - pb.frameSize.height) / 2
];
}
};
this.update = function (value, message) {
pb.bar.value = value;
if (maxWidth) {
pb.msg.visible = !!message;
if (message) pb.msg.text = message;
}
pb.text = title + ' \u2013 ' + value + '/' + pb.bar.maxvalue;
pb.show(); pb.update();
};
this.hide = function () { pb.hide(); };
this.close = function () { pb.close(); };
}
57 changes: 3 additions & 54 deletions misc/QR.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
QR code v3.5.7 (2021-09-12)
QR code v3.5.8 (2021-09-30)
(c) 2020-2021 Paul Chiorean (jpeg@basement.ro)
Adds a QR code to the current document or to a separate file.
Expand All @@ -26,6 +26,8 @@
SOFTWARE.
*/

// @include '../lib/Report.jsxinc';

doc = (app.documents.length === 0) ? app.documents.add() : app.activeDocument;
var currentPath = doc.saved ? doc.filePath : '';

Expand Down Expand Up @@ -392,56 +394,3 @@ function pageMargins(page) {
page.marginPreferences.left : page.marginPreferences.right
};
}

/**
* Simple scrollable alert inspired by this snippet by Peter Kahrel:
* http://web.archive.org/web/20100807190517/http://forums.adobe.com/message/2869250#2869250
* @param {String|String[]} message - Message to be displayed (string or array)
* @param {String} title - Dialog title
* @param {Boolean} [showFilter] - Shows a filtering field; wildcards: '?' (any char), space and '*' (AND), '|' (OR)
* @param {Boolean} [showCompact] - Sorts message and removes duplicates
*/
function report(message, title, showFilter, showCompact) {
var search, list;
var w = new Window('dialog', title);
// Convert message to array
if (message.constructor.name !== 'Array') message = message.split(/\r|\n/g);
if (showCompact && message.length > 1) { // Sort and remove duplicates
message = message.sort();
for (var i = 1, l = message[0]; i < message.length; l = message[i], i++) {
if (l === message[i]) { message.splice(i, 1); i--; }
if (message[i] === '') { message.splice(i, 1); i--; }
}
}
if (showFilter && message.length > 1) { // Add a filtering field
search = w.add('edittext { characters: 40, helpTip: "Wildcards: \'?\' (any character), space and \'*\' (AND), \'|\' (OR)" }');
search.onChanging = function () {
var str, line, i, n;
var result = [];
if (this.text === '') {
list.text = message.join('\n'); w.text = title; return;
}
str = this.text.replace(/[.[\]{+}]/g, '\\$&'); // Pass through '^*()|?'
str = str.replace(/\?/g, '.'); // '?' -> any character
if (/[ *]/g.test(str)) str = '(' + str.replace(/ +|\*/g, ').*(') + ')'; // space or '*' -> AND
str = RegExp(str, 'gi');
for (i = 0, n = message.length; i < n; i++) {
line = message[i].toString().replace(/^\s+?/g, '');
if (str.test(line)) result.push(line.replace(/\r|\n/g, '\u00b6').replace(/\t/g, '\\t'));
}
w.text = str + ' | ' + result.length + ' record' + (result.length === 1 ? '' : 's');
if (result.length > 0) list.text = result.join('\n'); else list.text = '';
};
}
list = w.add('edittext', undefined, message.join('\n'), { multiline: true, scrolling: true, readonly: true });
list.characters = (function () {
var width = 50;
for (var i = 0, n = message.length; i < n; width = Math.max(width, message[i].toString().length), i++);
return width;
}());
list.minimumSize.width = 600; list.maximumSize.width = 1024;
list.minimumSize.height = 100; list.maximumSize.height = 1024;
w.add('button { text: "Close", properties: { name: "ok" } }');
w.ok.active = true;
w.show();
}

0 comments on commit f54c8a1

Please sign in to comment.