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

Help with opening a Photoshop file using Javascript

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

TOPICS
Actions and scripting

Views

4.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

Community Expert , Oct 05, 2017 Oct 05, 2017
var fileRef = new File('~/Documents/working/jobs/client/file.psd');

Try the above. I'm not sure if capitalization matters.

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

app.path will refer to Photoshop, and not the scripting folder. is that where your file is? Not really a good place for an image file. if the image file is needed for your script, you can make a folder for it in presets, maybe call it "Images." I prefer directly naming a 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
Explorer ,
Oct 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

Hi Chuck, thanks for getting responding.

The file could be anywhere on my local hard drive. I had it in the script directory because I was thinking it would be easier for testing to  keep in in the same directory. For an example, what would be the correct syntax used to get to a file in documents/working/jobs/client/file.psd ? I haven't had any luck finding the correct way to target this.

Thanks, ~ 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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

I believe, using a "~" will refer to the users Library folder on a mac.

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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

Hi Chuck,

The tilde "~" won't help me if it starts my path down the user Library. I need it to start at Document level at least. I have included the path to my file below.

var fileRef = File(app.path + "/Users/joe/Documents/Practice/file.psd")

How would one target this file correctly? I currently get an error that the file does not exist.

Thanks, ~ 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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

It should be:

'~/documents/working/jobs/client/file.psd'

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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

So odd. I pasted your path into the string and I still get an error. There must be something else that I am missing.?

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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

var fileRef = new File('~/Documents/working/jobs/client/file.psd');

Try the above. I'm not sure if capitalization matters.

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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

Your suggestion works:

var fileRef = new File('~/Documents/Working/jobs/client/file.psd')

However, it is different than the original code that I had been using, which is:

var fileRef = File(app.path + '~/Documents/Working/jobs/client/file.psd')

Capitals or not has no effect.

I have been using File(app.path +'~/Documents...

Any thoughts on why that isn't working?

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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

app.path refers to the Photoshop program folder. The ~ refers to the users folder. Two completely separate things.

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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

I see.

Chuck thanks for your time. I figured it was something simple like that I just need a bit of guidance in order to see it.

Thanks you!

~ 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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

Joe

check this out

var selectedPSD =  File.openDialog("Select a PSD file to open" , "Select:*.psd");

if (selectedPSD!=undefined) app.open(selectedPSD);

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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

Hi JJMack, this works great. Thanks for sending it along.

~ 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 Beginner ,
Aug 10, 2021 Aug 10, 2021

Copy link to clipboard

Copied

Can i give Path address ?

 

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
LEGEND ,
Aug 10, 2021 Aug 10, 2021

Copy link to clipboard

Copied

LATEST
(fle = File('~/desktop/someFile.psd')).exists && open(fle)

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 05, 2017 Oct 05, 2017

Copy link to clipboard

Copied

Umm, capitalization doesn't matter, nor does the new.

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