我希望Photoshop自动执行给定文件夹的以下任务:
其中一些步骤可以通过使用简单的操作来完成,但由于我需要为每个图像应用不同的水平偏移值(几百个图像),所以事情变得有点棘手(我有一个具有不同偏移值的excel表格)。我被告知这可以通过Photoshop脚本完成,但不幸的是我不知道从哪里开始,因为我对JavaScript和脚本编写的经验不多。
那么是否可以生成执行上述步骤的脚本?
答案 0 :(得分:0)
欢迎使用Stack Overflow。通常我们会要求人们提供一些代码以便得到答案,但是因为你开始这很困难。
我要把这个放在盘子里给你。这不是Stack Overflow的工作方式,很多人会说SO不是脚本编写服务。在这种情况下,我认为您可以将问题的这四个阶段变成代码,从中受益。如果您能理解这一点,您可以添加更多自己的代码,依此类推。
这是您需要的代码。将其添加到文本文件中,并为其指定扩展名.jsx。从Photoshop运行脚本。
// Offset image as smart object
//
// how much do we want to offset the image?
// Set up the variables here
var xOffest = 0; // set this to what you want
var yOffset = 0; // set this to what you want also
// jpeg quality
var myJpgQuality = 8;
// get all the files to process
var inFolder = Folder.selectDialog("Please select folder to process");
if (inFolder != null)
{
// create array of all files we need
var fileList = inFolder.getFiles();
}
// loop over each file in turn using a loop
for (var i = 0; i < fileList.length; i++)
{
// all the main code action happens here
// create a variable called doc
// to hold each file in turn
openThisFile(fileList[i]);
// call the source document
// it's easier than having to type
// app.activeDocument all the time
var srcDoc = app.activeDocument;
// convert to smart object
convert_to_smartobject(srcDoc.activeLayer);
// get the name of the file with extension
var fName = srcDoc.name;
// make a variable called docname and remove the extension
var docName = fName.substring(0, fName.lastIndexOf("."));
// grab the filepath and name of image anad add jpg extension
var myFilePath = srcDoc.path + "/" + docName + ".jpg";
// select all
srcDoc.selection.selectAll();
//offset each layer
translateLayer(xOffest,yOffset);
// all done?
// Save out file as jpeg
// jpg file options
var jpgFile = new File(myFilePath);
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = myJpgQuality;
activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
// save as psd as well
var psdFile = new File(myFilePath);
psdSaveOptions = new PhotoshopSaveOptions();
psdSaveOptions.embedColorProfile = true;
psdSaveOptions.alphaChannels = true;
activeDocument.saveAs(psdFile, psdSaveOptions, false, Extension.LOWERCASE);
//close without saving
srcDoc.close(SaveOptions.DONOTSAVECHANGES);
} // end of loop
function convert_to_smartobject(alayer)
{
// Thanks to PHennessey for this snippet
var idnewPlacedLayer = stringIDToTypeID( 'newPlacedLayer' );
executeAction(idnewPlacedLayer, undefined, DialogModes.NO);
}
// function to offset image
function translateLayer(dx,dy)
{
// This is ugly scriptlistner code
// it's translate a layer
// don't try to understand it
// =======================================================
var id114 = charIDToTypeID( "move" );
var desc26 = new ActionDescriptor();
var id115 = charIDToTypeID( "null" );
var ref14 = new ActionReference();
var id116 = charIDToTypeID( "Lyr " );
var id117 = charIDToTypeID( "Ordn" );
var id118 = charIDToTypeID( "Trgt" );
ref14.putEnumerated( id116, id117, id118 );
desc26.putReference( id115, ref14 );
var id119 = charIDToTypeID( "T " );
var desc27 = new ActionDescriptor();
var id120 = charIDToTypeID( "Hrzn" );
var id121 = charIDToTypeID( "#Pxl" );
desc27.putUnitDouble( id120, id121, dx ); //horizontal offset
var id122 = charIDToTypeID( "Vrtc" );
var id123 = charIDToTypeID( "#Pxl" );
desc27.putUnitDouble( id122, id123, dy ); // vertical offset (negative is UP!)
var id124 = charIDToTypeID( "Ofst" );
desc26.putObject( id119, id124, desc27 );
executeAction( id114, desc26, DialogModes.NO );
}
function openThisFile(fileNameAndPath)
{
var fileRef = new File(fileNameAndPath)
if (fileRef.exists)
//open that doc
{
app.open(fileRef);
}
else
{
alert("error opening " + fileNameAndPath)
}
}
现在,它将浏览目录中的所有文件。如果那里有非图像文件,脚本将会失败。先拿出来。哦,我还没有对它进行过测试 - 所以只要脚本失效就大喊大叫。
有很多资源可以学习JavaScript。 CodeAcademy可能对您有益,但选择一个适合您的需求。一旦你了解了基础知识(变量,数组,字符串等),你就可以将它应用于Photoshop的知识。然后,您需要了解基本的Photoshop内容,例如在Photoshop文件中循环图层和图像操作。
我确定我的第一个问题是关于加载.CSV文件并将数据应用于图像。所以我相信你能够为自己做到这一点。自己敲门!