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

How to open a file in Photoshop with javascript

Contributor ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

HI!

I want to open a file/image with a button click in an extension, in photoshop >> javascript. 

Possibly with relative path  to the extension file (it have to work on several different computers)

I tried several scripts:

===================================================================================

function somename(){


#target photoshop

var fileRef = "C:\\Users\\Username\\Desktop\\scripting\\image.png"     // I tried several differnet syntay about the path >> / \  \\ etc. etc.

var doc = open(fileRef);

}

================  OR  ==================================================================

function somename2(){


#target photoshop

var fileRef = new File( ~/desktop/scripting/image.png )     // I tried several differnet syntay about the path >> / \  \\ etc. etc.

app.open(fileRef)

}

======================================================

I know these paths are absolute but >> None of them had work so far


Any ideas?


ps: im new to this >> please be specific or provide some example 🙂  thanks!

TOPICS
Actions and scripting

Views

39.7K

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

correct answers 1 Correct answer

Contributor , Nov 15, 2015 Nov 15, 2015

okay a little update!
Thanks Davide!

this works >> just i was a little to beginner to correctly interpret Davide answer.

So i put these code to the main.js file inside the JS folder:

(function () {

    'use strict';

    var csInterface = new CSInterface(); 

    function init() {

             

        themeManager.init();

             

        $("#something_1").click(function () {

  var csInterface = new CSInterface();

  var extensionPath = csInterface.getSystemPath(SystemPath.EXTENSION);

  var

...

Votes

Translate

Translate
Adobe
Participant ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

var fileRef = "C:/myImage.jpg";

app.open( new File( fileRef ) );

or

var fileRef = new File("C:/myImage.jpg");

app.open( fileRef ); 

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
Contributor ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

thanks for that Nemo! it works!
any ideas to make it relative path to the .jsx file ?!

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
Participant ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

// path to current JSX file
alert( $.fileName );

// path to folder of this JSX file
alert( (new File($.fileName)).parent );

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
Contributor ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

sorry maybe a wasnt specific enough:
I wanted to open the image with a relative path (relative to the jsx file and/or the extension) not an absolute >> becose it have to work in different computers

so instead of:

var myDoc = app.open(File("/c/Users/username/Desktop/scripting/image.png"));


something like this:

var myDoc = app.open(File("../../folder1/image.png"));

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
Community Beginner ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

I just came here searching for the same thing. I'll try to post back here if I figure it out.

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
Contributor ,
Nov 15, 2015 Nov 15, 2015

Copy link to clipboard

Copied

Okay we got result! 🙂 Please let me know if You strugle, now maybe i can help You out if my description is not understandable!

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
Participant ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

// Relative to the current JSX file

// works for ALL computers

var relativePath = $.fileName + "/../../folder1/image.png";

app.open( new File( relativePath ) );

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
Contributor ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

I found another script what is working. It is very specific about the path ( and " and / characters!

So here another working version:

function soemthing(){

#target photoshop

var myDoc = app.open(File("/c/Users/username/Desktop/scripting/image.png"));

}

still strugeling about the make it relative the jsx file

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
Advisor ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

Here are a couple more to ponder.

File(Folder.desktop + "/image.png");

File(File($.fileName).parent.parent + "/image.png");

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
Contributor ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

okay thanks! didnt have luck yet!

see the file structure:
2015-11-14_043952.jpg

im using this code:

//=====================================

var relativePath = $.fileName + "../image/picture.png";    // (but i tried + "/../image/etc. also)

 

app.open( new File( relativePath ) ); 

//======================================

not working yet, im guess i do something wrong ?!?!?

ps: inside the "content" folder there is a example.html file >> this html contains the button, wich trigger the javascript.... im guess the code  should be relative to the jsx, but i tried relative to the html and did not work either.

(works fine with absolute path)

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
Advocate ,
Nov 14, 2015 Nov 14, 2015

Copy link to clipboard

Copied

Hi,

If you are working in an HTML Panel, I'm afraid that $.fileName won't work (at least in Photoshop, possibly it's different in InDesign).

You need to pass down to the JSX the extension's path from the JS.

// JS

var csInterface = new CSInterface();

var extensionPath = csInterface.getSystemPath(SystemPath.EXTENSION);

var imagePath = extensionPath + "/img/myImage.png";

csInterface.evalScript("openFile('" + imagePath + "')");

//JSX

function openFile(imagePath) {

app.open(File(imagePath));

}

The path is relative to the Extension's root, and please note the extra quotes.

Hope this helps.

Davide Barranca

---

www.davidebarranca.com

www.cs-extensions.com

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
Contributor ,
Nov 14, 2015 Nov 14, 2015

Copy link to clipboard

Copied

Thanks!
I had put Your code into the jsx file, but still nothing....

i tried every version of the path relative to the jsx,to the extension(s), to the index.html, to the content folder... html's... come out empty

i should put the code above into the called javascript .jsx file right?!?! (so i mean into one single file)   or?!?!

the absolute path are still working.
please let me know if anybody see what im doing wrong!?

thanks

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
Contributor ,
Nov 15, 2015 Nov 15, 2015

Copy link to clipboard

Copied

okay a little update!
Thanks Davide!

this works >> just i was a little to beginner to correctly interpret Davide answer.

So i put these code to the main.js file inside the JS folder:

(function () {

    'use strict';

    var csInterface = new CSInterface(); 

    function init() {

             

        themeManager.init();

             

        $("#something_1").click(function () {

  var csInterface = new CSInterface();

  var extensionPath = csInterface.getSystemPath(SystemPath.EXTENSION);

  var imagePath = extensionPath + "/image/samplepicture.jpg";

  csInterface.evalScript("openFile('" + imagePath + "')");

        });

    }   

    init();

}());

And this is the hostscript.jsx, inside JSX folder:

function openFile(imagePath) {

app.open(File(imagePath));

}

And the index.html call this whole thing like this:

<body class="hostElt">

  <table width="200" border="0" align="center" cellpadding="5">

      <tr>

        <td><button class="button_css" id="something_1">Button Text</button></td>

      </tr>

    </table>

    <script src="js/libs/CSInterface.js"></script>

    <script src="js/libs/jquery-2.0.2.min.js"></script>

    <script src="js/themeManager.js"></script>

    <script src="js/main.js"></script>

 

    <script src="jsx/hostscript.jsx"></script>
</body>

A very big thanks for everybody who helped!!! Take Care!

Ben

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
Community Beginner ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

Is there any option to open the file in "Preview" app in Mac and same for Windows?

The above code is opening the file in photoshop and I want to open the file in OS image preview app from cc extension PHXP and ILST

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
Contributor ,
Nov 15, 2015 Nov 15, 2015

Copy link to clipboard

Copied

Davide! Thanks Again! Sorry to not mark your answer as correct!
Clearly the Glory is Yours!! 
ps: I provide my own answer with a description and marked that with correct.

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
Advocate ,
Nov 15, 2015 Nov 15, 2015

Copy link to clipboard

Copied

Hi,

for some reason the forum software didn't let me post a further explanation after your response, nice to hear you've been able to get there anyway 🙂

Cheers

Davide

---

www.davidebarranca.com

www.cs-extensions.com

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
Explorer ,
May 30, 2016 May 30, 2016

Copy link to clipboard

Copied

I did exactly the same and it doesn't work. Any suggestion what's going wrong?

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
Contributor ,
Jul 23, 2016 Jul 23, 2016

Copy link to clipboard

Copied

do You use the multi or single jsx version?!?!
(one or more jsx file contains the extendscript codes?!?

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
Advisor ,
Nov 13, 2015 Nov 13, 2015

Copy link to clipboard

Copied

var sfile = File($.fileName);

var imageFolder = Folder(sfile.parent.parent + "/image");

var image = File(imageFolder + "/image.png");

This should work. If it doesn't, step through it with the ESTK debugger and check the intermediate values

to see if you can make sense of what's going on.

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
New Here ,
Sep 17, 2016 Sep 17, 2016

Copy link to clipboard

Copied

I try this from upper. when click index for run. debugger chrome show error below.

I don't know. I ' m wrong.

Please helps.

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
Explorer ,
Oct 04, 2017 Oct 04, 2017

Copy link to clipboard

Copied

Hello and thanks for your time beforehand,

I'm new to Javascript and scripting for Photoshop. I'm trying to understand how to open a psd file using Javascript and cannot figure it out.

The code I'm working with is below:

=============================

//OPEN A FILE ON A MAC

//Not clear on the correct syntax structure to target a file

var fileRef = File(app.path + "/file.psd")

//Open the file at the path specified above

if (fileRef.exists){ 

    var docRef = app.open(fileRef); 

}

//If the file above does not exsist show this

else{alert("File does not exsist.");}

=============================

The "/file.psd" part is my snag, I think.

What is the correct way to target the file?

Is it best to use a relative or absolute path?

Is a relative path based on the location of the script?

I know that with html one can target using"../../directory/file.jpg" and is that how Javascript works? I can't find any documentation that addresses this.

Any help is appreciated,

~ Joe D

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
Community Expert ,
Oct 04, 2017 Oct 04, 2017

Copy link to clipboard

Copied

Joe

alert(app.path);

You will see app.path is Photoshop path  did you put a "file.psd" in it? I use CC 2014

Capture.jpg

also not the encoding / and %20 file coding is a bit strange consult the manuals

Adobe Photoshop Scripting

TOOL Guide

Absolute and relative path names

An absolute path name in URI notation describes the full path from a root directory down to a specific file

or folder. It starts with one or two slashes (/), and a slash separates path elements. For example, the

following describes an absolute location for the file myFile.jsx:

Int the Definitive JavaScript look up  decodeURI(

JavaScript decodeURI() Function

so then

alert(decodeURI(app.path));

Capture.jpg

JJMack

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
Explorer ,
Oct 04, 2017 Oct 04, 2017

Copy link to clipboard

Copied

Hello,

Yes, I did. Does your response mean that app,path is only looking for a

Photoshop file, or any valid file?

Thanks

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
Explorer ,
Oct 04, 2017 Oct 04, 2017

Copy link to clipboard

Copied

Hi JJMack and thanks for getting back to me.

The File(app.path + "/file.psd") comes directly from the example given on page 29 of the Photoshop CC Scripting Guide. I changed the name of the file in order to make it target an actual file on my hard drive.

In one of my previous tests I was able to create a folder using a "~" to begin the path to my desired directory. But when I put that exact path into the script above, with file.psd added, I still received an error. Perhaps I should be asking, "What is the simplest method for opening a Photoshop file from within Photoshop?" or "What is the propper way to target a file when scripting for Photoshop?"

Thanks for your time

~ Joe D

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