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

Read HTML file using fs.read in UXP (UTF-8 format)

Explorer ,
Aug 02, 2023 Aug 02, 2023

Hi All,

 

I'm using the UXP script to read the HTML file from the local folder. But I'm unable to read the file with UTF-8 format. 

 

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

let myInDesign = require("indesign");
let app = myInDesign.app;

const uxpfs = require("uxp").storage;
const ufs = uxpfs.localFileSystem;

const ufs_frm = require('uxp').storage.formats;

var fs = require('fs');

 

const file = await ufs.getFileForOpening({mode: uxpfs.modes.readWrite, type: "file"});

if (!file) {
// no file selected
return;
}

var manifest_str = fs.readFileSync(file);

const myNovel = await file.read({format: formats.utf8});

 
if (window.DOMParser) {
const parser = new DOMParser();

var manifest = parser.parseFromString(manifest_str, "text/xml");

}

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

 

Kindly suggest any one, will appriciate. 

TOPICS
UXP Scripting
1.1K
Translate
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 02, 2023 Aug 02, 2023
Translate
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 ,
Aug 02, 2023 Aug 02, 2023

Hi Rober

 

Thank you, Actually you shared web site is belongs to NodeJS related. We already working with Nodejs. it is working to reading the UTF-8 format with HTML file.

 

We requesting to do in IDJS script. Kindly suggest.

Translate
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 ,
Aug 02, 2023 Aug 02, 2023

Actually the Nodejs formats are not supporting in IDJS. Kindly suggest any one will appriciate.

Translate
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 ,
Aug 02, 2023 Aug 02, 2023

I see an issue with your code, the read method invocation should be

const myNovel = await file.read({format: uxpfs.formats.utf8});

Now after this if I alert myNovel I get the text displayed. After that the control does not go into the if block, which points that window does not have the DOMParser property.

-Manan

Translate
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 ,
Aug 03, 2023 Aug 03, 2023

Dear Manan Joshi,

 

Thank you,

Actually I tried that reading the html file using UTF-8. which you mentioned above. it is working fine now.

 

But need to read the HTML file with DOM parser based on only I can read the <div> elements inside the HTML.

 

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


let myInDesign = require("indesign");
let app = myInDesign.app;

const uxpfs = require("uxp").storage;
const ufs = uxpfs.localFileSystem;

const ufs_frm = require('uxp').storage.formats;

var fs = require('fs');

var DOMParser = require("uxp").DOMParser;

const file = await ufs.getFileForOpening({mode: uxpfs.modes.readWrite, type: "file"});
if (!file) {
// no file selected
return;
}

const text = await file.read({format: uxpfs.formats.utf8});

var parser = window.DomParser;

var dom = parser.parseFromString(text);

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

 

It shows the error for :  Cannot read the properties of undefined (reading 'parseFromString')

 

So that, need your help. How to read the HTML file using UTF-8 with DOM parser or without DOM parser to read the HTML file with inside <div> elements.

 

Kindly help me to achive the issues.

 

Thanks & Regards,

Harihara Sudhan T R.,

 

 

 

Translate
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 ,
Aug 05, 2023 Aug 05, 2023

Hi @Harihara28692478zxj6,

Now that we have figured out that the Window object does not have the DOMParser property, it explains the error you get. The only way now is to search for a library that will do this, and I did suggest one in the other thread of yours and the possible way we could be able to use that. Please try that out and share the update.

https://community.adobe.com/t5/indesign-discussions/dom-parser-not-working-in-idjs-script/m-p/139839...

-Manan

Translate
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 ,
Aug 07, 2023 Aug 07, 2023

Dear Manan,

 

Thank you So much, 🙂 but that will work NodeJS. anyway we already done through NodeJS.

 

🙂 Actually, our firm want to conduct InDesign on its own. That is why we asked.

 

Is there any indication that Adobe will support complete NodeJS?

 

Thanks & Regards

Harihara Sudhan T R.,

Translate
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 ,
Aug 08, 2023 Aug 08, 2023

@Harihara28692478zxj6 the Nodejs package is not something that would need the Nodejs env to work. If it contains just js then it might work for you. Now regarding the support for complete Nodejs API, I doubt that as per the link I shared.

-Manan

Translate
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 ,
Aug 09, 2023 Aug 09, 2023
LATEST

Dear Manan,

 

Thank you for your comments and clarification. But, Adobe will give  the full support with IDJS - NodeJS then really happy to work with new tecc knowledge.

 

Thanks & Regards

Harihara Sudhan T R.,

Translate
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