Formating a script that works for a PC, but needed for a Mac

Upload Photoshop Scripts, download Photoshop Scripts, Discussion and Support of Photoshop Scripts

Moderators: Tom, Kukurykus

fenomnon64

Formating a script that works for a PC, but needed for a Mac

Post by fenomnon64 »

Hey Guys,

I needed to create a script that targets subfolders that contains PSD's and images, Long story short, my friend wrote a script that works on a PC, but doesn't know how to transfer it to a mac. The mac version gets errors, where as the PC version runs OK. This is the script, I am hoping there is a line in here that can be tweaked and thats all, any help would be greatly appreciated!
____________________________________

// Collect from Subfolders - a script for Adobe Photoshop CS2 and higher
// Description: collects all images, by subfolder, into a single document (one for each subfolder)
// Version: 0.2.1, 5/Jan/2008
============================================================================
// Installation:
// 1. Place script in 'C:\Program Files\Adobe\Adobe Photoshop CS3\Presets\Scripts\'
// 2. Restart Photoshop
// 3. Choose File > Scripts > Collect from Subfolders
// ============================================================================

// Known issues:
// No known issues

// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events
app.bringToFront();

// user-customizable variables
var defaultFolder = '~'; // default browse location/folder (default: '~')
//var defaultFolder = '/h/root_folder/'; // default browse location/folder (default: '~')

///////////////////////////////////////////////////////////////////////////////
// collectSubfolders - main function
///////////////////////////////////////////////////////////////////////////////
function collectSubfolders() {

// switch to pixels
preferences.rulerUnits = Units.PIXELS;

// ROOT FOLDER
// get all subfolders within the root folder
var subFolders = rootFolder.getFiles();
var sfLength = subFolders.length;

// ROOT FOLDER CONTENTS
if (sfLength) { // check for files and folders
for (var l = 0; l < sfLength; l++) { // iterate through subfolders

// get contents of subfolders
var subFolderContents = subFolders[l].getFiles();
var sfcLength = subFolderContents.length;

// SUBFOLDER CONTENTS
if (sfcLength) { // check for files and folders
for (var k = 0; k < sfcLength; k++) { // iterate through contents of subfolders

// IMAGES FOLDER
if ((subFolderContents[k] instanceof Folder) && (subFolderContents[k].name == 'images')) { // check for 'images' folder

// get subfolders contained within the 'images' folder
var imagesFolder = Folder(subFolderContents[k].path + '/images/');
var imagesSubFolders = imagesFolder.getFiles();
var isfLength = imagesSubFolders.length;

if (isfLength) { // check for contents of 'images' folder
for (var j = 0; j < isfLength; j++) { // collect files by subfolder (within the subfolders of 'images')

// NUMBERED FOLDERS
if (imagesSubFolders[j] instanceof Folder) { // check for subfolders

// get all images within the subfolders of 'images'
var folder = imagesSubFolders[j];
var folderName = folder.name;
var images = folder.getFiles();
var iLength = images.length;
var docRef = '';

// open template and collect images
var template = new File(subFolders[l] + '/' + folderName + '.psd');
if (template.exists) {
docRef = open(template);

// count the number of files
for (var i = 0; i < iLength; i++) {

// open each document in file list
if (images instanceof File) {

// get the file name
var fName = images.name.toLowerCase();

// check for supported file formats
if ((fName.indexOf('.gif') == -1) && (fName.indexOf('.jpg') == -1) && (fName.indexOf('.png') == -1) && (fName.indexOf('.bmp') == -1) && (fName.indexOf('.tif') == -1) && (fName.indexOf('.psd') == -1)) {
// do nothing; skip unsupported formats
}
// combine documents into new document using their original names
else {
var imgRef = open(images); // open documents from list
var imgName = imgRef.name; // get document name
imgRef.flatten(); // ** might need to set background to white
imgRef.changeMode(ChangeMode.RGB); // ** might need to get template colour mode

// duplicate the layer into the new document
imgRef.activeLayer.duplicate(docRef, ElementPlacement.PLACEATBEGINNING);
imgRef.close(SaveOptions.DONOTSAVECHANGES);

// name duplicate layer using the original document name (without the extension)
docRef.activeLayer.name = imgName.substring(0, imgName.lastIndexOf('.'));

// apply Auto Color command
autoColor();

// properties for drop-shadow layer effect
var dsColor = [0, 0, 0]; // RGB color
var dsDistance = 5; // distance in pixels
var dsSize = 5; // size in pixels
var dsOpacity = 30; // opacity in percent

// properties for stroke layer effect
var sWidth = 24; // stroke width in pixels
var sColor = [255, 255, 255]; // RGB color
var sPosition = 'InsF'; // stroke position: insF = inside; OutF = outside; CtrF = center
var sOpacity = 100; // opacity in percent

// apply layer effects
applyEffects(dsColor, dsDistance, dsSize, dsOpacity, sWidth, sColor, sPosition, sOpacity);
}
}
} // end image file loop

// clean up, save, and then close documents
if (docRef) {
// delete empty layer
//docRef.artLayers[docRef.layers.length - 1].remove();

// reveal and trim canvas to fit all layers
//docRef.revealAll();
//docRef.trim(TrimType.TRANSPARENT, true, true, true, true);

// check for document changes and save
if (!docRef.saved) {
docRef.save();
}
// display message if no supported documents were found in the designated folder
else {
alert('Sorry, but folder "' + folderName + '" doesn\'t contain any recognized image formats.\n\nPress OK to continue.');
}
docRef.close(SaveOptions.DONOTSAVECHANGES);
}
}
// display message if no template is found
else {
alert('Sorry, but no template was found for folder "' + folderName + '".\n\nPress OK to continue.');
}
} // end numbered folders (in 'images' folder)
}
} // end 'images' contents
else {
alert('Sorry, but folder "' + imagesFolder + '" doesn\'t contain any image subfolders.\n\nPress OK to continue.');
}
} // end 'images' folder
else if (!imagesFolder && (k == (sfcLength - 1))) {
alert('Sorry, but folder "' + subFolders[l] + '" doesn\'t contain an "images" folder.\n\nPress OK to continue.');
}
}
} // end subfolder contents
else {
alert('Sorry, but folder "' + subFolders[l] + '" doesn\'t contain any files or folders.\n\nPress OK to continue.');
}
}
} // end root folder contents
else {
alert('Sorry, but folder "' + rootFolder + '" doesn\'t contain any subfolders.');
}
}

///////////////////////////////////////////////////////////////////////////////
// applyEffects - Script Listener output for drop shadow & stroke effects
///////////////////////////////////////////////////////////////////////////////
function applyEffects(dsColor, dsDistance, dsSize, dsOpacity, sWidth, sColor, sPosition, sOpacity) {
var id22 = charIDToTypeID('setd');
var desc4 = new ActionDescriptor();
var id23 = charIDToTypeID('null');
var ref1 = new ActionReference();
var id24 = charIDToTypeID('Prpr');
var id25 = charIDToTypeID('Lefx');
ref1.putProperty(id24, id25);
var id26 = charIDToTypeID('Lyr ');
var id27 = charIDToTypeID('Ordn');
var id28 = charIDToTypeID('Trgt');
ref1.putEnumerated(id26, id27, id28);
desc4.putReference(id23, ref1);
var id29 = charIDToTypeID('T ');
var desc5 = new ActionDescriptor();
var id30 = charIDToTypeID('Scl ');
var id31 = charIDToTypeID('#Prc');
desc5.putUnitDouble(id30, id31, 100.000000);
var id32 = charIDToTypeID('DrSh');
var desc6 = new ActionDescriptor();
var id33 = charIDToTypeID('enab');
desc6.putBoolean(id33, true);
var id34 = charIDToTypeID('Md ');
var id35 = charIDToTypeID('BlnM');
var id36 = charIDToTypeID('Mltp');
desc6.putEnumerated(id34, id35, id36);
var id37 = charIDToTypeID('Clr ');
var desc7 = new ActionDescriptor();
var id38 = charIDToTypeID('Rd ');
desc7.putDouble(id38, dsColor[0]);
var id39 = charIDToTypeID('Grn ');
desc7.putDouble(id39, dsColor[1]);
var id40 = charIDToTypeID('Bl ');
desc7.putDouble(id40, dsColor[2]);
var id41 = charIDToTypeID('RGBC');
desc6.putObject(id37, id41, desc7);
var id42 = charIDToTypeID('Opct');
var id43 = charIDToTypeID('#Prc');
desc6.putUnitDouble(id42, id43, dsOpacity);
var id44 = charIDToTypeID('uglg');
desc6.putBoolean(id44, true);
var id45 = charIDToTypeID('lagl');
var id46 = charIDToTypeID('#Ang');
desc6.putUnitDouble(id45, id46, 120.000000);
var id47 = charIDToTypeID('Dstn');
var id48 = charIDToTypeID('#Pxl');
desc6.putUnitDouble(id47, id48, dsDistance);
var id49 = charIDToTypeID('Ckmt');
var id50 = charIDToTypeID('#Pxl');
desc6.putUnitDouble(id49, id50, 0.000000);
var id51 = charIDToTypeID('blur');
var id52 = charIDToTypeID('#Pxl');
desc6.putUnitDouble(id51, id52, dsSize);
var id53 = charIDToTypeID('Nose');
var id54 = charIDToTypeID('#Prc');
desc6.putUnitDouble(id53, id54, 0.000000);
var id55 = charIDToTypeID('AntA');
desc6.putBoolean(id55, false);
var id56 = charIDToTypeID('TrnS');
var desc8 = new ActionDescriptor();
var id57 = charIDToTypeID('Nm ');
desc8.putString(id57, 'Linear');
var id58 = charIDToTypeID('ShpC');
desc6.putObject(id56, id58, desc8);
var id59 = stringIDToTypeID('layerConceals');
desc6.putBoolean(id59, true);
var id60 = charIDToTypeID('DrSh');
desc5.putObject(id32, id60, desc6);
var id61 = charIDToTypeID('FrFX');
var desc9 = new ActionDescriptor();
var id62 = charIDToTypeID('enab');
desc9.putBoolean(id62, true);
var id63 = charIDToTypeID('Styl');
var id64 = charIDToTypeID('FStl');
var id65 = charIDToTypeID(sPosition);
desc9.putEnumerated(id63, id64, id65);
var id66 = charIDToTypeID('PntT');
var id67 = charIDToTypeID('FrFl');
var id68 = charIDToTypeID('SClr');
desc9.putEnumerated(id66, id67, id68);
var id69 = charIDToTypeID('Md ');
var id70 = charIDToTypeID('BlnM');
var id71 = charIDToTypeID('Nrml');
desc9.putEnumerated(id69, id70, id71);
var id72 = charIDToTypeID('Opct');
var id73 = charIDToTypeID('#Prc');
desc9.putUnitDouble(id72, id73, sOpacity);
var id74 = charIDToTypeID('Sz ');
var id75 = charIDToTypeID('#Pxl');
desc9.putUnitDouble(id74, id75, sWidth);
var id76 = charIDToTypeID('Clr ');
var desc10 = new ActionDescriptor();
var id77 = charIDToTypeID('Rd ');
desc10.putDouble(id77, sColor[0]);
var id78 = charIDToTypeID('Grn ');
desc10.putDouble(id78, sColor[1]);
var id79 = charIDToTypeID('Bl ');
desc10.putDouble(id79, sColor[2]);
var id80 = charIDToTypeID('RGBC');
desc9.putObject(id76, id80, desc10);
var id81 = charIDToTypeID('FrFX');
desc5.putObject(id61, id81, desc9);
var id82 = charIDToTypeID('Lefx');
desc4.putObject(id29, id82, desc5);
executeAction(id22, desc4, DialogModes.NO);
}

///////////////////////////////////////////////////////////////////////////////
// autoColor - apply Auto Color image adjustment
///////////////////////////////////////////////////////////////////////////////
function autoColor() {
var id22 = charIDToTypeID('Lvls');
var desc4 = new ActionDescriptor();
var id23 = stringIDToTypeID('autoBlackWhite');
desc4.putBoolean(id23, true);
var id24 = stringIDToTypeID('autoNeutrals');
desc4.putBoolean(id24, true);
executeAction(id22, desc4, DialogModes.NO);
}

///////////////////////////////////////////////////////////////////////////////
// isCorrectVersion - check for Adobe Photoshop CS2 (v9) or higher
///////////////////////////////////////////////////////////////////////////////
function isCorrectVersion() {
if (parseInt(version, 10) >= 9) {
return true;
}
else {
alert('This script requires Adobe Photoshop CS2 or higher.', 'Wrong Version');
return false;
}
}

///////////////////////////////////////////////////////////////////////////////
// showError - display error message if something goes wrong
///////////////////////////////////////////////////////////////////////////////
function showError(err) {
if (confirm('An unknown error has occurred. Would you like to see more information?')) {
alert(err + ': on line ' + err.line, 'Script Error', true);
}
}

///////////////////////////////////////////////////////////////////////////////
// restoreUnits() - restore original ruler units
///////////////////////////////////////////////////////////////////////////////
function restoreUnits() {
preferences.rulerUnits = originalRulerUnits;
}


// test initial conditions prior to running main function
if (isCorrectVersion()) {
try {
// remember unit settings
var originalRulerUnits = preferences.rulerUnits;

// call the main function
var rootFolder = Folder.selectDialog('Please select the root folder:', Folder(defaultFolder));

// if a folder was selected, proceed; otherwise, end
if (rootFolder) {
collectSubfolders();
restoreUnits(); // restore user ruler units
}
else {
// alert('Script cancelled by user.');
}
}
catch(e) {
showError(e);
restoreUnits(); // restore user ruler units
}
}


______________


Any help would help tremendously, Thanks Guys