Guest User

Untitled

a guest
Dec 16th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. /**
  2. * Add array indexOf() method
  3. *
  4. * @param {object} Object
  5. * @param {int} max
  6. * @param {int} min
  7. * @return {mixed}
  8. */
  9. if (!Array.prototype.indexOf) Array.prototype.indexOf = (function(Object, max, min){
  10. "use strict";
  11. return function indexOf(member, fromIndex) {
  12. if(this===null||this===undefined)throw TypeError("Array.prototype.indexOf called on null or undefined");
  13.  
  14. var that = Object(this), Len = that.length >>> 0, i = min(fromIndex | 0, Len);
  15. if (i < 0) i = max(0, Len+i); else if (i >= Len) return -1;
  16.  
  17. if(member===void 0){ for(; i !== Len; ++i) if(that[i]===void 0 && i in that) return i; // undefined
  18. }else if(member !== member){ for(; i !== Len; ++i) if(that[i] !== that[i]) return i; // NaN
  19. }else for(; i !== Len; ++i) if(that[i] === member) return i; // all else
  20.  
  21. return -1; // if the value was not found, then return -1
  22. };
  23. })(Object, Math.max, Math.min);
  24.  
  25. /**
  26. * Save for web
  27. */
  28. var s = {
  29. defaults: {
  30. width: '1000px',
  31. quality: 60,
  32. warnSize: UnitValue('3000px')
  33. },
  34. imgTypes: ['jpg','jpeg','gif','png','pdf','svg'],
  35. files: null,
  36. destination: null,
  37. width: null,
  38. quality: null,
  39. /**
  40. * Set files
  41. */
  42. setFiles: function() {
  43. this.files = openDialog();
  44. },
  45. /**
  46. * Set image width
  47. */
  48. setWidth: function() {
  49. var w = prompt('What pixel width would you like to resize your images to?', '1000 px');
  50. this.width = UnitValue(w ? w : this.defaults.width);
  51. },
  52. /**
  53. * Set quality
  54. */
  55. setQuality: function(q) {
  56. q = parseInt(prompt('What quality would you like the images saved at? (0-100)', this.defaults.quality));
  57. if (!q || q < 0 || q > 100) {
  58. this.setQuality();
  59. }
  60. this.quality = q;
  61. },
  62. /**
  63. * Set save for web options
  64. */
  65. setSaveForWebOptions: function() {
  66. this.saveForWebOptions = new ExportOptionsSaveForWeb();
  67. this.saveForWebOptions.quality = this.quality;
  68. this.saveForWebOptions.format = SaveDocumentType.JPEG;
  69. this.saveForWebOptions.saveForWebOptions = true;
  70. },
  71. /**
  72. * Check width
  73. */
  74. checkWidth: function(c) {
  75. if (this.width.as('px') > this.defaults.warnSize.as('px')) {
  76. c = confirm('The provided size is very large to save for web. Are you sure you would like to continue?');
  77. if (c) return true;
  78. if (confirm('Would you like to provide a different width?')) this.init();
  79. return false;
  80. }
  81. return true;
  82. },
  83. /**
  84. * Set save destination
  85. */
  86. setDestination: function() {
  87. this.destination = Folder.selectDialog("Choose Save Destination");
  88. },
  89. /**
  90. * Save for web
  91. *
  92. * @param File
  93. */
  94. saveForWeb: function(file) {
  95. // Ensure valid file type
  96. if (file && file.constructor && file.constructor == File) {
  97. var doc = open(file),
  98. w = doc.width,
  99. h = doc.height,
  100. name = doc.name.split('.'),
  101. ext = name.pop();
  102.  
  103. if (this.imgTypes.indexOf(ext.toLowerCase()) < 0) {
  104. name.push(ext);
  105. }
  106. name = name.join('.');
  107.  
  108. // Resize
  109. if (w.as('px') > this.width.as('px')) {
  110. doc.resizeImage(this.width, null, 72);
  111. }
  112.  
  113. // Save for web
  114. doc.exportDocument(
  115. new File(this.destination + '/' + name + '.jpg'),
  116. ExportType.SAVEFORWEB,
  117. this.saveForWebOptions
  118. );
  119.  
  120. // Close
  121. doc.close(SaveOptions.DONOTSAVECHANGES);
  122. }
  123. },
  124. /**
  125. * Initialize script
  126. */
  127. init: function() {
  128. if (!this.files) this.setFiles();
  129. if (!this.destination) this.setDestination();
  130. this.setWidth();
  131. this.setQuality();
  132. this.setSaveForWebOptions();
  133.  
  134. // Check width
  135. if (this.checkWidth()) {
  136. // Loop through files
  137. for (var i in this.files) {
  138. this.saveForWeb(this.files[i]);
  139. }
  140. }
  141. }
  142. };
  143.  
  144. s.init();
Add Comment
Please, Sign In to add comment