Skip to main content
commodorejim
New Participant
October 17, 2014
Question

How to open an image in the same folder as a script (relative path)

  • October 17, 2014
  • 2 replies
  • 1442 views

Using Javascript with Photoshop CC (on Windows), I'm trying to create a script that will create a new document of certain dimension size, open an existing image alongside it, and paste it in as a new layer.

My problem is, I can only open the existing image when I supply the absolute path as follows:

var fileRef = new File("C://Users//James//Desktop//images//image01.jpg");

I had thought (obviously incorrectly since it won't work) that I could just specify the name of the file in the call to the new File object, e.g.:

var fileRef = new File("image01.jpg");

but this returns an 1233 error, i.e. "Expected a reference to an existing file or folder" so obviously the script can't see the file.

Is there a way to provide a relative address? This is for an assignment which will be viewed on a Mac so providing an absolute address which works on my PC isn't much good.

Thanks.

This topic has been closed for replies.

2 replies

uberplugins
Inspiring
October 24, 2014

This will get you the full os path to the desktop

Folder.desktop.fsName;

JJMack
Community Expert
Community Expert
October 24, 2014

JJMack
commodorejim
New Participant
October 24, 2014

Hi everyone, thanks for all your responses. Sorry, I've been away for a few days. I'll get cracking on some of these suggestions tomorrow and let you know how I get on. Thanks again!

JJMack
Community Expert
Community Expert
October 17, 2014

// Find the location where this script resides

function findScript() {

  var where = "";

  try {

  FORCEERROR = FORCERRROR;

  }

  catch(err) {

  // alert(err.fileName);

  // alert(File(err.fileName).exists);

  where = File(err.fileName);

  }

  return where;

}

JJMack
commodorejim
New Participant
October 17, 2014

Thanks for your response. I've just started with Javascript scripting in Photoshop so I'm still just a bit confused though .

The code you've provided will return the location on my computer of where the script resides, am I right? And since the script and the images are in the same folder, this should mean a relative address is obtainable?

How could I use this in connection with:

var fileRef = new File("image01.jpg");

var docRef = app.open(fileRef);


where image01,jpg is the image I'd like to open in the same folder as the script?


Thanks again for your help.

JJMack
Community Expert
Community Expert
October 17, 2014

I don't know javascript I just can hack others code.

// Find the location where this script resides

function findScript() {

  var where = "";

  try {

  FORCEERROR = FORCERRROR;

  }

  catch(err) {

  // alert(err.fileName);

  // alert(File(err.fileName).exists);

  where = File(err.fileName);

  }

  return where;

}

var Name = decodeURI(new File(findScript())).replace(/\.[^\.]+$/, ''); // strip the extension off);

var scriptPath = "";

while (Name.indexOf("/") != -1 ) {

         scriptPath= scriptPath + Name.substr(0, Name.indexOf("/") + 1);

         Name = Name.substr(Name.indexOf("/") + 1 ,);

         }

alert("Script Path = '" + scriptPath  +"'\rScript Name = '" + Name + "'");

JJMack