I'm looking for a script that can help me to place a "placefile" into an opened document in photoshop - the files are in a same location.
The point is the "placefile" sometime has different name but it always has "_01" at the end and it's a JPG file - sometimes it can be *.PSD. No matter the dimension it is.
For example: I have a folder(s) with images, one of them (here is "*_24.jpg") will be used to place to the others after I created action to add some groups-mask
Here is some screenshot Files Placed to document
I've tried this but it seems something wrong.
#target photoshop
var pathOrig = app.activeDocument.path;
var refName = "_01.JPG"
function place() {
var fileRef = File(pathOrig+'/'+refName);
app.open(fileRef);
}
Your question is unclear. But I'm guessing you want a way to script placing an image into the current document. I don't know what file you want, so I've added a file dialog box which will filter out everything except anything with _01.jpg or _01.psd on the end.
Your code didn't work because a) you're not calling the place function, b) you are not passing any parameters to the function and c) the file path is incomplete.
I've updated the code so it'll place one file ending in _01.jpg in the same folder as the document being worked on. It'll slect the next one in the list as it goes and write out a temp file to the folder. The source PSD document needs to be saved in order for the script to work.
#target photoshop
// Switch off any dialog boxes
displayDialogs = DialogModes.ERROR; // OFF
var pathOrig = app.activeDocument.path;
// var refName = "_01.JPG";
// use the ref neme here in a regular expression
var fileList = pathOrig.getFiles(/.+\_01.jpg/gim);
var countFile = pathOrig + "\\temp.txt";
var readFile = read_it(countFile);
if (k == readFile)
{
var k = 0;
write_it(k, countFile);
}
else
{
var k = parseInt(readFile);
// add one to the count
k+=1;
}
if (fileList != null)
{
// main loop starts here
// for(var i = 0; i < fileList.length; i++)
// {
// var myFile = fileList[i];
// place_it(myFile);
// }
var myFile = fileList[k];
if (k <fileList.length)
{
place_it(myFile);
write_it(k, countFile);
}
}
function place_it(afilename)
{
// Place (embedded) file
// =======================================================
var idPlc = charIDToTypeID( "Plc " );
var desc81 = new ActionDescriptor();
var idIdnt = charIDToTypeID( "Idnt" );
desc81.putInteger( idIdnt, 4 );
var idnull = charIDToTypeID( "null" );
desc81.putPath( idnull, new File( afilename ) );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc81.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc82 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc82.putUnitDouble( idHrzn, idPxl, 0.000000 ); //X offset
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc82.putUnitDouble( idVrtc, idPxl, 0.000000 ); // Y offset
var idOfst = charIDToTypeID( "Ofst" );
desc81.putObject( idOfst, idOfst, desc82 );
executeAction( idPlc, desc81, DialogModes.NO );
}
// function WRITE IT
// --------------------------------------------------------
function write_it(astring, afilepath)
{
// create a reference to a file for output
var txtFile = new File(afilepath);
var writeType = "w"; // w for (over) write
txtFile.open(writeType);
txtFile.seek(0,2);
txtFile.writeln(astring);
txtFile.close();
}
// function READ IT (filename with path) :returns string
// ----------------------------------------------------------------
function read_it(afilepath)
{
var theFile = new File(afilepath);
if (!theFile.exists) return undefined;
var textFile = new File(theFile);
textFile.open('r');
while(!textFile.eof)
{
var line = textFile.readln();
}
textFile.close();
return line;
}