Some example scripts for InDesign automation control on Mac.
- Installation:
pip install appscript
- HP: http://appscript.sourceforge.net/
click here
- Characters a-z, A-Z, 0-9 and underscores (_) are preserved.
- Spaces, hyphens (-) and forward slashes (/) are replaced with underscores.
- Ampersands (&) are replaced by the word 'and'.
- All other characters are converted to 0x00-style hexadecimal representations.
- Names that match Python keywords or names reserved by appscript have an underscore appended. (※)
from appscript import *
indd = app("Adobe InDesign CC 2019")
# print(indd.version())
doc = indd.active_document
# print(doc.name())
tfs = doc.text_frames
# print(len(tfs()))
first_tf = tfs[1] # 1-based indexing
characters = first_tf.characters
# syntax: parent_object.make(new=k.type)
indd.make(new=k.document) # add a new document
doc.make(new=k.page) # add a new page to doc
doc.pages[1].make(new=k.text_frame) # add a new text frame to page 1
first_tf.make(new=k.table) # add a new talbe to first_tf
doc.class_() # k.document
first_tf.class_() # k.text_frame
# same to python keyword arguments
doc.close(saving=k.ask, saving_in="path/to/file.indd")
doc.name()
first_tf.contents()
or
doc.name.get()
first_tf.contents.get()
first_tf.contents.set("sample contents")
first_tf.fill_color.set("Cyan")
first_tf.geometric_bounds.set([0,0,50,100])
or
# set properties at one time (this is faster)
first_tf.properties_.set({
k.contents: "sample contents",
k.fill_color: "Cyan",
k.geometric_bounds: [0,0,50,100]
})
tfs["abc"] # return the 1st text frame whose name is "abc"
# javascript: tfs.itemByName("abc")
tfs.ID(1234) # return text frame whose ID is 1234
# javascript: tfs.itemByID(1234)
characters[1:3] # note: include the 3rd item !!!!!
# javascript: characters.itemByRange(0, 2)
When running a script in InDesign, it operates at a significantly slower speed of approximately 30% compared to officially supported languages such as JavaScript, AppleScript, and VBScript. This is due to the InDesign document continually redrawing during script execution, which significantly slows down processing times.
# Note that this will not speed up processing.
indd.script_preferences.enable_redraw.set(False)
- 00_application
- 01_document
- 02_text_frame --> result example
- 03_set_ruby_to_textframe
- 04_layer
- 05_character --> result example
- 06_paragraph --> result example
- 07_font --> result example
- 08_table/cell --> result example
- 09_graphic --> result example
- 10_pdf
- 11_find_and_change_text --> example
- find/change_text
- find/change_grep
- 12_export_selection_to_image
- 13_find_all_imgs_smaller_than_350dpi
- 14_export_table_to_Excel --> example
- 15_export_document_to_idml
- 16_InDesign_vs_ChatGPT --> example
- 17_xml --> example1, example2, test data