Quantcast
Channel: Adobe Community: Message List - InDesign Scripting
Viewing all articles
Browse latest Browse all 37788

increase prices by a percentage in CS2

$
0
0

I have this VERY old script written for me in 2004. 

 

It worked then, but for some reason now when I run it, it updates the same number 3 times!  If I turn on "enable redraw", I can see this happen.  It updates the number to the correct value (but adds two decimal places to the end, then updates it two more times and then finally removes the decimals. 

 

I tried removing the spots in the array dealing with whole numbers with decimals, because all my numbers do not have decimal places (except if an item is less than a whole dollar)

 

Or, is this script outdated and could be re-written?

 

Basically I have a few files full of prices with dollar signs scattered around in various stories (text boxes), and need to increase all of them by a percentage that is manually entered.

 

Also, if the prices could go out to 5 places instead of 4, that might help in the future.

 

Thanks!

 

//PriceUpdateByStrings.js
//An InDesign CS example script
//
//Updates values of all dollar amounts from $.01 to $9999.99.
//
//If no documents are open, then do nothing.
if(app.documents.length !=0){
//If the document does not contain any text, or if the document does not contain any paragraph styles, then  do nothing.
if(app.activeDocument.stories.length != 0){
  myDisplayDialog();
}
}
function myDisplayDialog(){
var myDialog = app.dialogs.add({name:"UpdatePrices"});
with(myDialog.dialogColumns.add()){
  with(dialogRows.add()){
   staticTexts.add({staticLabel:"Percentage Change:"});
   var myPercentageField = percentEditboxes.add({editValue:5});
  }
}
var myResult = myDialog.show();
if(myResult == true){
  //Get the control settings from the dialog box.
  var myPercentage = myPercentageField.editValue;
  myDialog.destroy();
  myUpdatePrices(myPercentage);
}
else{
  myDialog.destroy();
}
}
function myUpdatePrices(myPercentage){
var myFoundItem, myPrice;
//Set the percentage change to five percent.
myPercentage = (myPercentage + 100)*.01;
app.findPreferences = NothingEnum.nothing;
app.changePreferences = NothingEnum.nothing;
var myFindStrings = ["$^9^9^9^9", "$^9^9^9", "$^9^9", "$^9.^9^9", "$.^9^9", "$^w^9^9^9^9", "$^w^9^9^9", "$^w^9^9", "$^w^9.^9^9", "$^w.^9^9", "$^9^9^9^9.^9^9","$^9^9^9.^9^9","$^9^9.^9^9","$^w^9^9^9^9.^9^9","$^w^ 9^9^9.^9^9","$^w^9^9.^9^9"]
for(myFindStringCounter = 0; myFindStringCounter < myFindStrings.length; myFindStringCounter++){
  var myFoundItems = app.activeDocument.search(myFindStrings[myFindStringCounter]);
  if(myFoundItems.length != 0){
   for(myCounter = myFoundItems.length-1; myCounter >= 0; myCounter --){
    myFoundItem = myFoundItems[myCounter];
    myFoundItem = myFoundItem.characters.itemByRange(1, -1);
    //Convert the price.
    myFoundItem.contents = myMakePrice(myFoundItem.contents*myPercentage);   
   }
  }
}
}


function myMakePrice(myValue){ 
var myString = "" + Math.round(myValue * 100) / 100 
var myIndex = myString.indexOf('.') 
if (myIndex < 0){     return myString + ".00";    }  
var myPrice = myString.substring(0, myIndex + 1) + myString.substring(myIndex + 1, myIndex + 3) 
if (myIndex + 2 == myString.length){ 
  myPrice += "0"; 
}
var myCents = parseInt(myPrice.substr(myPrice.length-2,2));
var myBucks = myPrice.substr(0, myPrice.length-3);
if (myCents >= 50) {
  myBucks = parseInt(myBucks)+1;
}
myPrice = myBucks + ""; 
return myPrice;
}


Viewing all articles
Browse latest Browse all 37788

Trending Articles