Skip to main content
Known Participant
August 21, 2024
Question

Help on InDesign script to convert images to PSD - can't read file locations on networks

  • August 21, 2024
  • 3 replies
  • 712 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.

3 replies

rob day
Community Expert
Community Expert
August 22, 2024

Hi @SmythWharf , looks like your file path string is returning double back slashes in the file path—I get this when I set the destination to the desktop:

 

alert(psdFileName.replace(/\\/g, "\\\\"))

 

 

 

 

If that’s not it, I use a similar script—this saves the new PSD into a folder named PSDs in the parent folder of the original link, and then relinks to the PSD:

 

imageToPSD()
var np;
function imageToPSD(){
    var bt = new BridgeTalk();  
    bt.target = "photoshop"; 
    var img;
    if (app.documents.length > 0) {
        //check selection
        if (app.activeDocument.selection.length == 1 && app.activeDocument.selection[0].graphics.length == 1) {
            img = app.activeDocument.selection[0].images[0];
        } else if (app.activeDocument.selection.length == 1 && app.activeDocument.selection[0].constructor.name == "Image") {
            img = app.activeDocument.selection[0]
        } else{
            alert("Please Select an image.")
            return
        }
        var lnk =  img.itemLink
        if (lnk.linkType != "Photoshop" && lnk.status == LinkStatus.NORMAL) {
            convertToPSD(lnk.filePath, bt)
            //relink to the new PSD
            try {
                lnk.relink(File(np))
            }catch(e) {}
        } 
    } else {
        alert("No Documents Open")
        return
    }
}


/**
* Convert a images to PSD 
* @ param image file path 
* @ param Bridgetalk instance 
* return new file path 
*/

function convertToPSD(pa, bt){
    //convert the psScript function into a string
    bt.body = psScript.toString() + "\rpsScript('"+pa+"');";
    bt.onError = function( inBT ) { alert(inBT.body); };  
    bt.onResult = function(resObj) { 
        np = resObj.body
     } 
     bt.send(1000);   

    function psScript(pa) {  
        app.bringToFront();
        
        //a folder for PSDs inside the links folder
        var f = Folder(File(pa).parent + "/PSDs")
        f.create();
        var of = open (File(pa));
        //file name and new path
        var n = of.name.replace(RegExp('()?\.[^\.]+$'), '');
        var nf = f +"/" + n + ".psd";

        //PSD options
        var so = new PhotoshopSaveOptions();
        so.alphaChannels = true;
        so.embedColorProfile = true;
        so.layers = true
        so.spotColors = true;

        of.saveAs(new File(nf), so, true);
        of.close(SaveOptions.DONOTSAVECHANGES);  
        //returns the new file pat to InDesign
        return nf
    }  
};

 

 

 

Inspiring
August 22, 2024

I'm doing something similar on the pc. I have set a shortcut for "copy full path" in links panel. I have also created a droplet that creates the tiff files.
Then I just have an AHK script that runs this droplet with the path parameter to the file I have selected in the ID.

Robert at ID-Tasker
Legend
August 22, 2024
quote

I'm doing something similar on the pc. I have set a shortcut for "copy full path" in links panel. I have also created a droplet that creates the tiff files.
Then I just have an AHK script that runs this droplet with the path parameter to the file I have selected in the ID.


By @DTPtutorialyCZ-L.Zalesky

 

If you have a lot of those - it would be much easier with the paid version of my ID-Tasker tool. 

 

Robert at ID-Tasker
Legend
August 22, 2024

Are you on Mac? 

 

Can you show examples of the full paths to files on the server? 

 

If you are on a PC - are you mapping path to a drive letter? 

 

Known Participant
August 22, 2024

I am on a PC. 

 

The path of the image and output when I am testing are in the same place. 

 

\\server\data\users\name\Desktop\test

 

I cannot directly see what the driver letter is....

 

I did manage to get the script working when there was a temp window batch snippet but this cuased errors on windows file explore plus ridiculous to have to hand the image over to windows to PS back again etc 

 

Regarding full paths to the server, other than going file properties I am not sure how I bring that up as I am on a network.

 

Thanks for suggestion, 

 

Smyth

Robert at ID-Tasker
Legend
August 22, 2024

You should map this folder to a letter - it will make your life much easier.