Advertisement
Guest User

Photoshop script to save the current selection to a PNG

a guest
Jul 7th, 2022
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. app.displayDialogs = DialogModes.NO;
  2.  
  3. var pngSaveOptions = new PNGSaveOptions();
  4. pngSaveOptions.compression = 9;
  5.  
  6. var hasSelection;
  7. var docRef;
  8. try {
  9. hasSelection = !!app.activeDocument.selection.bounds;
  10. } catch (err) {
  11. hasSelection = false;
  12. }
  13.  
  14. if (hasSelection) {
  15. app.activeDocument.selection.copy(true);
  16. var w = app.activeDocument.selection.bounds[2];
  17. var h = app.activeDocument.selection.bounds[3];
  18. docRef = app.documents.add(w, h, null, null, null, DocumentFill.TRANSPARENT);
  19. docRef.paste();
  20. } else {
  21. docRef = app.activeDocument;
  22. }
  23.  
  24. docRef.trim(TrimType.TRANSPARENT);
  25.  
  26. var file = File.saveDialog("Export as PNG to...");
  27. if (file && ((file.exists && confirm("Overwrite " + file +"?")) || !file.exists)) {
  28. docRef.saveAs(file, pngSaveOptions, !hasSelection, Extension.LOWERCASE);
  29. if (hasSelection) {
  30. docRef.close(SaveOptions.DONOTSAVECHANGES);
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement