JS cannot read file
Hello,
I have a script which can convert images to PSD from InDesign via Photoshop.
Photoshop however is not able to read the image which indesign is trying to send to photoshop.
It must be something to do with the next work.
I also am not convinced that the script is able to access the output folder properly too...
To be honest I have no idea what is going on - script works fine on local but on a network the script just fails at passing the image to photoshop.
It is not a privilage problem as everything the script is doing I can do by hand i.e. read and write to the following locations.
The script funtions fine it just cannot get the file in question
Any help would be good,
Many thanks,
Smyth
function openInPhotoshopAndConvertToPSD() {
// Check if Photoshop is running
if (BridgeTalk.getStatus("photoshop") !== "IDLE") {
alert("Photoshop is not open. Please open Photoshop and try again.");
return;
}
// Define the output folder path
var outputFolder = new Folder("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
// Check if the output folder exists
if (!outputFolder.exists) {
alert("Output folder does not exist: " + outputFolder.fsName);
return;
}
var selectedItems = app.selection;
// Check if exactly one item is selected
if (selectedItems.length !== 1) {
alert("Please select exactly one image.");
return;
}
var item = selectedItems[0];
// Check if the selected item is a rectangle with an image
if (item.constructor.name == "Rectangle" && item.images.length > 0) {
var imageFile = new File(item.images[0].itemLink.filePath);
if (imageFile.exists) {
var formattedPath = imageFile.fsName.replace(/\\/g, "\\\\");
var originalFileName = decodeURIComponent(imageFile.name.replace(/\.[^\.]+$/, ""));
var sanitizedFileName = originalFileName.replace(/[^a-z0-9]/gi, '');
var psdFileName = outputFolder.fsName + "\\" + sanitizedFileName + ".psd";
// Photoshop script to open the image, save as PSD, and close the document
var photoshopScript = 'var doc = app.open(File("' + formattedPath + '"));' +
'var psdFile = new File("' + psdFileName.replace(/\\/g, "\\\\") + '");' +
'doc.saveAs(psdFile, new PhotoshopSaveOptions(), true);' +
'doc.close(SaveOptions.DONOTSAVECHANGES);';
// Execute the Photoshop script directly
var bt = new BridgeTalk();
bt.target = "photoshop";
bt.body = photoshopScript;
bt.send();
alert("Your image has been converted and saved as:\n" + sanitizedFileName + ".psd");
} else {
alert("Image file not found: " + imageFile.fsName);
}
} else {
alert("Please select a valid image within a rectangle.");
}
}
// Run the function
openInPhotoshopAndConvertToPSD();
