Skip to main content
Barry23053734dbr6
Inspiring
February 27, 2023
Answered

Amend Price Adjuster Script to target colour swatches then change the currency symbol

  • February 27, 2023
  • 2 replies
  • 1303 views

Hi Guys,

 

A few years ago Peter Kahrel very kindly wrote me this version of the Price Adjuster script with which you can enter your parameters within the script instead of a dialog box. This allows you to use it on multiple documents using the 'Batch Convert' script.

Here's the script:

/* This script is a combination of Olav Kvern's "PriceUpdateByString" script and Steve Wareham's "number_adjuster" (and adds some functionality). Olav gave permission to use his code; Steve could not be found, his web site appears to have gone permanently off-line. (Add 18% : n * 1.18 Subtract 18%: n * (1-18) Peter Kahrel */ (function () { var data = { operation: 'Multiply', // Multiply, Divide, Add, Subtract operator: '1.68', currency: '£', // £, €, $, ¥, ¤, ₹, [Any], [None] format: '1,234.56', // 1,234.56, 1.234,56 separator: '[None]', // [None], Fixed space, Third space, Quarter space, Sixth space, Thin space, Hair space thousandsCheck: true, // true, false decimals: 2, // 0, 1, 2, 3 }; //------------------------------------------------------------------ function separatorCode (sep) { switch (sep) { case '[None]': return ''; case 'Fixedspace': return '\u00A0'; case 'Thirdspace': return '\u2004'; case 'Quarterspace': return '\u2005'; case 'Sixthspace': return '\u2006'; case 'Thinspace': return '\u2009'; case 'Hairspace': return '\u200A'; default: alert ('Illegal separator: ' + sep); exit(); } } //------------------------------------------------------------------ var calculate = { Multiply: function (x, y) { return x*y; }, Divide: function (x, y) { return x/y; }, Add: function (x, y) { return Number(x)+Number(y); }, Subtract: function (x, y) { return x-y; } } //------------------------------------------------------------------ // We can have any kind of currency symbol, including none function getCurrencySymbol (str) { var c = str.match (/^[£€$¥₹¤]/); return c === null ? '' : c[0]; } //------------------------------------------------------------------ // Do the arithmetic function adjustNumber (found) { var n = found.contents; var currency = getCurrencySymbol (n); n = n.replace (/^[£€$¥₹¤]\s?/,'') n = n.replace (data.thousandsRE, ''); // Delete any thousand separators if (data.decimalCharacter === ',') { n = n.replace (',', '.'); } var result = calculate[data.operation] (n, data.operator); result = Number(result).toFixed(data.decimals); var parts = result.split('.'); if (data.thousandsCheck) { parts[0] = parts[0].replace(/(\d)(?=(\d\d\d)+$)/g, '$1'+data.thousandsSeparator); } return currency + data.separator + parts.join(data.decimalCharacter); } //---------------------------------------------------------------- // Construct a GREP string for the currency. '[Any]' means all currencies. function getCurrency (c) { if (c === '[Any]') { return '[£€$¥₹¤]\\s?'; } else if (c ==='[None]') { return ''; } else { return '[' + c + ']\\s?'; } } //------------------------------------------------------------------ // Set the thousands and decimal separators and the regular expressions function setSeparators () { if (data.format === '1,234.56') { data.thousandsSeparator = ','; data.thousandsRE = /[\u2009,]/g; data.decimalCharacter = '.'; } else { data.thousandsSeparator = '.'; data.thousandsRE = /[\u2009\.]/g; data.decimalCharacter = ','; } } //------------------------------------------------------------------ data.target = app.documents[0]; setSeparators(); data.separator = separatorCode (data.separator); data.operator = data.operator.replace (',', '.'); app.findGrepPreferences = app.changeGrepPreferences = null; app.findGrepPreferences.findWhat = getCurrency(data.currency)+'\\d[\\x{2009}\\d.,]*'; var found = data.target.findGrep(); for (var i = found.length-1; i >= 0; i--) { found[i].contents = adjustNumber (found[i]); } }());

Does anyone know a way which the script can be amended so it targets prices in a certain swatch colour or paragraph style and then swaps out the price symbol for another currency. Basically so you coould use it to convert prices fron one currency to another in the 'Batch Process' script?

TIA

 

Barry 🙂

This topic has been closed for replies.
Correct answer Peter Kahrel

Barry wrote to me a few weeks ago right when I was going on a trip. I fixed the script and sent it to him privately.

2 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
March 6, 2023

Barry wrote to me a few weeks ago right when I was going on a trip. I fixed the script and sent it to him privately.

brian_p_dts
Community Expert
Community Expert
February 27, 2023

Toward the end, try adding this line in bold. Change "YourStyleName" to the name of the target paragraph style, leaving the double quotes. 

 

app.findGrepPreferences.findWhat = getCurrency(data.currency)+'\\d[\\x{2009}\\d.,]*'; app.findGrepPreferences.appliedParagraphStyle = "YourStyleName"; var found = data.target.findGrep(); for (var i = found.length-1; i >= 0; i--) { found[i].contents = adjustNumber (found[i]); } }());

Barry23053734dbr6
Inspiring
February 28, 2023

I'd prefer to target a swatch colour. Would that be:

app.findGrepPreferences.appliedfillcolor = "YourSwatchName";

instead?