• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Create new doc if is not already open

Contributor ,
Dec 17, 2022 Dec 17, 2022

Copy link to clipboard

Copied

I am trying hours without success..

I want to create a new document, if it is not already an opened document with the same name

The following always creates another document Temp100x70.psd, even if a Temp100x70.psd is already open

What I am doing wrong!

var originalUnit = app.preferences.rulerUnits;
preferences.rulerUnits = Units.CM;

var fName= "";

for (var i = 0; i < app.documents.length; i++) {
    var fName = app.documents[i].name
  if(fName.match(/(.*Temp100x70.psd).*/i)) { 
        app.activeDocument = app.documents.getByName(fName)
  } else {
 app.documents.add(100, 70, 72, "Temp100x70.psd")
 break;
} }

 

TOPICS
Actions and scripting

Views

478

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

correct answers 1 Correct answer

Community Expert , Dec 18, 2022 Dec 18, 2022

@siomosp – Does this work for you? I have tested with unsaved docs:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/create-new-doc-if-is-not-already-open/td-p/13428283
*/

#target photoshop;

if (documents.length) {

	// Set the open documents name variable
	var theNames = getDocInfo().join(",");

	//alert(theNames);
	//$.writeln(theNames);

	// Test the open documents array for an open doc named: Temp100x70.psd
	if (/Temp100x70.psd/i.test(theNames)) {
		// Do nothing...
		al
...

Likes

Translate

Translate
LEGEND ,
Dec 17, 2022 Dec 17, 2022

Copy link to clipboard

Copied

Unless the file is saved already, it doesn't exist. That's one of those quirks you have to account for.

 

Mylenium

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
Community Expert ,
Dec 17, 2022 Dec 17, 2022

Copy link to clipboard

Copied

@Mylenium – I don't believe that is it as @siomosp isn't using .path() or .fullName() in the code.

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
Contributor ,
Dec 17, 2022 Dec 17, 2022

Copy link to clipboard

Copied

I think @Mylenium  is right
Similar code works fine for an existing file

var myFileList = [
    'C:/BackgroundsSync/_usefull/Temp100x70.psd',
    'C:/Temp100x70.psd'
];
for (var i = 0; i < app.documents.length; i++) {
    var fName = app.documents[i].name
    if(fName.match(/(.*Temp100x70.psd).*/i)) { 
        app.activeDocument = app.documents.getByName(fName)
    } else {
        for (var a in myFileList) {
            var currentFile = new File(myFileList[a])
            if (currentFile.exists) {
                app.open(currentFile)
                break;
            };
        }
    }
}

Anyway, if someone finds another solution, it would be great 

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
Community Expert ,
Dec 18, 2022 Dec 18, 2022

Copy link to clipboard

Copied

I haven't had any success either.

 

The following lists all open docs, one by one by name, whether they are saved or not, which is why I was thinking that it shouldn't matter whether the file has been saved.

 

for (var i = 0; i < documents.length; i++) {
var theDoc = app.documents[i];
app.activeDocument = theDoc;
    alert(theDoc.name);
}

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
Community Expert ,
Dec 18, 2022 Dec 18, 2022

Copy link to clipboard

Copied

@siomosp – Does this work for you? I have tested with unsaved docs:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/create-new-doc-if-is-not-already-open/td-p/13428283
*/

#target photoshop;

if (documents.length) {

	// Set the open documents name variable
	var theNames = getDocInfo().join(",");

	//alert(theNames);
	//$.writeln(theNames);

	// Test the open documents array for an open doc named: Temp100x70.psd
	if (/Temp100x70.psd/i.test(theNames)) {
		// Do nothing...
		alert("The doc exists, do nothing!");
	} else {
		// Do something...
		alert("The doc doesn't exist!" + "\r" + "Create the temp doc...");
	}


	function getDocInfo() {
		/*
		Function modified from the original by Supermerlin:
		https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-list-of-all-photoshop-documents/m-p/9552414
		*/
		var IDs = new Array();
		var count = app.documents.length;
		for (var a = 1; a < count + 1; a++) {
			var ref = new ActionReference();
			ref.putIndex(charIDToTypeID('Dcmn'), a);
			var desc = executeActionGet(ref);
			var Title = desc.getString(stringIDToTypeID('title'));
			IDs.push([
				Title
			]);
		}
		return IDs;
	}

} else {
	alert("A document must be open to run this script!");
}

 

Edit:

 

And here it is in standard DOM code rather than AM code:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/create-new-doc-if-is-not-already-open/td-p/13428283
*/

#target photoshop;

if (documents.length) {

	// Set the open documents name variable
	var theNames = getDocName().join(",");

	//alert(theNames);
	//$.writeln(theNames);

	// Test the open documents array for an open doc named: Temp100x70.psd
	if (/Temp100x70.psd/i.test(theNames)) {
		// Do nothing...
		alert("The doc exists, do nothing!");
	} else {
		// Do something...
		alert("The doc doesn't exist!" + "\r" + "Create the temp doc...");
	}


	function getDocName() {
		var IDs = new Array();
		var count = app.documents.length;
		for (var a = 1; a < count + 1; a++) {
			var Title = activeDocument.name;
			IDs.push([
				Title
			]);
		}
		return IDs;
	}

} else {
	alert("A document must be open to run this script!");
}

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
Contributor ,
Dec 23, 2022 Dec 23, 2022

Copy link to clipboard

Copied

Hi!

The AM code detects the name Temp100x70.psd if exists

But how can I activate it ?

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
Community Expert ,
Dec 23, 2022 Dec 23, 2022

Copy link to clipboard

Copied

Both the DOM and AM code should detect the name.

 

To activate, do similar to what you were originally doing, this time using a string rather than a variable:

 

	// Test the open documents array for an open doc named: Temp100x70.psd
	if (/Temp100x70.psd/i.test(theNames)) {
		// Set the active document...
		activeDocument = documents.getByName("Temp100x70.psd");
	} else {
		// Create the temp doc...
        documents.add(100, 70, 72, "Temp100x70.psd");
	}

 

P.S. I think you need to look into the code for documents.add() to see if it performs as you expect...

 

Edit – Here it is in full, just in case the documentation isn't clear:

 

documents.add(UnitValue(100, "px"), UnitValue(70, "px"), 72, "Temp100x70.psd", NewDocumentMode.RGB, DocumentFill.WHITE, 1, BitsPerChannelType.EIGHT, "sRGB IEC61966-2.1");

 

You can sometimes use a value of null or undefined instead. 

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
Contributor ,
Dec 23, 2022 Dec 23, 2022

Copy link to clipboard

Copied

LATEST

The name is detected, (only with your first AM code, not in your second standard DOM code) but I need to activate the temp100x70.psd, when the active document is another document

Actually, I did it, modifying the https://stackoverflow.com/questions/67467344/find-item-from-an-array-adobe-cep
It is "kindergarten" code, but works

 

 

 

app.preferences.rulerUnits = Units.CM;
// Set the open documents name variable
	var theNames = getDocInfo().join(",");
	// Test the open documents array for an open doc named: Temp100x70.psd
	if (/Temp150x70.psd/i.test(theNames)) {

var array = new Array(theNames);
var found   = "";
var sample  = "Temp150x70.psd";
var counter = 0;
while (counter<array.length) {
  if (array[counter] = sample) {
    found = sample;
    break;
  }
  counter++;
}

if (found != "") {
	app.activeDocument = app.documents.getByName(found)
} else {
//  alert("Nothing was found");
}	
	
	} else {
	// Do something...
	 app.documents.add(150, 70, 72, "Temp150x70.psd")
	}
			}
				
				
	function getDocInfo() {
		/*
		Function modified from the original by Supermerlin:
		https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-list-of-all-photoshop-documents/m-p/9552414
		*/
		var IDs = new Array();
		var count = app.documents.length;
		for (var a = 1; a < count + 1; a++) {
			var ref = new ActionReference();
			ref.putIndex(charIDToTypeID('Dcmn'), a);
			var desc = executeActionGet(ref);
			var Title = desc.getString(stringIDToTypeID('title'));
			IDs.push([
				Title
			]);
		}
		return IDs;
	}				

 

 

 

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