Friday, April 1, 2011

Useful Functions

These days I got chance working with phothoshop automation. Working with photoshop is really tough task for me because I am a simple .net guy. Thank god photoshop expert is here to help me.
I am also thankful to John Deurbrouck who has posted one article which helped me a lot. Using his idea I was able to write some of the functions, which is I am posting here.


using ps = Photoshop;

static ps.Application app1 = null;


Undo:
public static string undo()
{
try
{
if (app1.ActiveDocument.HistoryStates.Index(app1.ActiveDocument.ActiveHistoryState) - 1 > 1)
{
app1.ActiveDocument.ActiveHistoryState = app1.ActiveDocument.HistoryStates[app1.ActiveDocument.HistoryStates.Index(app1.ActiveDocument.ActiveHistoryState) - 1];
}
}
catch (Exception ex)
{
}
return "";
}



Open PSD:
public static string OpenTemplate(string psdFile)
{
ps.ApplicationClass app = new ps.ApplicationClass();
app.Open(psdFile, null,false);
return "";
}

Image Contrast:
public static string contrast(int contrastValue)//
{
try
{
if (app1 == null)
{
app1 = new ps.Application();
}

undo();

var id136 = app1.CharIDToTypeID("BrgC");
Photoshop.ActionDescriptor desc35 = new Photoshop.ActionDescriptor();
var id137 = app1.CharIDToTypeID("Cntr");
desc35.PutInteger(id137, Convert.ToInt32(contrastValue));
var id138 = app1.StringIDToTypeID("useLegacy");
desc35.PutBoolean(id138, false);
app1.ExecuteAction(id136, desc35, ps.PsDialogModes.psDisplayNoDialogs);
app1.ActiveDocument.Selection.Deselect();

desc35 = null;
}
catch (Exception ex)
{
}
return "";
}

Brightness:
public static string brightness(int brightnessValue)
{
try
{
if (app1 == null)
{
app1 = new ps.Application();
}

undo();

var id136 = app1.CharIDToTypeID("BrgC");
Photoshop.ActionDescriptor desc35 = new Photoshop.ActionDescriptor();
var id137 = app1.CharIDToTypeID("Brgh");
desc35.PutInteger(id137, brightnessValue);
var id138 = app1.StringIDToTypeID("useLegacy");
desc35.PutBoolean(id138, false);
app1.ExecuteAction(id136, desc35, ps.PsDialogModes.psDisplayNoDialogs);
desc35 = null;
//app1.Purge(Photoshop.PsPurgeTarget.psHistoryCaches);
}
catch (Exception ex)
{
}
return "";
}


Saturation:
public static string saturation(int saturationValue)
{
if (app1 == null)
{
app1 = new ps.Application();
}

undo();

var id3 = app1.CharIDToTypeID("HStr");
var desc2 = new Photoshop.ActionDescriptor();
var id4 = app1.CharIDToTypeID("Clrz");
desc2.PutBoolean(id4, false);
var id5 = app1.CharIDToTypeID("Adjs");
var list1 = new Photoshop.ActionList();
var desc3 = new Photoshop.ActionDescriptor();
var id6 = app1.CharIDToTypeID("H ");
desc3.PutInteger(id6, 0);
var id7 = app1.CharIDToTypeID("Strt");
desc3.PutInteger(id7, saturationValue);
var id8 = app1.CharIDToTypeID("Lght");
desc3.PutInteger(id8, 0);
var id9 = app1.CharIDToTypeID("Hst2");
list1.PutObject(id9, desc3);
desc2.PutList(id5, list1);
app1.ExecuteAction(id3, desc2, Photoshop.PsDialogModes.psDisplayNoDialogs);
desc2 = null;
list1 = null;
desc3 = null;
app1.ActiveDocument.Selection.Deselect();

return "";
}


Cyan:
public static string cyan(int cyanValue)
{
if (app1 == null)
{
app1 = new ps.Application();
}

undo();

var id13 = app1.CharIDToTypeID("SlcC");
var desc5 = new Photoshop.ActionDescriptor();
var id14 = app1.CharIDToTypeID("Mthd");
var id15 = app1.CharIDToTypeID("CrcM");
var id16 = app1.CharIDToTypeID("Rltv");
desc5.PutEnumerated(id14, id15, id16);
var id17 = app1.CharIDToTypeID("ClrC");
var list2 = new Photoshop.ActionList();
var desc6 = new Photoshop.ActionDescriptor();
var id18 = app1.CharIDToTypeID("Clrs");
var id19 = app1.CharIDToTypeID("Clrs");
var id20 = app1.CharIDToTypeID("Rds ");
desc6.PutEnumerated(id18, id19, id20);
var id21 = app1.CharIDToTypeID("Cyn ");
var id22 = app1.CharIDToTypeID("#Prc");
desc6.PutUnitDouble(id21, id22, cyanValue);
var id23 = app1.CharIDToTypeID("ClrC");
list2.PutObject(id23, desc6);
desc5.PutList(id17, list2);
app1.ExecuteAction(id13, desc5, Photoshop.PsDialogModes.psDisplayNoDialogs);
desc5 = null;
list2 = null;
desc6 = null;
app1.ActiveDocument.Selection.Deselect();

return "";
}


Magenta:
public static string magenta(int magentaValue)
{
if (app1 == null)
{
app1 = new ps.Application();
}

undo();

var id13 = app1.CharIDToTypeID("SlcC");
var desc5 = new Photoshop.ActionDescriptor();
var id14 = app1.CharIDToTypeID("Mthd");
var id15 = app1.CharIDToTypeID("CrcM");
var id16 = app1.CharIDToTypeID("Rltv");
desc5.PutEnumerated(id14, id15, id16);
var id17 = app1.CharIDToTypeID("ClrC");
var list2 = new Photoshop.ActionList();
var desc6 = new Photoshop.ActionDescriptor();
var id18 = app1.CharIDToTypeID("Clrs");
var id19 = app1.CharIDToTypeID("Clrs");
var id20 = app1.CharIDToTypeID("Grns");
desc6.PutEnumerated(id18, id19, id20);
var id21 = app1.CharIDToTypeID("Mgnt");
var id22 = app1.CharIDToTypeID("#Prc");
desc6.PutUnitDouble(id21, id22, magentaValue);
var id23 = app1.CharIDToTypeID("ClrC");
list2.PutObject(id23, desc6);
desc5.PutList(id17, list2);
app1.ExecuteAction(id13, desc5, Photoshop.PsDialogModes.psDisplayNoDialogs);
desc5 = null;
list2 = null;
desc6 = null;
app1.ActiveDocument.Selection.Deselect();

return "";
}


Yellow:
public static string yellow(int yellowValue)
{
if (app1 == null)
{
app1 = new ps.Application();
}

undo();

var id13 = app1.CharIDToTypeID("SlcC");
var desc5 = new Photoshop.ActionDescriptor();
var id14 = app1.CharIDToTypeID("Mthd");
var id15 = app1.CharIDToTypeID("CrcM");
var id16 = app1.CharIDToTypeID("Rltv");
desc5.PutEnumerated(id14, id15, id16);
var id17 = app1.CharIDToTypeID("ClrC");
var list2 = new Photoshop.ActionList();
var desc6 = new Photoshop.ActionDescriptor();
var id18 = app1.CharIDToTypeID("Clrs");
var id19 = app1.CharIDToTypeID("Clrs");
var id20 = app1.CharIDToTypeID("Bls ");
desc6.PutEnumerated(id18, id19, id20);
var id21 = app1.CharIDToTypeID("Ylw ");
var id22 = app1.CharIDToTypeID("#Prc");
desc6.PutUnitDouble(id21, id22, yellowValue);
var id23 = app1.CharIDToTypeID("ClrC");
list2.PutObject(id23, desc6);
desc5.PutList(id17, list2);
app1.ExecuteAction(id13, desc5, Photoshop.PsDialogModes.psDisplayNoDialogs);
desc5 = null;
list2 = null;
desc6 = null;
app1.ActiveDocument.Selection.Deselect();

return "";
}


Gamma:
public static string gamma(double gammaValue)
{
if (app1 == null)
{
app1 = new ps.Application();
}

undo();

var id245 = app1.CharIDToTypeID("Exps");
var desc52 = new Photoshop.ActionDescriptor();
var id246 = app1.CharIDToTypeID("Exps");
desc52.PutDouble(id246, 0.000000);
var id247 = app1.CharIDToTypeID("Ofst");
desc52.PutDouble(id247, 0.000000);
var id248 = app1.StringIDToTypeID("gammaCorrection");
desc52.PutDouble(id248, gammaValue);
app1.ExecuteAction(id245, desc52, Photoshop.PsDialogModes.psDisplayNoDialogs);
app1.ActiveDocument.Selection.Deselect();

return "";
}


Purge History:
public static string purgeHistory()
{
string file = "";

string str = "#target photoshop; " + Environment.NewLine +
" app.bringToFront(); " + Environment.NewLine +
" // ======================================================= " + Environment.NewLine +
" var id796 = charIDToTypeID( \"Prge\" ); " + Environment.NewLine +
" var desc145 = new ActionDescriptor(); " + Environment.NewLine +
" var id797 = charIDToTypeID( \"null\" ); " + Environment.NewLine +
" var id798 = charIDToTypeID( \"PrgI\" ); " + Environment.NewLine +
" var id799 = charIDToTypeID( \"Hsty\" ); " + Environment.NewLine +
" desc145.putEnumerated( id797, id798, id799 ); " + Environment.NewLine +
" executeAction( id796, desc145, DialogModes.NO ); " + Environment.NewLine;

file = createFile(str);
System.Diagnostics.Process p;
p=System.Diagnostics.Process.Start(file);
return file;
}


public static string createFile(string toWrite)
{
using (StreamWriter outfile = new StreamWriter(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Adobe Scripts\\tmp.jsx"))
{
toWrite += Environment.NewLine + " var f = new File(decodeURI($.fileName)); " + Environment.NewLine +
" f.remove();";

outfile.Write(toWrite.ToString());
if (app1 == null)
{
app1 = new Photoshop.Application();
}
app1.Purge(Photoshop.PsPurgeTarget.psHistoryCaches);
}
//File should be saved in My Documents\Adobe Scripts\
//If we save .jsx file in above folder it wont display confirmation dialog before execution.
//Photoshop provides special rights to this folder
return System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Adobe Scripts\\tmp.jsx";
}

4 comments:

  1. Nice Article for Beginers ,

    Thanks Brother ....

    ReplyDelete
  2. Hi,
    Can you tell me how to load image , apply filter (like shadow & highlight (value 50/30)) & save image by
    Photoshop automation in c#...

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Photoshop Automation

    We provide Custom Photoshop Script and Automation tool to help you in Photoshop Design. Photoshop Automation gets your repetitive task done on click of a Button

    to get more - https://tejwani.com/photoshop-automation-scripts-panel/

    ReplyDelete