# 紹介、Introduction
今現在、InDesignを制御するには:Javascript、Applescript、VBScriptのみが正式にサポートされている。だが、COM(Component Object Model)経由すれば、DOM(Document Object Model)へのアクセルは可能で、即ち、他の言語でもInDesign(またはIllustrator,Photoshop等)を制御できる。Pythonの場合、Windowsにはpywin32(https://pypi.org/project/pywin32/)、OSXにはappscript(https://pypi.org/project/appscript/)のラッパー(架け橋)が存在する。こちらでは、OSXを例に、色々試してみる。

Currently, only Javascript, Applescript, and VBScript are officially supported for controlling InDesign. However, through the Component Object Model (COM), access to the Document Object Model (DOM) is possible, which means that InDesign (or Illustrator, Photoshop, etc.) can be controlled using other languages. For Python, there are wrappers for pywin32 (https://pypi.org/project/pywin32/) on Windows and appscript (https://pypi.org/project/appscript/) on OSX. Here, we will try various things on OSX as an example.

# メリットとデメリット、Advantages and disadvantages
Pythonライブラリが豊富、DBとInDesignドキュメント間の直接データ交換がやりやすい。一方、処理速度のほう、javascriptの1/3程度しかでない。しかも、コードの癖が強くて、よくわけ分からなくなる。特に、配列のインデックス(Applescriptは1、Pythonは0から)で、後々のデバッグとメンテナンスには、要注意。

Python libraries are rich, and direct data exchange between DB and InDesign documents is easy. On the other hand, the processing speed is only about 1/3 of Javascript, and the code habits are strong and often hard to understand. In particular, for array indexes (Applescript starts at 1, Python starts at 0), attention is required for debugging and maintenance in the future.

# ライブラリインストール、Library Installation

  • (Mac)pip install appscript
  • Source:https://pypi.org/project/appscript/
  • help: http://appscript.sourceforge.net/py-appscript/doc_3x/appscript-manual/index.html


# 特徴、Features

  • DOMはapplescriptと同様(applescriptのラッパーなので)
  • 構文はjavascriptと同様:object.property(キャメルケースをアンダーバーに)
  • 新規オブジェクトの作成:親オブジェクト.make(new=k.オブジェクト)※。属性付きの場合は:{k.属性1: 値1, k.属性2: 値2}。(※kはkeyword)
  • The DOM is similar to Applescript (since it is a wrapper for Applescript)
    Syntax is similar to Javascript: object.property (camel case to underscore)
    Creating a new object: parent_object.make(new=k.object)*. If you want to add attributes: {k.attribute1: value1, k.attribute2: value2}. (*k is keyword)

# サンプル集 sample code

https://github.com/mlove4u/InDesign-Automation-Python-Mac-Appscript


# サンプル

from appscript import *

indd = app('Adobe InDesign CC 2019')
# add document
doc = indd.make(new=k.document)  # applescript: set doc to make document
doc.document_preference.page_height.set(150)
doc.document_preference.page_width.set(100)
doc.name.set("doc")
# 1st page
all_pages = doc.pages  # class: appscript.reference.reference
page1 = all_pages[1]  # applescript index is from 1
"""
all_pages = doc.pages()  # class: list
page1 = all_pages[0]
"""
# add textFrame
tf1 = page1.make(new=k.text_frame)
tf1.contents.set("tf1")
tf1.fill_color.set("Cyan")
tf1.geometric_bounds.set([10, 10, 50, 50])
# add other textFrame with properties
tf2 = page1.make(new=k.text_frame, with_properties={
    k.contents: "tf2",
    k.fill_color: "Magenta",
    k.geometric_bounds: [60, 10, 100, 50]
})
# close document
# doc.close(saving=1634954016)  # 1634954016 = 'ask '
# doc.close(saving=k.ask)

これで、下記のようなindesignドキュメントが作成されると思う。