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
var fileRef = new File('~/Documents/working/jobs/client/file.psd');
Try the above. I'm not sure if capitalization matters.
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.
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
Copy link to clipboard
Copied
I believe, using a "~" will refer to the users Library folder on a mac.
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
Copy link to clipboard
Copied
It should be:
'~/documents/working/jobs/client/file.psd'
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.?
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.
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?
Copy link to clipboard
Copied
app.path refers to the Photoshop program folder. The ~ refers to the users folder. Two completely separate things.
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
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);
Copy link to clipboard
Copied
Hi JJMack, this works great. Thanks for sending it along.
~ Joe D
Copy link to clipboard
Copied
Can i give Path address ?
Copy link to clipboard
Copied
(fle = File('~/desktop/someFile.psd')).exists && open(fle)
Copy link to clipboard
Copied
Umm, capitalization doesn't matter, nor does the new.