Copy link to clipboard
Copied
Hi Experts,
I Wrote Small Script for Prefixing numbers via GREP in Javascript, but i wish that symbol will appear in superscript , can this be done?
var w = new Window ("dialog");
w.preferredSize.width = 320;
w.text = "GREP Prefix for Table Selected Cells ";
var buttons = w.add ("group")
buttons.add ("button", undefined, "OK");
buttons.add ("button", undefined, "Cancel");
var myDropdown = w.add ("dropdownlist", undefined, ["$", "£","€","¥"]);
myDropdown.selection = 0;
var a = w.show()
if(a == 2){
exit(0);
}
function DoGrepPrefix(){
var Selectedcells = app.selection[0];
myChoice = myDropdown.selection;
String(myChoice).position = Position.SUPERSCRIPT;
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
//
app.findGrepPreferences.findWhat="\\d+";
app.changeGrepPreferences.changeTo= String(myChoice)+"$0"; //Prefix
Selectedcells.findGrep();
Selectedcells.changeGrep();
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
}
DoGrepPrefix();
the script work normal with no error but doesn't execute command in this line :
String(myChoice).position = Position.SUPERSCRIPT;
So the final text not appear in superscript, i know that's can be done form find manually but i hope it can be done by the script for more fast result, and thanks in advance
Copy link to clipboard
Copied
app.changeGrepPreferences.position = Position.SUPERSCRIPT;
Add this before your changeGrep() call. Not sure why you're calling findGrep() as well. String.position is not a property. I don't know why this code is not throwing an error.
Edit. If you just want the currency character to be superscript then do this:
function DoGrepPrefix(){
var Selectedcells = app.selection[0];
myChoice = myDropdown.selection;
//String(myChoice).position = Position.SUPERSCRIPT;
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
//here we change the findGrep to just find the insertion point before the digits
app.findGrepPreferences.findWhat="(?=\\d+)";
app.changeGrepPreferences.changeTo= String(myChoice); //Prefix
app.changeGrepPreferences.position= Position.SUPERSCRIPT;
//Selectedcells.findGrep();
Selectedcells.changeGrep();
app.findGrepPreferences.findWhat="^(?=\\d+)";
Selectedcells.changeGrep();
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
}
Copy link to clipboard
Copied
Hi @brianp311 , Thanks for your reply, but when i updated the script it doesnt work at all also no error!
var w = new Window ("dialog");
w.preferredSize.width = 320;
w.text = "GREP Prefix for Table Selected Cells ";
var buttons = w.add ("group")
buttons.add ("button", undefined, "OK");
buttons.add ("button", undefined, "Cancel");
var myDropdown = w.add ("dropdownlist", undefined, ["$", "£","€","¥"]);
myDropdown.selection = 0;
var a = w.show()
if(a == 2){
exit(0);
}
function DoGrepPrefix(){
var Selectedcells = app.selection[0];
myChoice = myDropdown.selection;
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
//here we change the findGrep to just find the insertion point before the digits
app.findGrepPreferences.findWhat="(?=\\d+)";
app.changeGrepPreferences.changeTo= String(myChoice); //Prefix
app.changeGrepPreferences.position= Position.SUPERSCRIPT;
Selectedcells.changeGrep();
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
}
DoGrepPrefix();
Copy link to clipboard
Copied
If the number begins the cell then you need to put a ^ in front of your lookahead:
"^(?=\\d+)"
You might want to do two searches to catch both, unless someone more in-tune with GREP has a better solution. Edited the code above to do both finds and replaces.
Copy link to clipboard
Copied
Hi @brianp311 after modifying the GREP Code the Script Worked! but also no change for the currency symbol, its in the same position and no superscript position :
var w = new Window ("dialog");
w.preferredSize.width = 320;
w.text = "GREP Prefix for Table Selected Cells ";
var buttons = w.add ("group")
buttons.add ("button", undefined, "OK");
buttons.add ("button", undefined, "Cancel");
var myDropdown = w.add ("dropdownlist", undefined, ["$", "£","€","¥"]);
myDropdown.selection = 0;
var a = w.show()
if(a == 2){
exit(0);
}
function DoGrepPrefix(){
var Selectedcells = app.selection[0];
myChoice = myDropdown.selection;
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
//here we change the findGrep to just find the insertion point before the digits
app.findGrepPreferences.findWhat="^(?=\\d+)";
app.changeGrepPreferences.changeTo= String(myChoice); //Prefix
app.changeGrepPreferences.position= Position.SUPERSCRIPT;
Selectedcells.changeGrep();
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
}
DoGrepPrefix();
Copy link to clipboard
Copied
@brianp311 Thank you , I Will Try to make another function and see result
Copy link to clipboard
Copied
it works with all other symbols except dollar sign ($), thats weird!
var w = new Window ("dialog");
w.preferredSize.width = 320;
w.text = "GREP Prefix for Table Selected Cells ";
var buttons = w.add ("group")
buttons.add ("button", undefined, "OK");
buttons.add ("button", undefined, "Cancel");
var myDropdown = w.add ("dropdownlist", undefined, ["$", "£","€","¥"]);
myDropdown.selection = 0;
var a = w.show()
if(a == 2){
exit(0);
}
function DoGrepPrefix(){
var Selectedcells = app.selection[0];
myChoice = myDropdown.selection;
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
//here we change the findGrep to just find the insertion point before the digits
app.findGrepPreferences.findWhat="^(?=\\d+)";
app.changeGrepPreferences.changeTo= String(myChoice); //Prefix
app.changeGrepPreferences.position= Position.SUPERSCRIPT;
Selectedcells.changeGrep();
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
CallDoGrepSuperscript(); //Call Second Search!
}
DoGrepPrefix();
function CallDoGrepSuperscript(){ //Second Search
var Selectedcells = app.selection[0];
myChoice = myDropdown.selection;
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat=String(myChoice); //Prefix
app.changeGrepPreferences.position= Position.SUPERSCRIPT;
Selectedcells.changeGrep();
//
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
}
Copy link to clipboard
Copied
Hi @M.Hasanin, problem might be because $ is a regular expression special symbol. I haven't tested, but escaping the $ might work. Something like:
app.changeGrepPreferences.changeTo= String(myChoice).replace('$','\$');
- Mark
Copy link to clipboard
Copied
Thanks a lot @m1b it works in findWhat in the second function :
app.findGrepPreferences.findWhat=String(myChoice).replace('$','\\$');
Copy link to clipboard
Copied
Excellent! Well done working that out! Some cases you need to escape the dollarsign, but other cases you need to escape the backslash!