如何使用Adobe Indesign CS4中的Javascript打开文本文件?

9 javascript adobe-indesign

如何打开文本文件,阅读内容,然后将内容插入InDesign中的文档?

Jos*_*gts 8

以下是从InDesign中读取文件的示例.如果您也想写入文件,则需要open使用file写入模式w.

// 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();
Run Code Online (Sandbox Code Playgroud)

File在线对象文档的链接.您还可以在此处找到InDesign脚本DOM文档的其余部分.


Cia*_*her -4

出于安全原因,Javascript 不允许访问您计算机的操作系统、文件或目录,因此无法直接使用 Javascript 访问文本文件。

通常,服务器端技术(例如 PHP、Adobe Coldfusion、Java 或 .NET)用于通过 HTML 表单提交上传文件、读取文件并执行所需的任何操作。

我希望这有帮助。

  • 您似乎对 Web 浏览器中存在的 Javascript 引擎感到困惑。InDesign 在应用程序中嵌入了 Javascript 引擎,通常用于为复杂任务编写脚本(Photoshop 也有相同的)。您绝对可以在 InDesign 中编写访问文件系统的 Javascript,只是文档很少。 (8认同)
  • 我不同意 Ciaran 的观点,你不需要回答问题来指出现有答案不正确。尤其是当回答者完全误解了问题,而蒂尼斯特指出了误解。 (7认同)