How to get file path?

I’m using Photoshop 22.5.3 on Win10, in this part of code I would like to achieve how “Automate-Batch” interface was made(if you click “Choose…”, though folder instead of file, the path will be printed below).

async function OpenAFile(){
    const fs = require("uxp").storage.localFileSystem;
    const file =await fs.getFileForOpening({type: ["txt"]});
    if(!file){return;}
    const text = await file.read();
    filePath = file.getNativePath();
    //const fileOrigin = file.original;
    //var filePath = fileOrigin.getNativePath()
}
function OutputFunc(){
    document.getElementById("OutputDiv").innerHTML = `${window.filePath}`
}
document.getElementById("btnOpenAFile").addEventListener("click", OpenAFile);
document.getElementById("Test").addEventListener("click", OutputFunc)


It print “undefined” where there should be an absolute path of my selected file.


This is what debugger is talking about.
I’m pretty sure there are some dumb problems in my code but I just can’t find em where.

Where and how is your initial filePath variable defined?
You’re setting it as filePath = file.getNativePath();, but accessing as window.filePath. Maybe you should set it also to window.filePath?

I think you’ve mixed up the two ways to get the native path:
You can either read the property directly on an entry:
entry.nativePath
Or call the function on the FileSystemProvider with an entry as the parameter:
fileSystemProvider.getNativePath(entry)

edit: I just noticed that this is from the XD documentation, not sure if it’s the same for Photoshop currently. Searching the docs for entry or storage does only give me the XD link however.

filePath = file.getNativePath()
Yeah because I can’t directly console.log(filePath) in debugger so I’m thinking about making that a global variable… that’s why I do “window.filePath”.

Thank you for suggestions! I think I’ve got it.
The key seems to be “getNativePath()”, which is a method belongs to “fs” instead of a file.
And I find out this.
https://www.adobe.io/photoshop/uxp/2022/uxp/reference-js/Modules/uxp/Persistent%20File%20Storage/FileSystemProvider/
but I don’t know why there is a blank in (), maybe there should a token or something?
image
Anyway this should be the solution, thanks again! :smiley:

async function OpenAFile(){
    const fs = require("uxp").storage.localFileSystem;
    const file =await fs.getFileForOpening({type: ["txt"]});
    
    if(!file){return;}
    const text = await file.read();
    filePath = fs.getNativePath(file)
    // const fileOrigin = file.original;
    // var filePath = fileOrigin.getNativePath()
}
function OutputFunc(){
    document.getElementById("OutputDiv").innerHTML = `${window.filePath}`
}


document.getElementById("Test").addEventListener("click", OutputFunc)
document.getElementById("btnOpenAFile").addEventListener("click", OpenAFile);