lecter
lecter

Reputation:

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

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

Upvotes: 10

Views: 14203

Answers (4)

frankrolf
frankrolf

Reputation: 26

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

    fileObj.execute()

Upvotes: 0

Ruan Mendes
Ruan Mendes

Reputation: 92314

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

Upvotes: 4

Josh Voigts
Josh Voigts

Reputation: 4132

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.

Upvotes: 9

Ciaran Archer
Ciaran Archer

Reputation: 12466

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.

Upvotes: -4

Related Questions