0

I am trying to make a script for photoshop in js, and I want to add an image to a new layer. The proble is that I simply cant find the jpg (teszt_fekvő.jpg), evendough it is in the same folder. Here is the code:

var newDoucument=app.documents.add(1080, 1080, 300, "New Document", NewDocumentMode.RGB);
var layers = newDoucument.artLayers;
var newLayer = layers.add();
newLayer.name = "New Layer";

var imagePath = new File("~/teszt_fekvő.jpg");

if(imagePath.exists) {
  placeFile(imagePath);
} else {
  alert("File not found");
}

I have tried absolute filepath, and I am 100% that the name of the file is correct.

5
  • 1
    Note that you're assigning imagePath = new File(...) and then you immediate throw that away and overwrite the variable by using imagePath = File.openDialog(...). Commented May 21, 2024 at 20:19
  • thanks for the comment left it there accidentally Commented May 21, 2024 at 20:57
  • 1
    File is a thin wrapper around the OS's file handling without its own logic, so which OS are you running? Because if it's Windows, ~ is not part of a valid path (so your script will silently error out), and if it's MacOS, ~ is your home directory. One thing you probably want to do is make liberal use of try { ... } catch (e) { alert(...) } to catch errors. Photoshop scripting is basically an exercise in "do I remember all the best practices from 1998" because of how old the version of JS involved is =) Commented May 21, 2024 at 21:06
  • Thanks for the help it solved the issue. The funny thing is, that if the JS is really that old, that means I wasnt even born yet Commented May 21, 2024 at 22:54
  • It's ActionScript 3, based on JavaScript 1.5. So yeah, it really is that old. Commented May 22, 2024 at 15:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.