Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. function main(){
  2.  
  3. var hexToRGB = function(hex) { var r = hex >> 16; var g = hex >> 8 & 0xFF; var b = hex & 0xFF; return [r, g, b]; };
  4.  
  5. function SetLayerType() {
  6. var layerType='';
  7. while(layerType == null || layerType == ''){
  8. layerType = prompt('Enter layers type...', '', 'Step 1');
  9. }
  10. return layerType;
  11. }
  12. layerType = SetLayerType();
  13.  
  14. function SetLayerName() {
  15. var layerName = prompt('Enter layers name...', '', 'Step 2');
  16. }
  17. layerName = SetLayerName();
  18.  
  19. if (confirm('Click "Yes" and choose the color you want to replace...', false, 'Step 3')) {
  20. if (app.showColorPicker()){
  21. var color1_decimal = app.foregroundColor.rgb.hexValue;
  22. var color1_hexadecimal = color1_decimal.toString(16);
  23. var color1_rgb = hexToRGB(parseInt(color1_hexadecimal, 16));
  24. };
  25. }
  26. else {
  27. return;
  28. };
  29.  
  30. if (confirm('Click "Yes" and choose the new color...', false, 'Step 4')) {
  31. if (app.showColorPicker()){
  32. var color2_decimal = app.foregroundColor.rgb.hexValue;
  33. var color2_hexadecimal = color2_decimal.toString(16);
  34. var color2_rgb = hexToRGB(parseInt(color2_hexadecimal, 16));
  35. };
  36. }
  37. else {
  38. return;
  39. };
  40.  
  41. var layers = getLayersData(),
  42. sourceColor = [color1_rgb[0], color1_rgb[1], color1_rgb[2]],
  43. targetColor = [color2_rgb[0], color2_rgb[1], color2_rgb[2]];
  44. if (confirm ('You are about to replace color for [' + layers.length + '] shapes inn[' + activeDocument.name + '] document.nnOld color: (' + sourceColor + ')nNew Color: (' + targetColor + ')nnDo you want to continue?', false, 'Step 5')) {
  45. var colorToChange = new SolidColor();
  46. colorToChange.rgb.red = sourceColor[0];
  47. colorToChange.rgb.green = sourceColor[1];
  48. colorToChange.rgb.blue = sourceColor[2];
  49. for (var i = 0; i < layers.length; i++) {
  50. if (layers[i].color.rgb.hexValue == colorToChange.rgb.hexValue) {
  51. selectById(layers[i].id);
  52. changeShapeColor(targetColor);
  53. }
  54. }
  55. }
  56. else {
  57. return;
  58. };
  59.  
  60. function getLayersData() {
  61. var lyrs = [];
  62. try {
  63. activeDocument.backgroundLayer;
  64. var layers = 0
  65. }
  66. catch (e) {
  67. var layers = 1;
  68. };
  69. while (true) {
  70. ref = new ActionReference();
  71. ref.putIndex(charIDToTypeID('Lyr '), layers);
  72. try {
  73. var desc = executeActionGet(ref);
  74. }
  75. catch (err) {
  76. break;
  77. }
  78. var lyr = {};
  79. lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
  80. lyr.name = desc.getString(charIDToTypeID("Nm "));
  81. lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
  82. if (lyr.type == layerType && lyr.name.match(layerName)) {
  83. var adj = desc.getList(stringIDToTypeID("adjustment")).getObjectValue(0);
  84. if (adj.hasKey(stringIDToTypeID("color"))) {
  85. var curColor = new SolidColor();
  86. curColor.rgb.red = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("red"));
  87. curColor.rgb.green = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("grain"));
  88. curColor.rgb.blue = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("blue"));
  89. lyr.color = curColor;
  90. lyrs.push(lyr);
  91. }
  92. }
  93. layers++;
  94. }
  95. return lyrs
  96. };
  97.  
  98. function changeShapeColor(color) {
  99. var desc8 = new ActionDescriptor();
  100. var ref1 = new ActionReference();
  101. ref1.putEnumerated(stringIDToTypeID('contentLayer'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
  102. desc8.putReference(charIDToTypeID('null'), ref1);
  103. var desc9 = new ActionDescriptor();
  104. var desc10 = new ActionDescriptor();
  105. desc10.putDouble(charIDToTypeID('Rd '), color[0]);
  106. desc10.putDouble(charIDToTypeID('Grn '), color[1]);
  107. desc10.putDouble(charIDToTypeID('Bl '), color[2]);
  108. desc9.putObject(charIDToTypeID('Clr '), charIDToTypeID('RGBC'), desc10);
  109. desc8.putObject(charIDToTypeID('T '), stringIDToTypeID('solidColorLayer'), desc9);
  110. executeAction(charIDToTypeID('setd'), desc8, DialogModes.NO);
  111. };
  112.  
  113. function selectById(id) {
  114. var desc1 = new ActionDescriptor();
  115. var ref1 = new ActionReference();
  116. ref1.putIdentifier(charIDToTypeID('Lyr '), id);
  117. desc1.putReference(charIDToTypeID('null'), ref1);
  118. executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
  119. };
  120.  
  121. }
  122. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement