Hi, I am new to scripting completely. I am working on creating mockups for client, where multiple .png files are to be placed on multiple .psd files on a targeted layer. I was able to find a script from GitHub which, with some modifications is working fine.
But the problem is that its naming the output files by just numbers. like file1. file2. file3... do on. I need the out put file to be named based on the name of the input .png file. Elsewise, I have to copy paste each file into respected folders and then re name them. Can anyone please help me with the code to ger the output file name based on the input file name? Help would be much appreciated. Thank you.
var includePath = '';
if ( $.includePath ) includePath = File.decode($.includePath);
var docPath = '';
function mockups( mockups ) {
var displayDLG = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var playbackDisplayDLG = app.playbackDisplayDialogs;
app.playbackDisplayDialogs = DialogModes.NO; // Affects Actions, which aren't used as of now, but maybe that changes in the future...
soReplaceBatch( mockups );
app.displayDialogs = displayDLG;
app.playbackDisplayDialogs = playbackDisplayDLG;
}
function soReplaceBatch( mockups ) {
for ( var i=0; i < mockups.length; i++ ) {
var mockup = mockups[i];
var mockupPSD = absolutelyRelativePath( mockup.mockupPath );
if ( mockupPSD.file ) {
app.open( mockupPSD.file );
if ( mockup.showLayers ) {
each( mockup.showLayers, function( layerName ) {
var layer = getLayer( layerName );
layer.visible = true;
});
}
if ( mockup.hideLayers ) {
each( mockup.hideLayers, function( layerName ) {
var layer = getLayer( layerName );
layer.visible = false;
});
}
soReplace({
output: mockup.output,
items: mockup.smartObjects,
noRepeats: mockup.noRepeats,
});
app.activeDocument.close( SaveOptions.DONOTSAVECHANGES );
}
}
alert('Batch process done!');
}
function soReplace( rawData ) {
app.activeDocument.suspendHistory("soReplace", "init(rawData)");
function init( rawData ) {
var rulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var data = replaceLoopOptionsFiller( rawData );
// Preparing files
for ( var i=0; i < data.items.length; i++ ) {
var item = data.items[i];
item.files = prepFiles( item );
}
// This makes sure all file arrays are the same length
data = evenOutFileArrays( data );
replaceLoop( data );
app.preferences.rulerUnits = rulerUnits;
}
}
function replaceLoop( data ) {
for ( var fileIndex=0; fileIndex < data.maxLoop; fileIndex++ ) {
for ( var itemIndex=0; itemIndex < data.items.length; itemIndex++ ) {
var item = data.items[ itemIndex ];
if ( item.target ) {
var targetConfirmed = false;
try {
app.activeDocument.activeLayer = item.target;
targetConfirmed = true;
} catch(e) {}
if ( targetConfirmed ) {
if ( fileIndex == 0 ) convertSoToEmbedded(); // Just in case the target layer is a linked SO...
var sourceFilePath = item.files[ fileIndex ];
if ( sourceFilePath !== null ) {
app.activeDocument.activeLayer.visible = true;
replaceSoContents( item, sourceFilePath ); // The old switcheroo
// if ( item.targetVisibility === false || item.targetVisibility === true ) {
// app.activeDocument.activeLayer.visible = item.targetVisibility;
// }
}
else {
app.activeDocument.activeLayer.visible = false;
}
}
}
}
var outputPathPrefix = data.output.path + "/";
var outputFileName = outputPathPrefix + parseFilename( data, fileIndex, outputPathPrefix );
app.activeDocument.saveAs( new File( outputFileName ), saveOpts()[ data.output.format ](), true, Extension.LOWERCASE);
if ( sourceFilePath === null ) app.activeDocument.activeLayer.visible = item.targetVisibility;
}
}
function parseFilename( data, fileIndex, outputPathPrefix ) {
var fileNumber = fileIndex+1;
if ( data.output.zeroPadding ) fileNumber = zeroPadding( fileNumber, data.maxLoop.toString().length );
var inputFile = data.largestArray[fileIndex];
var inputFilename = inputFile ? inputFile.name.replace(/\.[^\.]+$/, '') : fileNumber;
// var hasInput = data.output.filename.match(/@input/);
var filename = data.output.filename.replace('@mockup', data.doc.name).replace('$', fileNumber).replace('@input', inputFilename);
var outputFilename = filename + "." + data.output.format;
// if ( hasInput ) {
// var testOutputFile = new File( outputPathPrefix + outputFilename );
// if ( testOutputFile.exists ) {
// outputFilename = filename + " (" + fileNumber + ")." + data.output.format;
// }
// }
return outputFilename;
}
function prepFiles( item ) {
if ( typeof item.input === 'string' ) item.input = [ item.input ];
var inputFiles = [];
for ( var i=0; i < item.input.length; i++ ) {
var inputFolder = new Folder( item.input[i] );
var files = getFiles( inputFolder, item );
if ( files ) inputFiles = inputFiles.concat( files );
};
return inputFiles.sort(function (a, b) {
if ( app.compareWithNumbers ) {
return app.compareWithNumbers(a.name, b.name)
}
else {
return sortAlphaNum(a.name, b.name);
}
});
}