Skip to main content
Participating Frequently
October 5, 2009
Question

Trying to applescript smart objects instead of double click & edit psb file

  • October 5, 2009
  • 2 replies
  • 3482 views

Hi,

I am trying to place an image inside of a smart object by applescripting instead of double clicking the smart object & placing the image inside of the psb file & saving.

I found the following information on how to do this via javascript (interesting because it seems undocumented in Adobe literature)

My question before quoting it below is:

Does anyone know how to applescript the equivalent of the javascript below, or is their a way to call the javascript from applescript?

Any pointers in the right direction is greatly appreciated. This would save me a ton of time over the year.

Here is the method mentioned before, quoted as I found it (Thanks for taking a look*):

" The method to open the contents of a SmartObject for editing is an undocumented action, however you can discover it by using the ScriptListener plugin. You have set a couple of parameter values and then use the executeAction command. After your "if" statement, you'll want to make the SmartObject layer the active layer...

docRef.activeLayer = docRef.artLayers;
then insert the following lines to open the layer's contents...
var idAction = stringIDToTypeID( "placedLayerEditContents" );     
var idDesc = new ActionDescriptor();
executeAction(idAction, idDesc, DialogModes.NO);
Your script will open the SmartObject just as if you double-clicked its icon in the layer stack. The object's contents are opened as a temporary .psb file, and it then becomes the activeDocument for your script. You may take further actions upon it and then close it (saving changes) to return to your original document. "

This topic has been closed for replies.

2 replies

Participating Frequently
October 7, 2009

If anyone knows how & could tell me how to resize the placed image within the smart object to 100%

(for any size placed image whether it is horizontal or vertical orientation) that would put the finish on this script for me.

Right now I am using:

layer.resize(32, 32);

but, it is different for every incoming image, which makes me have to go into the smart object & resize some after they are placed by hand.

Paul Riggott
Inspiring
October 7, 2009

This will resize the SO to its full size.

activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER);
activeDocument.revealAll();

Participating Frequently
October 8, 2009

I like this code, but it seems to scale up the placed layer inside of the smart object UP to it's original file dimensions. Which makes it way too big when viewing it outside of the smart object (viewing the main psd document)

This will resize the SO to its full size.

activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER);
activeDocument.revealAll();

________________________

I am trying to scale the placed layer inside of the smart object up to fit the inside of the smart object's dimensions & orientation without having to worry about which files dropped (they are all different dimensions & orientations)

Thanks though... I didnt know about AnchorPosition.MIDDLECENTER that was useful elsewhere for me.

Participating Frequently
October 5, 2009

Okay, here is the suggested Applescript for calling a do javascript command:

tell application "Adobe Photoshop CS4"

set theFile to alias "Macintosh HD:User:Desktop:Applescripts:Picturesque Emulation:picturesque.psd"

open theFile

set openAndPlace to alias "Macintosh HD:User:Desktop:Applescripts:Picturesque Emulation:openandplace_in_new_smartobject.jsx"

set theOne to "/Users/User/Desktop/photos/DSC_0014.JPG"

do javascript (openAndPlace) ¬

with arguments {theOne}

end tell

It is suggested then to put all the Script Listener javascript into it's own file (openandplace_in_new_smartobject.jsx)

Here is the Script Listener javascript from the above jsx file I made / You evidently are supposed to put your number of arguments passed from the Applescript just below the bottom of the javascript function:

function openandplace_SO( path_to_image )

{

// =======================================================

var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );

    var desc10 = new ActionDescriptor();

executeAction( idplacedLayerEditContents, desc10, DialogModes.NO );

// =======================================================

var idPlc = charIDToTypeID( "Plc " );

    var desc11 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

    desc11.putPath( idnull, new File( path_to_image ) );

    var idFTcs = charIDToTypeID( "FTcs" );

    var idQCSt = charIDToTypeID( "QCSt" );

    var idQcsa = charIDToTypeID( "Qcsa" );

    desc11.putEnumerated( idFTcs, idQCSt, idQcsa );

    var idOfst = charIDToTypeID( "Ofst" );

        var desc12 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc12.putUnitDouble( idHrzn, idPxl, 0.000000 );

        var idVrtc = charIDToTypeID( "Vrtc" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc12.putUnitDouble( idVrtc, idPxl, 0.000000 );

    var idOfst = charIDToTypeID( "Ofst" );

    desc11.putObject( idOfst, idOfst, desc12 );

executeAction( idPlc, desc11, DialogModes.NO );

// =======================================================

var idsave = charIDToTypeID( "save" );

executeAction( idsave, undefined, DialogModes.NO );

// =======================================================

var idupdatePlacedLayer = stringIDToTypeID( "updatePlacedLayer" );

executeAction( idupdatePlacedLayer, undefined, DialogModes.NO );

// =======================================================

var idCls = charIDToTypeID( "Cls " );

executeAction( idCls, undefined, DialogModes.NO );

// =======================================================

// =======================================================

// Call openandplace_SO with values provided in the "arguments" collection

openandplace_SO( arguments[0]);

}

Everything starts up fine & script just stalls with result in Script Debugger of undefined. I am not sure what is undefined, but I am guessing it is

the argument into the javascript function.

If someone with a better eye for spotting what the problem could be..... I am all ears.

Thanks for taking a look & have a really good day*

Muppet_Mark-QAl63s
Inspiring
October 5, 2009

Im only using CS2 and for me there is NO AppleScript alternative way to deal with smart layer objects other than to use script listener output. Here is a bare bones example of what I think you are trying to do here:

tell application "Adobe Photoshop CS2"

activate

set Doc_Ref to the current document

tell Doc_Ref

set current layer to (first layer whose kind = smart object layer)

my Edit_Smart_Layer()

end tell

set Smart_Ref to the current document

set The_File to POSIX path of ¬

(choose file with prompt "Please select a file to place…" without invisibles)

tell Smart_Ref

my Place_File(The_File)

close saving yes

end tell

set the current document to Doc_Ref

end tell

--

on Place_File(The_File)

tell application "Adobe Photoshop CS2"

do javascript "placeFile(); function placeFile() {function cTID(s) { return app.charIDToTypeID(s); }; var desc = new ActionDescriptor(); desc.putPath( cTID('null'), new File( '" & The_File & "' )); desc.putEnumerated( cTID('FTcs'), cTID('QCSt'), cTID('Qcsa') ); var ldesc = new ActionDescriptor(); ldesc.putUnitDouble( cTID('Hrzn'), cTID('#Pxl'), 0.000000 ); ldesc.putUnitDouble( cTID('Vrtc'), cTID('#Pxl'), 0.000000 ); desc.putObject( cTID('Ofst'), cTID('Ofst'), ldesc ); executeAction( cTID('Plc '), desc, DialogModes.NO );}" show debugger on runtime error

end tell

end Place_File

--

on Edit_Smart_Layer()

tell application "Adobe Photoshop CS2"

do javascript "editSmartLayer(); function editSmartLayer() { function cTID(s) { return app.charIDToTypeID(s); }; function sTID(s) { return app.stringIDToTypeID(s); }; var desc01 = new ActionDescriptor(); executeAction( sTID('placedLayerEditContents'), desc01, DialogModes.NO );};" show debugger on runtime error

end tell

end Edit_Smart_Layer

If you are going to be using output from script listener then do yourself a massive favor and get 'X's' toolkit this helps a great deal

Muppet_Mark-QAl63s
Inspiring
October 5, 2009

This should be more or less the same thing as JavaScript (I hope I got this right!)

#target photoshop

app.bringToFront();

var docRef = app.activeDocument;

for (var i = 0; i < docRef.artLayers.length; i++) {

if( docRef.artLayers.kind == LayerKind.SMARTOBJECT ) {

docRef.activeLayer = docRef.artLayers;

}

}

editSmartLayer();

var smartRef = app.activeDocument;

var placeRef = File.openDialog('Please select a file to "Place"…');

placeFile(placeRef);

smartRef.close(SaveOptions.SAVECHANGES);

app.activeDocument = docRef;

// Place file as Smart

function placeFile(placeRef) {

  function cTID(s) {return app.charIDToTypeID(s); };

var desc = new ActionDescriptor();

desc.putPath(cTID('null'), new File(placeRef));

desc.putEnumerated(cTID('FTcs'), cTID('QCSt'), cTID('Qcsa') );

var ldesc = new ActionDescriptor();

ldesc.putUnitDouble(cTID('Hrzn'), cTID('#Pxl'), 0.000000 );

ldesc.putUnitDouble(cTID('Vrtc'), cTID('#Pxl'), 0.000000 );

desc.putObject(cTID('Ofst'), cTID('Ofst'), ldesc);

executeAction(cTID('Plc '), desc, DialogModes.NO);

// Resize the layer

var layer = app.activeDocument.activeLayer;

layer.resize(100, 100);

return layer;

}

// Edit Smart Layer

function editSmartLayer() {

  function cTID(s) { return app.charIDToTypeID(s); };

  function sTID(s) { return app.stringIDToTypeID(s); };

    var desc01 = new ActionDescriptor();

    executeAction( sTID('placedLayerEditContents'), desc01, DialogModes.NO );

};