Skip to content

mlove4u/InDesign-Automation-Python-Mac-Appscript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

Some example scripts for InDesign automation control on Mac.

About appscript

Keyword conversion

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. (※)

※example: class--> class_ , properties--> properties_

Usage

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

Add new object

# 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

Get type/class of an object

doc.class_() # k.document
first_tf.class_() # k.text_frame

Method with keyword

# same to python keyword arguments
doc.close(saving=k.ask, saving_in="path/to/file.indd")

Get value

doc.name()
first_tf.contents()

or

doc.name.get()
first_tf.contents.get()

Set value

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]
    })

Get item

by name

tfs["abc"] # return the 1st text frame whose name is "abc"
# javascript: tfs.itemByName("abc")

by id

tfs.ID(1234) # return text frame whose ID is 1234
# javascript: tfs.itemByID(1234)

by range

characters[1:3] # note: include the 3rd item !!!!!
# javascript: characters.itemByRange(0, 2)

Performance

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)

Sample code

About

InDesign Automation with Python in Mac

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages