Skip to main content
Known Participant
August 21, 2024
Question

JS cannot read file

  • August 21, 2024
  • 2 replies
  • 254 views

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();

 

 

 

 

This topic has been closed for replies.

2 replies

Stephen Marsh
Community Expert
Community Expert
August 25, 2024

@SmythWharf 

 

Not the answer that you're looking for, but perhaps a good thing with the known issues of possible corruption when writing over a network:

 

https://helpx.adobe.com/au/photoshop/kb/networks-removable-media-photoshop.html

 

jane-e
Community Expert
Community Expert
August 24, 2024

@SmythWharf 

 

Have you tried unembedding the images from InDesign? Links panel > Select embedded images > Links panel menu > Unembed link ?

 

Jane

 

 

Known Participant
August 24, 2024

Hello, 

 

The file is not embedded unfortunently, it is simply just on the desktop. 

 

The issue arrises that the script can read the file path perfectly on a local computer but a network path where I do not know if it is  C D or E etc drive is tough 

 

so for example if the network link is \\server\\name\\desktop\\image it cant work but its fine if the link is c\user\desktop 

 

etc 

 

so it cannot find either the image or output folder in such instances