Advertisement
Guest User

Example Photoshop code.

a guest
Jan 19th, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | Source Code | 0 0
  1. #target photoshop
  2.  
  3. // Ensure a document is open
  4. if (app.documents.length === 0) {
  5. alert("No document is open. Please open a document and try again.");
  6. throw new Error("No document open");
  7. }
  8.  
  9. var doc = app.activeDocument;
  10.  
  11. // Step 1: Create a new layer
  12. var newLayer = doc.artLayers.add();
  13. newLayer.name = "Example";
  14.  
  15. // Step 2: Get current dimensions in pixels
  16. var originalWidth = doc.width.as('px');
  17. var originalHeight = doc.height.as('px');
  18.  
  19. // Function to resize the document
  20. function resizeDocument() {
  21. var newWidth, newHeight;
  22.  
  23. if (originalWidth > 1024 || originalHeight > 1024) {
  24. if (originalWidth === originalHeight) {
  25. // Square image
  26. newWidth = 1024;
  27. newHeight = 1024;
  28. } else {
  29. // Non-square image
  30. if (originalWidth > originalHeight) {
  31. newWidth = 1024;
  32. newHeight = Math.round((originalHeight / originalWidth) * 1024);
  33. } else {
  34. newHeight = 1024;
  35. newWidth = Math.round((originalWidth / originalHeight) * 1024);
  36. }
  37. }
  38.  
  39. // Resize the image
  40. doc.resizeImage(UnitValue(newWidth, "px"), UnitValue(newHeight, "px"), null, ResampleMethod.BICUBIC);
  41. } else {
  42. // Image is already within the desired size
  43. return false;
  44. }
  45. return true;
  46. }
  47.  
  48. // Step 3: Resize if necessary
  49. var resized = resizeDocument();
  50.  
  51. // Step 4: Save the document
  52. function saveDocument() {
  53. var saveFile;
  54. var newName;
  55.  
  56. if (doc.saved) {
  57. // The document has been saved before
  58. try {
  59. var originalPath = doc.path;
  60. var originalName = doc.name;
  61.  
  62. // Extract name and extension
  63. var dotIndex = originalName.lastIndexOf(".");
  64. if (dotIndex === -1) {
  65. alert("The document does not have an extension. Cannot determine file type.");
  66. throw new Error("No file extension");
  67. }
  68.  
  69. var nameWithoutExt = originalName.substring(0, dotIndex);
  70. var extension = originalName.substring(dotIndex);
  71.  
  72. // Create new filename with "_edit" appended
  73. newName = nameWithoutExt + "_edit" + extension;
  74. saveFile = File(originalPath + "/" + newName);
  75. } catch (e) {
  76. alert("Error accessing the document's path. Please choose a save location manually.\n\nError: " + e.message);
  77. saveFile = File.saveDialog("Choose a location to save the edited image", "All Files:*.*");
  78. if (saveFile === null) {
  79. alert("Save operation cancelled.");
  80. throw new Error("Save cancelled");
  81. }
  82.  
  83. // Append "_edit" to the filename
  84. var selectedName = saveFile.name;
  85. var dotIndex = selectedName.lastIndexOf(".");
  86. if (dotIndex === -1) {
  87. // No extension; append "_edit"
  88. newName = selectedName + "_edit";
  89. saveFile = new File(saveFile.path + "/" + newName);
  90. } else {
  91. var nameWithoutExt = selectedName.substring(0, dotIndex);
  92. var extension = selectedName.substring(dotIndex);
  93. newName = nameWithoutExt + "_edit" + extension;
  94. saveFile = new File(saveFile.path + "/" + newName);
  95. }
  96. }
  97. } else {
  98. // The document has not been saved before; prompt user to choose save location
  99. saveFile = File.saveDialog("Choose a location to save the edited image", "All Files:*.*");
  100. if (saveFile === null) {
  101. alert("Save operation cancelled.");
  102. throw new Error("Save cancelled");
  103. }
  104.  
  105. // Append "_edit" to the filename
  106. var selectedName = saveFile.name;
  107. var dotIndex = selectedName.lastIndexOf(".");
  108. if (dotIndex === -1) {
  109. // No extension; append "_edit"
  110. newName = selectedName + "_edit";
  111. saveFile = new File(saveFile.path + "/" + newName);
  112. } else {
  113. var nameWithoutExt = selectedName.substring(0, dotIndex);
  114. var extension = selectedName.substring(dotIndex);
  115. newName = nameWithoutExt + "_edit" + extension;
  116. saveFile = new File(saveFile.path + "/" + newName);
  117. }
  118. }
  119.  
  120. // Determine the save format based on extension
  121. var extension = newName.split('.').pop().toLowerCase();
  122. var saveOptions;
  123.  
  124. switch (extension) {
  125. case 'jpg':
  126. case 'jpeg':
  127. saveOptions = new JPEGSaveOptions();
  128. saveOptions.quality = 12;
  129. break;
  130. case 'png':
  131. saveOptions = new PNGSaveOptions();
  132. break;
  133. case 'tif':
  134. case 'tiff':
  135. saveOptions = new TiffSaveOptions();
  136. break;
  137. case 'psd':
  138. saveOptions = new PhotoshopSaveOptions();
  139. break;
  140. default:
  141. alert("Unsupported file format. Saving as PNG.");
  142. saveOptions = new PNGSaveOptions();
  143. // Ensure the file has a .png extension
  144. if (!newName.toLowerCase().endsWith('.png')) {
  145. saveFile = new File(saveFile.path + "/" + newName + ".png");
  146. }
  147. }
  148.  
  149. // Save the document
  150. try {
  151. doc.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);
  152. alert("Image saved successfully as:\n" + saveFile.fsName);
  153. } catch (e) {
  154. alert("Failed to save the image. Error: " + e.message);
  155. }
  156. }
  157.  
  158. saveDocument();
  159.  
  160. alert("Script completed successfully.");
Tags: photoshop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement