InDesign脚本开发:从排版脚本TextFrame剪切段落粘贴至新空白文本框
需求说明
我需要一款实现以下功能的Adobe脚本:
- 选中包含完整排版脚本的TextFrame(包含页面1文本、页面2文本等内容);
- 剪切单个段落(段落通常以
\r或^p作为分隔符); - 由用户选中或新建一个空白TextFrame;
- 将之前剪切的段落粘贴到该新空白TextFrame中;
- 自动跳转至下一个段落重复上述流程。
现有方案调研
我在做排版工作时,发现了面向Photoshop的Typesetterer工具,之后寻找InDesign平台的同类脚本/应用,找到了对应解决方案,试用后可行,至少将我的工作效率提升了2倍。我也试过同类拆分脚本,但后续需要花大量时间调整所有生成TextFrame的尺寸和位置,不过它对我开发最终代码帮助很大。
上述方案的核心问题是要求排版脚本为.txt格式,会丢失原脚本中所有Bold和Italic格式。
现有开发代码
main(); function main() { // Main() just to keep it organized try { var myMeasuringUnit = app.scriptPreferences.measurementUnit; app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS; } catch (exception) { alert(exception.description); exit(); } var myTextFrameSelection = app.activeDocument.selection; cutAndPaste(myTextFrameSelection); try { app.scriptPreferences.measurementUnit = myMeasuringUnit; } catch (exception) {} } function cutAndPaste(myTextFrameSelection) { for (elementsInSelection = 0; elementsInSelection < myTextFrameSelection.length; elementsInSelection++) { if (myTextFrameSelection[elementsInSelection].constructor.name == "TextFrame") { var myParagraphs = myTextFrameSelection[elementsInSelection].texts[0].paragraphs; var isCopied = true; for (var i = 0; i < myParagraphs.length; i++) { try { myParagraphs[i].insertionPoints.itemByRange(0, -2).select(); app.cut(); isCopied = true; } catch (exception) { isCopied = false; } if (isCopied) { /* Attempt at adding a way to select another TextFrame and app.paste() the previously app.cut() line in it var doc = app.activeDocument.selection[0]; doc.addEventListener('afterSelectionChanged', selectionChanged); function selectionChanged() { if (doc.selection[0] instanceof TextFrame && doc.selection[0].contents == '' && doc.selection[1] == null) { if (doc.selection[0] instanceof TextFrame) { doc.selection[0].contents = app.paste(); } } */ app.paste(); // This just pastes it in the same folder } } } } } // For now, what it does it "cut paragraph from the TextFrame but also paste in the same TextFrame"
当前问题
目前我写的脚本仅能实现逐段从TextFrame剪切段落,再粘贴回原TextFrame,距离需求还差在主循环中触发用户选中新TextFrame的逻辑,我注释掉了暂未调通的尝试方案。想请教我是否用错了剪切/粘贴函数?或者修改现有拆分方案使其支持.docx格式是不是更简单?
内容的提问来源于stack exchange,提问作者Vinicius
相关产品推荐
相关产品推荐

