Skip to main content
Participant
February 24, 2010
Question

Using .js scripts to launch Photoshop

  • February 24, 2010
  • 1 reply
  • 6747 views

Hi I'm kind of new to this Photoshops scripts field

and would like to  write an external application (.NET based) that would be able to invoke  the Photoshop application and run Photoshop .js scripts on it and then return the reply to some other external application.

I would like to now if and how it is possible to executes scripts  that  way? It is obvious that I could launch the Photoshop application by  calling the .exe file. But how do I run the script and retrieve data  such as status of the script, errors, etc.

Any  idea's???

Thanks

This topic has been closed for replies.

1 reply

Paul Riggott
Inspiring
February 24, 2010

If you are going to run external JavaScript from .NET you will not be able to get the details unless the script writes the details to a file and you read that. The alternative is to use .NET to talk to Photoshop direct, then you can get the results/errors and pass on the data to your other apps.

Participant
February 25, 2010

OK

But how can I do this? (use the .NET to invoke and operate Photoshop functions)

Do I need to use the SDK or are there other Photoshop API's that I can use?

Yair

Paul Riggott
Inspiring
February 25, 2010

Here is an example of getting some details of an open document and putting the results in an Excel worksheet.

It also shows how to run JavaScript from within C# and how to show messages.

It's very basic but should get you started.

To get started you can dowload the FREE version of Microsoft® Visual C# :-

http://www.microsoft.com/express/Windows/

Create a new Console Application and paste in the following code..

using System;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using ps = Photoshop;

namespace PSexample
{
    public class PSdata
    {
        static void Main(string[] args)
        {
            ps.ApplicationClass app = new ps.ApplicationClass();
            app.Preferences.RulerUnits = ps.PsUnits.psPixels;
            ps.Documents doc_arr = app.Documents;
            if (app.Documents.Count == 0) return;
            double Width = app.ActiveDocument.Width;
            double Height = app.ActiveDocument.Height;
            double PixelCount = Width * Height;
            app.DoJavaScript("alert('Message from within Photoshop\\nTotal Pixels " + PixelCount + " ');", null, null);
            int mess = app.ActiveDocument.Layers.Count;
            MessageBox.Show("Document Name = "+app.ActiveDocument.Name, "PS Example");
            MessageBox.Show("This document has "+Convert.ToString(mess)+ " layers","PS Example");
            Excel.Application app2 = new Excel.ApplicationClass();
            app2.Visible = true; //change to false when tested
            var excelWorkbook = app2.Workbooks.Add(VarEnum.VT_NULL);
            app2.ActiveCell.set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault, " ");     
                app2.Cells[1, 1] = app.ActiveDocument.Path;
                app2.Cells[1, 2] = app.ActiveDocument.Name;
                app2.Cells[1, 3] = Width;
                app2.Cells[1, 4] = Height;
                app2.Cells[1, 5] = app.ActiveDocument.Layers.Count;
        }
    }
}

You now need to add a couple of COM references one for Photoshop and one for Excel.

On the menu bar select Project - Add Reference -Select The COM tab

Now select the COM object..

Adobe Photoshop CS# Object Libary          //CS# depends on what version of Photoshop you want to use (This is the API)

Microsoft Excel 12.0 Object Library          // The Number may be different depending on your version of Excel

Now select .NET tab and select

System.Windows.Forms

With a document open in the CORRECT version of Photoshop you can run the script from C# by pressing the F5 function key.

Hope this helps.