Highlighted

[Scripting] Select specific document (instead of previous, next)

Community Beginner ,
Aug 17, 2020

Copy link to clipboard

Copied

I cannot for the life of me find a way to select a specific document, either by name or otherwise, with scripting. The only thing close I have found is select next/select previous document commands. This is a problem, because it means the document I run my script on needs to be in a specific order to work. 

 

How can I open a specific document by name?

 

Edit: Figured it out by storing the current doc name as a var equal to app.activeDocument, then calling my var as the active doc.

Most Valuable Participant
Correct answer by JJMack | Most Valuable Participant

That sound more like you are recording an action. Action Only have a small set of conditions they can test for. A photoshop script would have no problem doing what you want to do.   They can switch to any open document they want to  and cam open any file they want to add to the open documents in photoshop.

 

Scripting is programming a photoshop process.  Here is a script I wrote that will always switch to  my open clipboard document or create the Document.

/* ==========================================================
// 2013  John J. McAssey (JJMack) 
// ======================================================= */
// This script is supplied as is. It is provided as freeware. 
// The author accepts no liability for any problems arising from its use.

// I set a shortcut key in my case F5 to this Photoshop script mainly for doing Window screen capturing of:
// desktop displays, Active window and selected area copy to clipboard. The script log the cardboard into a document with the name clipboard. 

/*
<javascriptresource>
<about>$$$/JavaScripts/AppendClipboard/About=JJMack's Append Clipboard into Clipboard document.^r^rCopyright 2014 Mouseprints.^r^rJJMack's Script.^r
NOTE:New Clipboard Document will be open if necessary</about>
<category>JJMack's Script</category>
</javascriptresource>
*/

// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events
app.bringToFront();

if (!documents.length) {
	NewCliboardDoc (); 
	app.activeDocument.suspendHistory('appendClipboard','main(true)'); // New document opened proceed
	}
else { 	//locate Document clipboard
	for (var i=0;i<app.documents.length;i++) { 
		app.activeDocument=app.documents[i];
		if (app.activeDocument.name == "Clipboard"){break;}
		}
	if (app.activeDocument.name != "Clipboard"){
		NewCliboardDoc ();
		app.activeDocument.suspendHistory('appendClipboard','main(true)'); // New document opened proceed
		}
	else {app.activeDocument.suspendHistory('appendClipboard','main(false)'); }	// Old Clipboard Document found
	}

///////////////////////////////////////////////////////////////////////////////
//                            main function                                  //
///////////////////////////////////////////////////////////////////////////////
function main(newFile) {
	// declare local variables
	var orig_ruler_units = app.preferences.rulerUnits;
	app.preferences.rulerUnits = Units.PIXELS;			// Set the ruler units to PIXELS

	try { 
		activeDocument.paste(); 					    // past in clipboard may fail enclose in try
		var LB = app.activeDocument.activeLayer.bounds;	// Get the bounds of the work layer
		var LWidth = (LB[2].value) - (LB[0].value);		// Area width
		var LHeight = (LB[3].value) - (LB[1].value);	// Area height
		// BOTTOMCENTER BOTTOMLEFT BOTTOMRIGHT MIDDLECENTER MIDDLELEFT MIDDLERIGHT TOPCENTER TOPLEFT TOPRIGHT
		if(newFile) {
			app.activeDocument.resizeCanvas(LWidth, LHeight, AnchorPosition.TOPLEFT);
			app.activeDocument.layers[1].remove();
			}
		else {
			if (LWidth>=app.activeDocument.width) {	app.activeDocument.resizeCanvas(LWidth, app.activeDocument.height + LHeight, AnchorPosition.TOPLEFT);}
			else {app.activeDocument.resizeCanvas(app.activeDocument.width, app.activeDocument.height + LHeight, AnchorPosition.TOPLEFT);}
			}
		app.activeDocument.selection.selectAll();		// select all
		// Left('AdLf'); Right('AdRg'); Top('AdTp'); Bottom('AdBt'); Center Horizontal('AdCH'); Center Vertical('AdCV');
		align("AdLf");align("AdBt");					//Align to selection Left bottom
		app.activeDocument.selection.deselect();
		SetViewFitonScreen();
		//app.purge(PurgeTarget.CLIPBOARDCACHE);
		}
	catch(e) { 
		if(newFile) app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
		alert("Clipboard Empty"); 
		}		

	app.preferences.rulerUnits = orig_ruler_units;			// Reset units to original settings
	}
///////////////////////////////////////////////////////////////////////////////
//                           main function end                               //
///////////////////////////////////////////////////////////////////////////////
function NewCliboardDoc () {
	// app.documents.add([width][, height][, resolution][, name][, mode][, initialFill][, pixelAspectRatio][, bitsPerChannel][, colorProfileName])
	var Clipboard = app.documents.add( null, null, 100, "Clipboard", NewDocumentMode.RGB, null, null, null, "sRGB IEC61966-2.1");
	}

function align(method) {
	var desc = new ActionDescriptor();
			var ref = new ActionReference();
			ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
		desc.putReference( charIDToTypeID( "null" ), ref );
		desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );
	executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );
	};

function SetViewFitonScreen() {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(charIDToTypeID('Mn  '), charIDToTypeID('MnIt'), charIDToTypeID('FtOn'));
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
};

Topics

Actions and scripting, SDK

Views

124

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

[Scripting] Select specific document (instead of previous, next)

Community Beginner ,
Aug 17, 2020

Copy link to clipboard

Copied

I cannot for the life of me find a way to select a specific document, either by name or otherwise, with scripting. The only thing close I have found is select next/select previous document commands. This is a problem, because it means the document I run my script on needs to be in a specific order to work. 

 

How can I open a specific document by name?

 

Edit: Figured it out by storing the current doc name as a var equal to app.activeDocument, then calling my var as the active doc.

Most Valuable Participant
Correct answer by JJMack | Most Valuable Participant

That sound more like you are recording an action. Action Only have a small set of conditions they can test for. A photoshop script would have no problem doing what you want to do.   They can switch to any open document they want to  and cam open any file they want to add to the open documents in photoshop.

 

Scripting is programming a photoshop process.  Here is a script I wrote that will always switch to  my open clipboard document or create the Document.

/* ==========================================================
// 2013  John J. McAssey (JJMack) 
// ======================================================= */
// This script is supplied as is. It is provided as freeware. 
// The author accepts no liability for any problems arising from its use.

// I set a shortcut key in my case F5 to this Photoshop script mainly for doing Window screen capturing of:
// desktop displays, Active window and selected area copy to clipboard. The script log the cardboard into a document with the name clipboard. 

/*
<javascriptresource>
<about>$$$/JavaScripts/AppendClipboard/About=JJMack's Append Clipboard into Clipboard document.^r^rCopyright 2014 Mouseprints.^r^rJJMack's Script.^r
NOTE:New Clipboard Document will be open if necessary</about>
<category>JJMack's Script</category>
</javascriptresource>
*/

// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events
app.bringToFront();

if (!documents.length) {
	NewCliboardDoc (); 
	app.activeDocument.suspendHistory('appendClipboard','main(true)'); // New document opened proceed
	}
else { 	//locate Document clipboard
	for (var i=0;i<app.documents.length;i++) { 
		app.activeDocument=app.documents[i];
		if (app.activeDocument.name == "Clipboard"){break;}
		}
	if (app.activeDocument.name != "Clipboard"){
		NewCliboardDoc ();
		app.activeDocument.suspendHistory('appendClipboard','main(true)'); // New document opened proceed
		}
	else {app.activeDocument.suspendHistory('appendClipboard','main(false)'); }	// Old Clipboard Document found
	}

///////////////////////////////////////////////////////////////////////////////
//                            main function                                  //
///////////////////////////////////////////////////////////////////////////////
function main(newFile) {
	// declare local variables
	var orig_ruler_units = app.preferences.rulerUnits;
	app.preferences.rulerUnits = Units.PIXELS;			// Set the ruler units to PIXELS

	try { 
		activeDocument.paste(); 					    // past in clipboard may fail enclose in try
		var LB = app.activeDocument.activeLayer.bounds;	// Get the bounds of the work layer
		var LWidth = (LB[2].value) - (LB[0].value);		// Area width
		var LHeight = (LB[3].value) - (LB[1].value);	// Area height
		// BOTTOMCENTER BOTTOMLEFT BOTTOMRIGHT MIDDLECENTER MIDDLELEFT MIDDLERIGHT TOPCENTER TOPLEFT TOPRIGHT
		if(newFile) {
			app.activeDocument.resizeCanvas(LWidth, LHeight, AnchorPosition.TOPLEFT);
			app.activeDocument.layers[1].remove();
			}
		else {
			if (LWidth>=app.activeDocument.width) {	app.activeDocument.resizeCanvas(LWidth, app.activeDocument.height + LHeight, AnchorPosition.TOPLEFT);}
			else {app.activeDocument.resizeCanvas(app.activeDocument.width, app.activeDocument.height + LHeight, AnchorPosition.TOPLEFT);}
			}
		app.activeDocument.selection.selectAll();		// select all
		// Left('AdLf'); Right('AdRg'); Top('AdTp'); Bottom('AdBt'); Center Horizontal('AdCH'); Center Vertical('AdCV');
		align("AdLf");align("AdBt");					//Align to selection Left bottom
		app.activeDocument.selection.deselect();
		SetViewFitonScreen();
		//app.purge(PurgeTarget.CLIPBOARDCACHE);
		}
	catch(e) { 
		if(newFile) app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
		alert("Clipboard Empty"); 
		}		

	app.preferences.rulerUnits = orig_ruler_units;			// Reset units to original settings
	}
///////////////////////////////////////////////////////////////////////////////
//                           main function end                               //
///////////////////////////////////////////////////////////////////////////////
function NewCliboardDoc () {
	// app.documents.add([width][, height][, resolution][, name][, mode][, initialFill][, pixelAspectRatio][, bitsPerChannel][, colorProfileName])
	var Clipboard = app.documents.add( null, null, 100, "Clipboard", NewDocumentMode.RGB, null, null, null, "sRGB IEC61966-2.1");
	}

function align(method) {
	var desc = new ActionDescriptor();
			var ref = new ActionReference();
			ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
		desc.putReference( charIDToTypeID( "null" ), ref );
		desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );
	executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );
	};

function SetViewFitonScreen() {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(charIDToTypeID('Mn  '), charIDToTypeID('MnIt'), charIDToTypeID('FtOn'));
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
};

Topics

Actions and scripting, SDK

Views

125

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
Most Valuable Participant ,
Aug 17, 2020

Copy link to clipboard

Copied

That sound more like you are recording an action. Action Only have a small set of conditions they can test for. A photoshop script would have no problem doing what you want to do.   They can switch to any open document they want to  and cam open any file they want to add to the open documents in photoshop.

 

Scripting is programming a photoshop process.  Here is a script I wrote that will always switch to  my open clipboard document or create the Document.

/* ==========================================================
// 2013  John J. McAssey (JJMack) 
// ======================================================= */
// This script is supplied as is. It is provided as freeware. 
// The author accepts no liability for any problems arising from its use.

// I set a shortcut key in my case F5 to this Photoshop script mainly for doing Window screen capturing of:
// desktop displays, Active window and selected area copy to clipboard. The script log the cardboard into a document with the name clipboard. 

/*
<javascriptresource>
<about>$$$/JavaScripts/AppendClipboard/About=JJMack's Append Clipboard into Clipboard document.^r^rCopyright 2014 Mouseprints.^r^rJJMack's Script.^r
NOTE:New Clipboard Document will be open if necessary</about>
<category>JJMack's Script</category>
</javascriptresource>
*/

// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events
app.bringToFront();

if (!documents.length) {
	NewCliboardDoc (); 
	app.activeDocument.suspendHistory('appendClipboard','main(true)'); // New document opened proceed
	}
else { 	//locate Document clipboard
	for (var i=0;i<app.documents.length;i++) { 
		app.activeDocument=app.documents[i];
		if (app.activeDocument.name == "Clipboard"){break;}
		}
	if (app.activeDocument.name != "Clipboard"){
		NewCliboardDoc ();
		app.activeDocument.suspendHistory('appendClipboard','main(true)'); // New document opened proceed
		}
	else {app.activeDocument.suspendHistory('appendClipboard','main(false)'); }	// Old Clipboard Document found
	}

///////////////////////////////////////////////////////////////////////////////
//                            main function                                  //
///////////////////////////////////////////////////////////////////////////////
function main(newFile) {
	// declare local variables
	var orig_ruler_units = app.preferences.rulerUnits;
	app.preferences.rulerUnits = Units.PIXELS;			// Set the ruler units to PIXELS

	try { 
		activeDocument.paste(); 					    // past in clipboard may fail enclose in try
		var LB = app.activeDocument.activeLayer.bounds;	// Get the bounds of the work layer
		var LWidth = (LB[2].value) - (LB[0].value);		// Area width
		var LHeight = (LB[3].value) - (LB[1].value);	// Area height
		// BOTTOMCENTER BOTTOMLEFT BOTTOMRIGHT MIDDLECENTER MIDDLELEFT MIDDLERIGHT TOPCENTER TOPLEFT TOPRIGHT
		if(newFile) {
			app.activeDocument.resizeCanvas(LWidth, LHeight, AnchorPosition.TOPLEFT);
			app.activeDocument.layers[1].remove();
			}
		else {
			if (LWidth>=app.activeDocument.width) {	app.activeDocument.resizeCanvas(LWidth, app.activeDocument.height + LHeight, AnchorPosition.TOPLEFT);}
			else {app.activeDocument.resizeCanvas(app.activeDocument.width, app.activeDocument.height + LHeight, AnchorPosition.TOPLEFT);}
			}
		app.activeDocument.selection.selectAll();		// select all
		// Left('AdLf'); Right('AdRg'); Top('AdTp'); Bottom('AdBt'); Center Horizontal('AdCH'); Center Vertical('AdCV');
		align("AdLf");align("AdBt");					//Align to selection Left bottom
		app.activeDocument.selection.deselect();
		SetViewFitonScreen();
		//app.purge(PurgeTarget.CLIPBOARDCACHE);
		}
	catch(e) { 
		if(newFile) app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
		alert("Clipboard Empty"); 
		}		

	app.preferences.rulerUnits = orig_ruler_units;			// Reset units to original settings
	}
///////////////////////////////////////////////////////////////////////////////
//                           main function end                               //
///////////////////////////////////////////////////////////////////////////////
function NewCliboardDoc () {
	// app.documents.add([width][, height][, resolution][, name][, mode][, initialFill][, pixelAspectRatio][, bitsPerChannel][, colorProfileName])
	var Clipboard = app.documents.add( null, null, 100, "Clipboard", NewDocumentMode.RGB, null, null, null, "sRGB IEC61966-2.1");
	}

function align(method) {
	var desc = new ActionDescriptor();
			var ref = new ActionReference();
			ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
		desc.putReference( charIDToTypeID( "null" ), ref );
		desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );
	executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );
	};

function SetViewFitonScreen() {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(charIDToTypeID('Mn  '), charIDToTypeID('MnIt'), charIDToTypeID('FtOn'));
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
};

JJMack

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
Reply
Loading...
Community Beginner ,
Aug 19, 2020

Copy link to clipboard

Copied

Thank you for the response, this solution looks very elegant. You are a wizard and a legend. I ended up using app.activeDocument but some of the methods you demonstrated above look like they will be very useful still in other ways.

 

 

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
Reply
Loading...