• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Convert images on InDesign layout to PSD via Photoshop

Contributor ,
Aug 16, 2024 Aug 16, 2024

Copy link to clipboard

Copied

Hello, 

 

I have this script which a user can select and image in Indesign and it will via Photoshop convert that image to PSD and save it to a particular output. 

When I run it it works, however I am not a scripter so any feedback or if you think that there are problems which could corrupt images etc please can you let me know.

 

I am bit parnoid becuase an easlier verion resulted in class not registerd. The older version was using a windows batch code snippet to move files  - it was to overcome any permissions issues which on an inital script had encounted.  The result was very bad  - if I had tested an image all other files in that location when in file explorer hitting open would prompt the below error. 

 

SmythWharf_0-1723841039378.png

 

The reason why there was origionally windows bat code was that a temp out file was made and moved to the wanted to location. This was becuase I used to get windows permission errors when photoshop itself was outputting the file. Which I didnt understand becuase if I was saving to the output folder I would not have a permission problem, the script seem to encounter that. Anyhow not sure if that makes sense.

 

Getting to the point - below is the script. It no longer uses windows bat code - and should only be indesign and photoshop based. 

 

Any advice and comments are welcome for potential errors and or improvements.

 

Thank you 

 

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;
    }

    var outputFolder = new Folder("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

    // Check if the output folder exists, and stop if it doesn't
    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();

 

Thanks 

 

Smyth 

TOPICS
SDK

Views

229

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 17, 2024 Nov 17, 2024

Copy link to clipboard

Copied

LATEST

@SmythWharf wrote on 16 August 2024

 

Apologies for not seeing your post for three months. I've moved it from Photoshop Developers to the InDesign forum. 

 

InDesign allows exports to PNG and JPEG, but not directly to PSD.

https://helpx.adobe.com/indesign/using/export-jpeg-format.html

You can also right-click an image in InDesign (Links panel or in the layout) and choose to edit it in Photoshop. Once in Photoshop, you can save it in a new format. I'm guessing that the original link will not be updated and you will need to use Replace.

 

I am not a scripter and am not able to follow your steps, but someone who is will likely pick up on it.

 

Jane

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines