Skip to main content
Known Participant
November 5, 2020
Answered

How to create a script to open files with the same name

  • November 5, 2020
  • 1 reply
  • 1337 views
And guys, help me.

I have a Jpg file open in Photosop and in the same folder where this file is located I have a file of the same name but with the PSD format I would like to know if there is any script so that when I open the jpg file it will search in the same folder by the file on PSd and open.

 

This topic has been closed for replies.
Correct answer Stephen Marsh

The following code will work on an open (previously saved) document:

 

 

// Open PSD File with Same Name As Open Doc.jsx
// 2020 - Stephen_A_Marsh

/* https://community.adobe.com/t5/photoshop/how-to-create-a-script-to-open-files-with-the-same-name/td-p/11571441?page=1 */

#target photoshop

if (app.documents.length > 0) {

    var targetDocName = app.activeDocument.name.replace(/\.[^\.]+$/, '.psd'); // presumes a lowercase PSD extension
    var otherFile = app.activeDocument.path.fsName + '/' + targetDocName;
    openOtherFile = File(otherFile);
    open(openOtherFile);

} else {
    alert('Open PSD File with Same Name As Open Doc.jsx' + '\r' + 'You must have a document open to run this script!');
}

 

 

1 reply

Stephen Marsh
Stephen MarshCorrect answer
Community Expert
November 7, 2020

The following code will work on an open (previously saved) document:

 

 

// Open PSD File with Same Name As Open Doc.jsx
// 2020 - Stephen_A_Marsh

/* https://community.adobe.com/t5/photoshop/how-to-create-a-script-to-open-files-with-the-same-name/td-p/11571441?page=1 */

#target photoshop

if (app.documents.length > 0) {

    var targetDocName = app.activeDocument.name.replace(/\.[^\.]+$/, '.psd'); // presumes a lowercase PSD extension
    var otherFile = app.activeDocument.path.fsName + '/' + targetDocName;
    openOtherFile = File(otherFile);
    open(openOtherFile);

} else {
    alert('Open PSD File with Same Name As Open Doc.jsx' + '\r' + 'You must have a document open to run this script!');
}

 

 

Stephen Marsh
Community Expert
November 8, 2020

You can use the previous code in the Open event in the File > Scripts > Scripts Events Manager so that every time you open a file it will check for a .psd file of the same name in the same directory and automatically open it for you.

 

 

One could of course add an extra condition to alert if no matching file was found, or to perhaps open the first file using a file open dialog window rather than using a already open file (see code below).

 

Stephen Marsh
Community Expert
November 8, 2020

This variation on the previous script offers the File > Open dialog window to select a single file to open rather than using the active document (don't select multiple files unless you want an error):

 

 

// Open PSD File with Same Name As Open Doc via Open Dialog.jsx
// 2020 - Stephen A Marsh

/* https://community.adobe.com/t5/photoshop/how-to-create-a-script-to-open-files-with-the-same-name/td-p/11571441?page=1 */

#target photoshop

// Use the File > Open dialog to open the file
var selectFile = app.openDialog();
openFile = File(selectFile);
open(openFile);
var targetDocName = app.activeDocument.name.replace(/\.[^\.]+$/, '.psd'); // presumes a lowercase PSD extension
var otherFile = app.activeDocument.path.fsName + '/' + targetDocName;
openOtherFile = File(otherFile);
open(openOtherFile);