How to read the text content of a file from a path using a PSJS script

How to read the content of a file from the path: C:\Users\Administrator\Desktop\X.ini using a .PSJS script
Example JSX code, thank you.

#target photoshop

var filePath = "C:\\Users\\Administrator\\Desktop\\X.ini";
var file = new File(filePath);

if (file.exists) {
    file.open('r');
    var fileContent = file.read();
    file.close();
    alert("File Content:\n" + fileContent);
} else {
    alert("File does not exist: " + filePath);
}

The snippet below should mimic your JSX code. Please note there are some limitations on file system access with UXP so you may have to work around that especially since it looks like you are on Windows (Adobe Docs, MS Docs).

const { app } = require("photoshop");
const fs = require("uxp").storage.localFileSystem;

const filePath = "/Users/jbd/Desktop/example.txt";

try {
    const entry = await fs.getEntryWithUrl(`file:${filePath}`);
    const data = await entry.read();
    app.showAlert(`File Content:\n${data}`);
} catch (error) {
    app.showAlert(`File does not exist: ${filePath}`);
}
1 Like

This is amazing, thank you