0

I have written a photoshop script to generate product image for a newsletter on runtime and a vbs script to call it which all work fine when I execute the vbs script throught the command line, but if I call it from a asp.net application it doesn't, then a get the error: There is no script engine for file extension ".jsx".

The jsx script, the vbs script and the code I use to execute can be found below. Can anyone help me to make it work through the asp.net application please.

UPDATE I managed to fix the script engine error, that was just a problem with the c# process argument. I've fixed the code below. It would seem, the problem is the network service user, doesn't have permission to startup photoshop. I have tried all kind of stuff to give it the right permission, but it's still not working.

How do I allow the network service user to access photoshop?

JSX script:

#target photoshop

function getLayer(target, layerName)
{
    try
    {
        var layer = doc.layers.getByName(layerName);
        return layer;
    }
    catch(e)
    {
       alert("Unable to find layer: " + layerName); // only for debug
    }

    return false;

}

function changeTextOnTextLayer(target, layerName, textToInsert)
{
    var textLayer = getLayer(target, layerName);
    if(textLayer && textLayer.kind == LayerKind.TEXT)
    {
            textLayer.textItem.contents = textToInsert;
    }

    textLayer = null;
}

function setImageOnLayerFromFile(target, layerName, fullFileName)
{
    var imageLayer = getLayer(target, layerName);        

    if(imageLayer)
    {

            var x = imageLayer.bounds[0];
            var y = imageLayer.bounds[1];

            var layerSize = getWidthAndHeight(imageLayer);
            var width = layerSize[0];
            var height = layerSize[1];

            var file = File(fullFileName);

            app.load(file);
            var imageFile = app.activeDocument;
            imageFile.resizeImage(  width, height);
            imageFile.selection.selectAll();
            imageFile.selection.copy();
            imageFile.close(SaveOptions.DONOTSAVECHANGES);
            file.close();
            file = null;

            target.paste();
            var newImageLayer = target.layers[0];
            moveLayerTo(newImageLayer, x, y);
    }

    textLayer = null;
}

function getWidthAndHeight(layer)
{
    var width =  layer.bounds[2] - layer.bounds[0];
    var height =  layer.bounds[3] - layer.bounds[1];

    return [width, height];
}

function moveLayerTo(fLayer,fX,fY) 
{

    var Position = fLayer.bounds;
    Position[0] = fX - Position[0];
    Position[1] = fY - Position[1];

    fLayer.translate(Position[0],Position[1]);
}

function saveToPNG(doc, fileName)
{
    var file = new File(fileName);
    opts = new ExportOptionsSaveForWeb();
    with (opts) 
    {
        format = SaveDocumentType.PNG;
        PNG8 = false;
    }

    doc.exportDocument(file, ExportType.SAVEFORWEB, opts);
    // save for web
}

if(!arguments || arguments.length != 5 )
{
     alert("Missing arguments!"); // only for debug
}
else
{
    var file = File(arguments[0]);
    app.load(file);
    var doc = app.activeDocument;

    setImageOnLayerFromFile(doc, 'Image', arguments[1]);

    changeTextOnTextLayer(doc, 'ProductDescription', arguments[3]);
    changeTextOnTextLayer(doc, 'Price', arguments[4]);

    saveToPNG(doc, arguments[2]);

    doc.close(SaveOptions.DONOTSAVECHANGES);
    doc = null;
    file.close();
    file = null;
}

VBS script:

Set vbsArguments = WScript.Arguments
If vbsArguments.Length = 0 Then
   WScript.Echo "Argument(0) is `your script to execute"
   WScript.Echo "Arguments(0+n) are passed to your script as argument(0) to argument(n-1)"
Else
   ReDim jsxArguments(vbsArguments.length-2)

for i = 1 to vbsArguments.length - 1
  jsxArguments(i-1) = vbsArguments(i)
Next
    Set photoshop = CreateObject( "Photoshop.Application" )
    photoshop.BringToFront
Call photoshop.DoJavaScriptFile( vbsArguments(0), jsxArguments, 1)
End IF 

C# asp.net

const string vbsScript = "cscript";
string arguments = "\"" + @"D:\script\createproduct.vbs" + "\" " + string.Format(@"D:\script\createproduct.jsx " +
                "/d/script/test.psd \"{0}\" \"{1}\" \"{2}\" \"{3}\"",
                imagePath,
                fullSavePath,
                requestSpecificProduct.ShortDescription,
                requestSpecificProduct.DisplayPrice);

            context.Response.Write(vbsScript + "\n");
            context.Response.Write(arguments);

            using (var scriptProc = new Process
                {
                    StartInfo =
                        {
                            FileName = vbsScript,
                            Arguments = arguments,
                            WorkingDirectory = @"D:\",
                            WindowStyle = ProcessWindowStyle.Hidden,
                            UseShellExecute = false,
                            RedirectStandardOutput = true,
                            RedirectStandardError = true,
                        }
                })
            {

                scriptProc.Start();
                scriptProc.WaitForExit();

Thank you for your help

Dinirex
  • 105
  • 1
  • 10
  • I suggest that using Photoshop to add text to an image is severe overkill. You can do it all in your program; to get you started you could look at [Write Text On An Image in c#](http://stackoverflow.com/questions/6826921/write-text-on-an-image-in-c-sharp). – Andrew Morton Mar 22 '14 at 21:26
  • @Andrew Morton The reason I Use photoshop, is because our customer needs the ability to change the model. So they can decide what font, font size, and image size and position. I never know where the text is to be positioned or how big the image must be. All this can photoshop calculate and insert correctly. – Dinirex Mar 23 '14 at 08:04

0 Answers0