Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Need Help, Clean Up Code? Loop Open Tabs, Save Stack in Group

Explorer ,
Jul 20, 2021 Jul 20, 2021

Copy link to clipboard

Copied

Below is my code. I know it isnt clean. 

I need someone to help me make this a cleaner code. 

 

It works, but doesn't when I try to add it to a dialog box (button). I call to a functions often because I'm awful at coding lol. Can someone help?

 

It loops through all open tabs, looks for a group called "swatches" and saves the layers stack in the group "swatches" as individual tifs. 

 

 

 

#target photoshop;
app.bringToFront();

// ===============================================================
// NOTE: GO THRU EACH DOCUMENT AND SAVE OUT LAYERS BY DOCNAME & layerIndex:
// NOTE: MUST SAVE DOCUMENT IN LOCATION FIRST FOR CODE TO WORK
// ===============================================================

openDocuments();

function openDocuments() {

	alert( "IMPORTANT:" + "\n\n" + "1. Save PSD First." + "\n" + "2. Save Stack in Group Called Swatches." + "\n\n" + "NOTE: Script will not work otherwise." + "\n" + "STACK ORDER: Saves Top Layer - Bottom Layer." );

	var docLength = app.documents.length;

	for ( var i = 0; i < docLength; i++ ) {
		if ( i === docLength ) {
			break;
		}

		var doc = documents[ i ];
		app.activeDocument = doc;

		// NOTE: ===============================================================
		// NOTE: call to function below:
		// NOTE: ===============================================================

		saveTIF();

		// NOTE: ===============================================================
		// NOTE: ===============================================================

	}

	alert( "STACKS Saved: " + "\n\n" + i + " Opened Documents" );
}

// NOTE: ===============================================================
// NOTE: below is the save stack tiff files
// NOTE: ODS stands for Open Document's Stacks!
// NOTE: ===============================================================

function saveTIF() {

	var doc = app.activeDocument;
	var docPath = doc.path;
	var docName = doc.name.replace( /\.[^\.]+$/, '' );

	// =======================================================
	// NOTE: FINDS LAYER CALLED SWATCHES
	// IF IT DOESNT EXIST - SCRIPT WONT DO ANYTHING
	// I DIDNT ADD A PROMPT ERROR TO LET YOU KNOW
	// =======================================================

	doc.activeLayer = doc.layers.getByName( "Swatches" );

	if ( app.documents.length > 0 ) {
		if ( app.activeDocument.activeLayer.layers ) {} else {
			alert( "OOPS! \nLayer Group not Selected!!!" );

		}
	}

	var group = doc.activeLayer;
	var groupLength = group.layers.length;

	for ( var i = 0; i < groupLength; i++ ) {
		group.layers[ i ].visible = false;
	}

	for ( i = 0; i < groupLength; i++ ) {

		var layer = group.layers[ i ];
		var layerIndex = i + 1;
		// var layerName = layer.name.slice( 0, -4 );  Omit the file extension.

		layer.visible = true; //////////////////////////////////////////

		var folderString = docPath + "/" + "TIF_Stack";
		if ( Folder( folderString ).exists == false ) {
			new Folder( folderString ).create();
		}

		TiffOpts = new TiffSaveOptions();
		TiffOpts.embedColorProfile = true;
		TiffOpts.alphaChannels = false;
		TiffOpts.imageCompression = TIFFEncoding.NONE;
		TiffOpts.layers = false;
		TiffOpts.spotColors = true;

		doc.saveAs( ( new File( folderString + "/" + docName + "_" + layerIndex + ".tif" ) ), TiffOpts, true );

		layer.visible = false; //////////////////////////////////////////

	}

	//app.refresh();
	//doc.save();
	//doc.close( SaveOptions.SAVECHANGES );
	//doc.close();
	//doc.close(SaveOptions.DONOTSAVECHANGES);
}

 

 

 

TOPICS
Actions and scripting, macOS

Views

102

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 20, 2021 Jul 20, 2021

Copy link to clipboard

Copied

I should note that this script does exactly what I need it to do, I just want the script to be optimized i guess. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 20, 2021 Jul 20, 2021

Copy link to clipboard

Copied

I don't know if your code is the best, but I think it's clean enough.
Is there a problem with the code that works with the buttons on the dialog?
Is the dialog a ScriptUI?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

yes, its script ui. 

 

my button is calling to 

openDocuments();

but it simply wont run. It just does nothing. But if i run it as is through photoshop - it works fine. It's weird. That's why i thought my first function openDocuments(); calling to saveTif function was causing the issue. I thought if it could be consolidated, then perhaps it would work better?

 

button2.onClick = function () {
openDocuments();
};

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

Did you know that the modal dialog must be closed before the next step can be performed? Try adding a process to close the dialog in the function set for the button.

like,

button2.onClick=function(){

this.parent.close();

openDocuments();

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

I did not know that. Ill keep you posted and see if that works.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 29, 2021 Jul 29, 2021

Copy link to clipboard

Copied

LATEST

So, I tried this. I wanted to make sure it wasnt something I was doing... For some reason it still wont run the script. I will post a sample w/UI in a bit, so you can see what I mean. There shouldnt be any reason why it doesnt work, but it actually just does nothing. Even with the close option.  Hold tight.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines