自动将文本框放置在彼此下方 (JavaScript)

发布于 2025-01-16 00:15:24 字数 803 浏览 3 评论 0原文

我正在尝试自动化 inDesign 从 JSON 文件创建文本框架,其中每个记录都应放置在文本框架中,该文本框架位于前述记录的正下方。

var tweets_json = '[ {"name":"record_1"}, {"name":"record_2"}, ... ]';  // I have not yet figured out how to import external JSON files, so tor testing this is my current way to go
var tweets = eval("(" + tweets_json + ")");

for ( var t = 0; t < tweets.length; t++ ) {
  var newTextFrame = page.textFrames.add();
  newTextFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY;
  var tweet = tweets[ t ];

  newTextFrame.parentStory.texts.item(0).applyParagraphStyle(styleContent, true);
  newTextFrame.geometricBounds = [y1,x1,y2,x2];   // Ignore values
  newTextFrame.contents = tweet.name;
}

我几天前开始 inDesign 脚本编写,文档很难找到我正在寻找的解决方案,特别是对于我的基本 JavaScript 知识,因此非常感谢您的帮助!谢谢你!

I am trying to automate inDesign to create text frames from a JSON file, where every record shall be placed in a text frame, which is right below the foregoing record.

var tweets_json = '[ {"name":"record_1"}, {"name":"record_2"}, ... ]';  // I have not yet figured out how to import external JSON files, so tor testing this is my current way to go
var tweets = eval("(" + tweets_json + ")");

for ( var t = 0; t < tweets.length; t++ ) {
  var newTextFrame = page.textFrames.add();
  newTextFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY;
  var tweet = tweets[ t ];

  newTextFrame.parentStory.texts.item(0).applyParagraphStyle(styleContent, true);
  newTextFrame.geometricBounds = [y1,x1,y2,x2];   // Ignore values
  newTextFrame.contents = tweet.name;
}

I started inDesign scripting a few days ago and the documentation is hard to find the solution i am looking for, especially with my basic JavaScript knowledge, so any help is appreciated! Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不会写诗 2025-01-23 00:15:24

可能是这样的:

var doc = app.activeDocument;
var page = doc.pages[0];

// get JSON data (just as an example)
var tweets_json = '[ {"name":"line1 line2"}, {"name":"line3"}, {"name":"line4, line5"}]';
var tweets = eval("(" + tweets_json + ")");

// set geometric bounds for the first text frame
var x = 0, y = 0;                          // coordinates of top a left corner of the text frame
var width = 15;                            // width of the text frame
var min_heigth = 20;                       // minimal heigth of the text frame
var bottom = min_heigth;                   // bottom edge of the text frame
var bounds = [y, x, bottom, width];

for (var t = 0; t < tweets.length; t++) {
    var tweet = tweets[t];

    // create the a new text frame and set its geometric bounds
    var newTextFrame = page.textFrames.add();
    newTextFrame.geometricBounds = bounds;

    // fill the box with a text and apply the style
    newTextFrame.contents = tweet.name;
    newTextFrame.parentStory.texts[0].applyParagraphStyle(styleContent, true);

    // resize the created text box with auto sizing
    newTextFrame.textFramePreferences.autoSizingReferencePoint = AutoSizingReferenceEnum.TOP_CENTER_POINT;
    newTextFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY;

    // set the new geometric bounds for the next text frame
    bounds = newTextFrame.geometricBounds; // get the bounds of the created text frame
    bottom = bounds[2];                    // get the new bottom edge
    bounds[0] = bottom;                    // set the top edge for the next text frame
    bounds[2] = bounds[2] + min_heigth;    // set the bottom edge for the next text frame
}

结果:

在此处输入图像描述

出于教育目的,代码有点冗长。实际上它可以更短。

It could be something like this:

var doc = app.activeDocument;
var page = doc.pages[0];

// get JSON data (just as an example)
var tweets_json = '[ {"name":"line1 line2"}, {"name":"line3"}, {"name":"line4, line5"}]';
var tweets = eval("(" + tweets_json + ")");

// set geometric bounds for the first text frame
var x = 0, y = 0;                          // coordinates of top a left corner of the text frame
var width = 15;                            // width of the text frame
var min_heigth = 20;                       // minimal heigth of the text frame
var bottom = min_heigth;                   // bottom edge of the text frame
var bounds = [y, x, bottom, width];

for (var t = 0; t < tweets.length; t++) {
    var tweet = tweets[t];

    // create the a new text frame and set its geometric bounds
    var newTextFrame = page.textFrames.add();
    newTextFrame.geometricBounds = bounds;

    // fill the box with a text and apply the style
    newTextFrame.contents = tweet.name;
    newTextFrame.parentStory.texts[0].applyParagraphStyle(styleContent, true);

    // resize the created text box with auto sizing
    newTextFrame.textFramePreferences.autoSizingReferencePoint = AutoSizingReferenceEnum.TOP_CENTER_POINT;
    newTextFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_ONLY;

    // set the new geometric bounds for the next text frame
    bounds = newTextFrame.geometricBounds; // get the bounds of the created text frame
    bottom = bounds[2];                    // get the new bottom edge
    bounds[0] = bottom;                    // set the top edge for the next text frame
    bounds[2] = bounds[2] + min_heigth;    // set the bottom edge for the next text frame
}

Result:

enter image description here

The code a bit verbose, for educational purposes. Actually it can be way shorter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文