How to open a text file using Javascript from Adobe Indesign CS4?
Asked Answered
S

4

10

How can I open a text file, read the contents, and then insert the contents into a document in InDesign?

Standardbearer answered 30/8, 2009 at 3:36 Comment(0)
C
9

Here's an example of reading a file from InDesign. If you want to write to a file as well, you will need to open the file in write mode w instead.

// Choose the file from a dialog
var file = File.openDialog();

// Or use a hard coded path to the file
// var file = File("~/Desktop/hello world.txt");

// Open the file for reading
file.open("r");

// Get the first text frame of the currently active document
var doc = app.activeDocument;
var firstTextframe = doc.pages[0].textFrames[0];

// Add the contents of the file to the text frame
firstTextframe.contents += file.read();

Here is a link to the File object's documentation online. You can also find the rest of InDesign's scripting DOM documentation here.

Counter answered 6/12, 2012 at 23:29 Comment(0)
D
4

This is the pdf for InDesign JavaScript scripting. There's a few mentions of a File object in there, but it's not documented. http://www.adobe.com/products/indesign/scripting/pdfs/InDesignCS4_ScriptingGuide_JS.pdf

That's because the core utilities for all CS5 products are documented here https://www.adobe.com/content/dam/Adobe/en/devnet/indesign/cs55-docs/InDesignScripting/InDesign-ScriptingTutorial.pdf

or the general documentation: http://www.adobe.com/content/dam/Adobe/en/devnet/scripting/pdfs/javascript_tools_guide.pdf

Look for: File System Access

Dichromaticism answered 10/11, 2010 at 21:37 Comment(0)
W
0

Thanks for the pointer to the various PDFs.
The response to this question is in the execute() command.

    fileObj.execute()
Ways answered 6/5, 2014 at 0:56 Comment(0)
D
-4

Javascript does not allow access to your computer's operating system, files or directories for security reasons, therefore there is no way to access the text file directly using Javascript.

Usually a server-side technology such as PHP, Adobe Coldfusion, Java or .NET (for example) is used to upload the file via a HTML form submission, read it and do whatever it needs to do.

I hope that helps.

Discord answered 30/8, 2009 at 13:0 Comment(3)
You seem to be confusing the Javascript engine as it exists in web browsers. InDesign has a Javascript engine embedded into the application, which is often used to write scripts for complex tasks (Photoshop has the same). You can definitely write Javascript in InDesign that accesses the filesystem, it's just very poorly documented.Briony
@Briony - Thanks for the comment. Perhaps you can now help answer the question?Discord
I disagree with Ciaran, you don't need to answer the question to point out that an existing answer is not correct. Especially when the answerer completely misunderstood the question, and Tinister pointed out the misunderstanding.Dichromaticism

© 2022 - 2024 — McMap. All rights reserved.