Created
November 24, 2023 18:32
-
-
Save Doppelkeks/a98efa299ccd00a0ef61dd4827f83181 to your computer and use it in GitHub Desktop.
Export a selected group as .png in Photoshop (Script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check if Photoshop is active and a document is open | |
if (app.documents.length > 0) { | |
var doc = app.activeDocument; | |
var layerVisibility = []; | |
// Function to check if the active layer is a group | |
function isLayerGroup(layer) { | |
try { | |
var temp = layer.layers.length; | |
return true; | |
} catch (e) { | |
return false; | |
} | |
} | |
// Function to set all layers visibility | |
function setAllLayersVisibility(restore) { | |
for (var i = 0; i < doc.layers.length; i++) { | |
var layerVisible = doc.layers[i].visible; | |
if(!restore){ | |
if(layerVisibility.length >= doc.layers.length){ | |
layerVisibility.push(layerVisible); | |
}else{ | |
layerVisibility[i] = layerVisible; | |
} | |
doc.layers[i].visible = false; | |
}else{ | |
doc.layers[i].visible = layerVisibility[i]; | |
} | |
} | |
} | |
// Check if the active layer is a group | |
if (isLayerGroup(doc.activeLayer)) { | |
var selectedGroup = doc.activeLayer; | |
// Prepare the export options for PNG | |
var exportOptions = new ExportOptionsSaveForWeb(); | |
exportOptions.format = SaveDocumentType.PNG; | |
exportOptions.PNG8 = false; // Use PNG-24 for better quality | |
exportOptions.transparency = true; | |
exportOptions.interlaced = false; | |
// Set the initial file name and path | |
var groupName = selectedGroup.name; | |
var desktopPath = Folder.desktop; | |
var initialPath = new File(desktopPath + "/" + groupName + ".png"); | |
// Prompt user to select save location and file name | |
var saveFile = initialPath.saveDlg("Save PNG file"); | |
// Proceed only if the user selects a file path | |
if (saveFile != null) { | |
// Hide all layers except the selected group | |
setAllLayersVisibility(false); | |
selectedGroup.visible = true; | |
// Export the visible group as PNG | |
doc.exportDocument(saveFile, ExportType.SAVEFORWEB, exportOptions); | |
// Restore the visibility of all layers | |
setAllLayersVisibility(true); | |
} | |
} else { | |
alert("Please select a group to export."); | |
} | |
} else { | |
alert("No document is open in Photoshop."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why is this still needed in newer PS Versions?