Skip to main content
Participant
September 22, 2024
Answered

Need help with a script

  • September 22, 2024
  • 1 reply
  • 298 views

Hi, I wrote a script with which I could open a selected image in AdobeInDesign 2023 in Photoshop, and if the folder containing the image had the same file but with the psd extension, it would open it. If not, then the image I selected was opened. This worked fine on the windows operating system, but I bought a macbook with a m1 processor on Sonoma 14.2.1. As I understand it, the macbook system works differently and now my script doesn't work at all. Please help me rewrite the script so that it works correctly, I really need it for work. Please
Here is the code

#target indesign
var doc = app.activeDocument;
var selection = app.selection[0];

if (selection && selection.constructor.name === "Rectangle" && selection.graphics.length > 0) {
    var graphic = selection.graphics[0];
    if (graphic.itemLink) {
        var filePath = graphic.itemLink.filePath;
        var fileFolder = new Folder(filePath).parent;
        var baseFileName = filePath.substr(0, filePath.lastIndexOf('.'));
        var psdFilePath = baseFileName + ".psd";
        var psdFile = new File(psdFilePath);
        
        if (psdFile.exists) {
            openInPhotoshop(psdFilePath);
        } else {
            openInPhotoshop(filePath);
        }
    } else {
        alert("Графічний об'єкт не має посилання на файл.");
    }
} else {
    alert("Виберіть прямокутник з графічним об'єктом.");
}

function openInPhotoshop(filePath) {
    var bt = new BridgeTalk();
    bt.target = "photoshop";
    
    // Відкриваємо файл
    bt.body = "app.open(new File('" + filePath + "'));";
    bt.body += "app.bringToFront();"; // Виводимо Photoshop на передній план
    
    // Для macOS
    if ($.os.indexOf("Mac") > -1) {
        bt.body += "var appFile = new File(app.path + '/' + app.name);";
        bt.body += "app.system('open \"' + appFile.fsName + '\"');";
    }
    
    bt.onResult = function(resultMsg) {
        $.sleep(1000); // Чекаємо секунду, щоб дати час Photoshop відкритися
        app.activate(); // Повертаємо фокус на InDesign
    };
    
    bt.onError = function(errorMsg) {
        alert("Помилка при відкритті файлу в Photoshop: " + errorMsg);
    };
    
    bt.send();
}

 

This topic has been closed for replies.
Correct answer Фанфи35124368fx7y

Thank you for your help, but I've already found the problem myself. It didn't want to open the file because of the Cyrillic name of the file path. I wanted to delete this post, but I don't know how, and the forum moderation doesn't respond to anything

1 reply

rob day
Community Expert
Community Expert
September 22, 2024

Hi @Фанфи35124368fx7y , Your script works for me on OSX Mojave. I don’t think you need the system check in order to open the file—maybe try this and see if the alert returns the correct file path:

 

#target indesign
var doc = app.activeDocument;
var selection = app.selection[0];

if (selection && selection.constructor.name === "Rectangle" && selection.graphics.length > 0) {
    var graphic = selection.graphics[0];
    if (graphic.itemLink) {
        var filePath = graphic.itemLink.filePath;
        var fileFolder = new Folder(filePath).parent;
        var baseFileName = filePath.substr(0, filePath.lastIndexOf('.'));
        var psdFilePath = baseFileName + ".psd";
        var psdFile = new File(psdFilePath);
        
        if (psdFile.exists) {
            openInPhotoshop(psdFilePath);
        } else {
            openInPhotoshop(filePath);
        }
    } else {
        alert("Графічний об'єкт не має посилання на файл.");
    }
} else {
    alert("Виберіть прямокутник з графічним об'єктом.");
}

function openInPhotoshop(filePath) {
    var bt = new BridgeTalk();
    bt.target = "photoshop";

    alert(filePath)
    
    // Відкриваємо файл
    bt.body = "app.open(new File('" + filePath + "'));";
    bt.body += "app.bringToFront();"; // Виводимо Photoshop на передній план

    bt.onResult = function(resultMsg) {
        $.sleep(1000); // Чекаємо секунду, щоб дати час Photoshop відкритися
        app.activate(); // Повертаємо фокус на InDesign
    };
    
    bt.onError = function(errorMsg) {
        alert("Помилка при відкритті файлу в Photoshop: " + errorMsg);
    };
    
    bt.send();
}

 

Фанфи35124368fx7yAuthorCorrect answer
Participant
September 22, 2024

Thank you for your help, but I've already found the problem myself. It didn't want to open the file because of the Cyrillic name of the file path. I wanted to delete this post, but I don't know how, and the forum moderation doesn't respond to anything