Advertisement
Guest User

Untitled

a guest
May 12th, 2012
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // script.name = scaleStrokes.jsx;
  2. // script.description = scales selected objects strokes only;
  3. // script.required = one or more selected objects
  4. // script.parent = CarlosCanto // 5/7/12;
  5. // script.elegant = false;
  6. // script.updates = preview ON by default // 5/7/12
  7. #target Illustrator
  8. #targetengine 'main'
  9.  
  10. var unscale = 0;
  11.  
  12. var win = new Window("palette", "Scale Strokes");
  13. var pnlScale = win.add("panel");
  14. pnlScale.orientation = "row";
  15. var lblScale = pnlScale.add("statictext", undefined, "Scale");
  16. var editScale = pnlScale.add("edittext", undefined, "100");
  17. editScale.characters = 5;
  18. var lblScale = pnlScale.add("statictext", undefined, "%");
  19.  
  20. var grpScroll = win.add("group");
  21. var scrlScale = grpScroll.add("scrollbar", undefined, 100, 1, 1000); // 1 min scaling to avoid division by zero
  22. var btnReset = win.add("button", undefined, "Reset");
  23. btnReset.helpTip = "unscale or reset value to 100%"
  24. win.helpTip = "\u00A9 2012 Carlos Canto";
  25.  
  26. scrlScale.onChange = function() {
  27.     editScale.text = this.value; // update edit box with scroll value
  28.     editScale.notify("onChange") // call the onchange event handler
  29. }
  30.  
  31. editScale.onChange = function() {
  32.     if (this.text < 1) // if value entered by hand is less than 1, make it 1 to avoid division by zero
  33.     this.text = 1;
  34.     scrlScale.value = this.text; // update edit box with scroll value
  35.     btMsg(scaleStrokes.toString() + "\n scaleStrokes(activeDocument.selection," + editScale.text + ")");
  36. }
  37.  
  38. // unscale (back to 100%) on reset, except no scaling has applied
  39. btnReset.onClick = function() {
  40.     editScale.text = 100;
  41.     editScale.notify("onChange")
  42. }
  43.  
  44. // Deal with selection changed, reset values onActivate
  45. var sel, newsel;
  46.  
  47. win.onDeactivate = function() {
  48.     btMsg('sel = activeDocument.selection')
  49. }
  50.  
  51. win.onActivate = function() {
  52.     btMsg('newsel = activeDocument.selection')
  53.     if (!isSelEqual(sel, newsel) && editScale.text != 100) {
  54.         editScale.text = scrlScale.value = 100, unscale = 0;
  55.     }
  56. }
  57.  
  58. win.center();
  59. win.show();
  60.  
  61. function scaleStrokes(sel, scale) {
  62.     // skip unscaling the first time, there's nothing to unscale
  63.     if (unscale != 0) {
  64.         unscaleStrokes(sel, unscale);
  65.     }
  66.     for (i = 0; i < sel.length; i++) {
  67.         var pgitem = sel[i];
  68.         pgitem.resize(100, 100, undefined, undefined, undefined, undefined, scale, Transformation.CENTER);
  69.     }
  70.     unscale = 10000 / scale;
  71. }
  72.  
  73. // unscale or bring back selection to 100%
  74. function unscaleStrokes(sel, unscale) {
  75.     for (j = 0; j < sel.length; j++) {
  76.         var pgitem = sel[j];
  77.         pgitem.resize(100, 100, undefined, undefined, undefined, undefined, unscale, Transformation.CENTER);
  78.     }
  79. }
  80.  
  81. function BridgeTalkErrorHandler(a) {
  82.     alert(a.body + "(" + a.headers["Error-Code"] + ")")
  83. }
  84.  
  85. // use BridgeTalk to call function, this is needed because the use of "palette" panel
  86. function btMsg(a) {
  87.     var b = new BridgeTalk;
  88.     b.target = "illustrator", b.body = a, b.onError = BridgeTalkErrorHandler, b.send()
  89. }
  90.  
  91. // check whether selection changed or not
  92. function isSelEqual(a, b) {
  93.     if (!a || !b) return false
  94.     if (a.length != b.length) return false
  95.     for (var i = 0; i < a.length; i++) {
  96.         if (a[i] != b[i]) return false
  97.     }
  98.     return true
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement