Interesting script, hard to make?! hmm

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

NoomForPhotoshop
Posts: 6
Joined: Thu Sep 03, 2020 6:06 pm

Interesting script, hard to make?! hmm

Post by NoomForPhotoshop »

SO I have a bunch of demands but from what I have to do I am struggling with couple of issues here:

How to read content from a text file on a computer and >> load it into a variable.


How to exactly check if a layer exist?
How to execute a action if it does exist?
how to execute another action if it does not exist?

when I say action, I refer to the actions recorded and saved in photoshop actions window.


Also one interestin request the client has is "how to record zoom coordinates and then repeat them on another document?"

Here is the full descripton of what he wants:



SCRIPT 0 Get $WORKINGLAYER variable from the NAME.txt file.

SCRIPT 1. Check if layer with name=$WORKINGLAYER exists, If Not existing then Select BAkcgounrd layer.
Execute function from the Photoshop Actions: "NOTEXISTS"
IF the Layer exists: Change name of $WORKINGLAYER to "0" Execute function "ALREADYEXISTS"
once the action ALREADY EXISTS has been executed (check if succesfull by checking if there is a layer with name "Background copy 2".
If successful then change name of layer Background copy 2 to> $WORKINGLAYER
if not successful give errror: FUNCTION NOT COMPLETED.



Script 2 script is requred:
Check if layer with name=$WORKINGLAYER exists, if yes, select layer mask, and activate Brush tool (with hardness 100% and color ffffff")
if not, give error that says "Nothing painted yet, nothing to add painting to "

Script 3 is requred:
Check if layer with name=$WORKINGLAYER exists, if yes, select layer mask, and activate Brush tool (with hardness 100% and color 000000")
if not, give error that says "Nothing painted yet, nothing to erase"


Script 4 is requred:
Check if layer with name=$WORKINGLAYER exists, if yes, delete layer
if not, give error that says "Nothing painted yet, no layer to delete"

Script 5 is requred:
Make all layers visible
Save all opened PSDs files

Script 6 is requred:
Make all layers visible
Save all opened PSDs files
Close all opened files (not the photoshop program)


Script 8 is required
Record zoom coordinates and save them,
Replicate the same actionthe exact same zoom on another image with one keystroke
User avatar
Scriptor
Posts: 24
Joined: Tue Oct 01, 2019 12:07 pm

Re: Interesting script, hard to make?! hmm

Post by Scriptor »

Hey NoomForPhotoshop - I Cannot help you with all questions - but here's what I can help you with, and it should get you started;

How to read content from a textfile on a computer and load it into a variable?
This one's a bit tricky, but this might work;

Code: Select all

var Textfile= new File("file:///C://asd.txt");					//Assigns new path to "Textfile"
TextVariable= "";												//Resets TextVariable
Textfile.open('r');												//Opens Text in read-mode
while(!Textfile.eof)											//While open, for each line;
TextVariable+= Textfile.readln() + "\n";						//Adds everything into TextVariable
Textfile.close();	

alert(TextVariable);

How to exactly check if a layer exists?
I think you can read this this thread to get a better understanding - javascript doesn't search for layer names that's inside folders ect, so take a look and see if you can find what you're looking for here;
https://community.adobe.com/t5/photosho ... 661?page=1

How to execute an action if it exists? And another if it doesn't exists?

Code: Select all

try {
	app.doAction("Action name","Action set name.ATN");
} catch(error) {		//If fail
	app.doAction("Other action name","Action set name.ATN");
}
Hope it helps a bit! I might have missed something, I haven't tested this code - just tell me if you encounter some problem!
NoomForPhotoshop
Posts: 6
Joined: Thu Sep 03, 2020 6:06 pm

Re: Interesting script, hard to make?! hmm

Post by NoomForPhotoshop »

Hello I have hired a developer to make a script that does these things:

HEre is the script:

Now the problem with this script is that it executes action "NOTEXISTS" twice. I have no idea why.

0


This code is made in JavaScript to be run by Photoshop to do the following described below.

Here is the code of the script a developer wrote to me. What this code does:

Load TITLE.txt content into > variable $workingLayer 1 check if such layer exists> if not execute action "NOTEXISTS" from the actions in Photoshop.

2 Action after doing some funny things ends up with creating a new layer with layer name "0".

3 Then the script should change the "0" into whatever is inside of the workingLayer variable loaded fromt TITLE.txt

If it exists: 1 change the layer name of layer "$workingLayer" into "0"

2 execute the action "EXISTS"

3 change the layer name back from "0" into "$workingLayer"

so I tested this after some kind gentleman here helped me fix some syntax errors.

The problem now is that the action executes the action "NOTEXISTS" twice

I wonder why... hmmm

Here is the entire code:

Code: Select all

function somename(){
    #target photoshop
    
    var fileRef = "C:/Users/Dharmindar/OneDrive/Desktop/NAME.txt"
    var doc = open(fileRef);
    alert(doc);
}
// this function somename is supposably testing if the name.txt can be opened and it gives an alert 

// this is anther test function I guess to see how to get layers name or document name I am not sure what he did here

function testFunction() {
    var testName = app.activeDocument.name;
    var nameWithoutExt = testName.split(".")[0];
    nameWithoutExt = nameWithoutExt + ", " + app.activeDocument.layers[0].name;
    alert(nameWithoutExt);
}






   //here he lists files from folder desktop and then he loops and searches for a file named TITLE.txt. from there he reads the value that he puts into a layer workingLayer. Later he will execute function based on the fact if the layer exists or not.
function SomeOtherFunction() {
    //Choose the desired folder
    var currentFolder = new Folder('~/desktop/');

    //Get files from the selected folder
    var currentFiles = currentFolder.getFiles(); 
    var selectedFile;

    for (i = 0; i < currentFiles.length; i++) {
        if  (currentFiles[i] == "~/desktop/NAME.txt") {
          selectedFile = currentFiles[i];
        }
    }


 // here he reads the file he opens and equates it to working layer
        selectedFile.open("r");
        var workingLayer = selectedFile.read();
    
        alert(workingLayer);

    var layers = app.activeDocument.layers;
    //alert(layers);

    for (l = 0; l < layers.length; l++) {
        //alert(layers[l].name + " - " + str); 
        var currentLayer = layers[l].name;
         // here he conditions and says if the working layer is equal to the current layer name then execute action exists from custom made 1145Actionset and later he says else, if it does not exist the layer with equal name as working layer then execute action 

        if(currentLayer.name == workingLayer) {
            currentLayer.name = "0";
            ExecuteAction("EXISTS","1145ActionSet");
        } else {
            ExecuteAction("NOTEXISTS","1145ActionSet");
        }

        RenameLayer("0", workingLayer, layers);
    }
}


//here he created function rename layer which is supposed to change layer names after the action has been completed and why was this needed? well it was needed because photoshop actions can not work with variables so what he did he changes the layer name to "0" and then executes action always on layer "0" and then after that he changes the layer 0 back to workingLayer variable value that comes from that TITLE.txt and that is pretty much all there is to it and this weird thing gives me error exactly on the line 60 it says it expects ; 
functon RenameLayer(oldName, newName, layers) {
    for (var j=0;j < layers.length; j++) {
        if (layers[j].name == oldName ){
            layers[j].name = newName;
        }
    }
}


function ExecuteAction(actionName, actionSetName) {
    try {
        //TO DO IF THE LAYER DOESNOT EXISTS
        //Action set must be created first and the action should be in that.
        //The first parameter is the name of th action to be performed and the second parameter is the name of th ActionSet
        app.doAction(actionName,actionSetName);
    } catch(e) {
        alert(e);
    }
}


SomeOtherFunction();
NoomForPhotoshop
Posts: 6
Joined: Thu Sep 03, 2020 6:06 pm

Re: Interesting script, hard to make?! hmm

Post by NoomForPhotoshop »

Hello anyone?
NoomForPhotoshop
Posts: 6
Joined: Thu Sep 03, 2020 6:06 pm

Re: Interesting script, hard to make?! hmm

Post by NoomForPhotoshop »

This is what we did now but now it stupidly instead of changing layers name from 0 to the variable value workingLAyer (which was loaded in from a text file) it stupidly somehow leaves layer 0 as it is and changes another layers name. :x please anyone help?????????

function somename(){
#target photoshop

var fileRef = "C:/Users/Dharmindar/OneDrive/Desktop/NAME.txt"
var doc = open(fileRef);
alert(doc);
}



function testFunction() {
var testName = app.activeDocument.name;
var nameWithoutExt = testName.split(".")[0];
nameWithoutExt = nameWithoutExt + ", " + app.activeDocument.layers[0].name;
alert(nameWithoutExt);
}





function SomeOtherFunction() {
//Choose the desired folder
var currentFolder = new Folder('~/desktop/');

//Get files from the selected folder
var currentFiles = currentFolder.getFiles();
var selectedFile;

for (i = 0; i < currentFiles.length; i++) {
if (currentFiles == "~/desktop/NAME.txt") {
selectedFile = currentFiles;
}
}

selectedFile.open("r");
var workingLayer = selectedFile.read();

var layers = app.activeDocument.layers;

for (l = 0; l < layers.length; l++) {
alert(layers[l].name);
alert(workingLayer);
if(layers[l].name.toUpperCase() == workingLayer.toUpperCase()) {
alert("Executing Exists.");
layers[l].name = "0";

ExecuteAction("EXISTS","1145ActionSet");
layers[l].name = workingLayer;
break;
} else {
alert("Executing Not Exists.");
ExecuteAction("NOTEXISTS","1145ActionSet");
layers[l].name = workingLayer;
break;
}
}
}



function SelectLayerMaskAndPerformAction() {
//Choose the desired folder
var currentFolder = new Folder('~/desktop/');

//Get files from the selected folder
var currentFiles = currentFolder.getFiles();
var selectedFile;

for (i = 0; i < currentFiles.length; i++) {
if (currentFiles == "~/desktop/NAME.txt") {
selectedFile = currentFiles;
}
}

selectedFile.open("r");
var workingLayer = selectedFile.read();

var layers = app.activeDocument.layers;

for (l = 0; l < layers.length; l++) {
if(layers[l].name.localeCompare(workingLayer)) {
// TO DO here.
alert("Adding Layer mask.");
addMask();
alert("Added Layer mask.");
}
}
}



function addMask() {
var idMk = charIDToTypeID( "Mk " );
var desc2 = new ActionDescriptor();
var idNw = charIDToTypeID( "Nw " );
var idChnl = charIDToTypeID( "Chnl" );
desc2.putClass( idNw, idChnl );
var idAt = charIDToTypeID( "At " );
var ref1 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref1.putEnumerated( idChnl, idChnl, idMsk );
desc2.putReference( idAt, ref1 );
var idUsng = charIDToTypeID( "Usng" );
var idUsrM = charIDToTypeID( "UsrM" );
var idHdAl = charIDToTypeID( "HdAl" );
desc2.putEnumerated( idUsng, idUsrM, idHdAl );
executeAction( idMk, desc2, DialogModes.NO );
}



// function RenameLayerNew(oldName, newName, layers) {
// alert("Rename starting...");
// alert("Old Layer name : " + oldName);
// for (var currentLayer = 0; currentLayer < layers.length; currentLayer++) {
// alert("Current Layer: " + layers[currentLayer].name);
// if (layers[currentLayer].name.localeCompare(oldName)) {
// layers[currentLayer].name = newName;
// break;
// }
// }
// }


// function RenameLayer(oldName, newName, layers) {
// for (var j=0;j < layers.length; j++) {
// var currentLayer = layers[j].name;
// if (currentLayer.localeCompare(oldName)){
// layers[j].name = newName;
// }
// }
// }


function ExecuteAction(actionName, actionSetName) {
try {
//TO DO IF THE LAYER DOESNOT EXISTS
//Action set must be created first and the action should be in that.
//The first parameter is the name of th action to be performed and the second parameter is the name of th ActionSet
app.doAction(actionName,actionSetName);
} catch(e) {
alert(e);
}
}


SomeOtherFunction();

//SelectLayerMaskAndPerformAction();