GithubHelp home page GithubHelp logo

lohriialo / illustrator-scripting-python Goto Github PK

View Code? Open in Web Editor NEW
126.0 9.0 24.0 455 KB

Scripting in Illustrator is used to automate a wide variety of repetitive task or as complex as an entire new feature

Python 100.00%
illustrator scripting python photoshop photoshop-script illustrator-scripts indesign-scripts adobe indesign photoshop-javascript

illustrator-scripting-python's Introduction

WIP (More code snippets coming soon....!)

Illustrator Scripting in Python

Scripting in Illustrator is used to automate repetitive tasks and are often used as a creative tool to streamline tasks that might be too time consuming to do manually. For example, you could write a script to generate a number of localized versions of a particular image or to gather information about the various color profiles used by a collection of images.

Illustrator COM & DOM

Illustrator can be scripted through COM(Component Object Model). Its DOM(Document Object Model) is the same when accessing it through either its own JavaScript engine or Python or any other scripting language it supports. The Illustrator DOM consists of a hierarchical representation of the Illustrator application, the documents used in it, and the components of the documents. The DOM allows you to programmatically access and manipulate the Artboard and its components. For example, through the DOM, you can create a new document, add a layer to an existing document, or change the background color of a layer. Most of the functionality available through the Illustrator user interface is available through the DOM.

But why Python?

Illustrator scripting officially supports JavaScript, AppleScript & VBScript. However, scripting in Python is also fairly easy if not easier if you're already comfortable with Python. You may have already heard that Python is gaining in popularity, but did you know it’s now the most popular introductory programming language in U.S. universities? Python is also cross platform just like JavaScript is and lately becoming one of the fastest growing programming language according to StackOverflow as of 2017 / as of 2019

Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike. Python’s readability makes it a great first programming language - it allows you to think like a programmer and not waste time understanding the mysterious syntax that other programming languages can require.

Getting Started

Python allows you to access COM and it's DOM with the help of a Python extensions like "pypiwin32" or "comtypes". Install these modules and you're ready to start scripting Illustrator in Python

  • pip install pypiwin32 or pip install comtypes

Hello World!

from win32com.client import GetActiveObject

app = GetActiveObject("Illustrator.Application")
docRef = app.Documents.Add()
rectRef = docRef.PathItems.Rectangle(700, 50, 100, 100)
areaTextRef = docRef.TextFrames.AreaText(rectRef)
areaTextRef.Contents = "Hello World!"

How to inspect scripting object properties?

There's not a straight forward way, you need to read the documentation to understand what properties/attributes are available for a scripting object, or possibly a COM browser. For example, I've extracted the Python scripting object reference for Illustrator CC 2018 at api_reference

Scripting on Mac?

Yes, scripting on Mac is also possible, see photoshop_mac_scripting for more details as a reference to getting started

Illustrator Scripting Resources

Also see

Contribution

If you've written a useful Illustrator Python script and wants to share with the world, please create a new issue with the file as an attachment to the issue.

When you submit a script, please try to include the following information at the start of your script

# script_file_name.py

# Created: 1st January 2019
__author__ = 'Your Name or Original Author Name'
__version__ = '1.0'

"""
A short description of what the script does
"""

"""
Instructions on how to use the script, if any
"""

illustrator-scripting-python's People

Contributors

lohriialo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

illustrator-scripting-python's Issues

Can't save file as PDF due to COM variant conversion error

Hello,

First off, thank you so much for working on this project and enabling us to do so much more with Illustrator outside of its Javascript engine. I am using it to create a script to assist with sticker sheet creation and other large format printing needs.

I have had issues when trying to send certain parameters to Illustrator through a method call. Most variable types work, but when I try to send an object, I get this error:

doc.SaveAs(infotech, saveOpts)
File "c:\Users\USERDIRECTORY\Documents\PythonLearning\IllustratorLibrary21.py", line 2017, in SaveAs
return self._oleobj_.InvokeTypes(1398161747, LCID, 1, (24, 0), ((8, 1), (12, 17)),SaveIn
TypeError: Objects of type 'type' can not be converted to a COM VARIANT`

"infotech" is a variable that I have my destination path saved to. This is how I build "saveOpts":

 saveOpts = ai.PDFSaveOptions
 saveOpts.Compatibility = 7
 saveOpts.GenerateThumbnails = False
 saveOpts.PreserveEditability = False

I cannot save the document to a PDF because I cannot send the PDFSaveOptions parameter without receiving this error. I have done my own research into the error and honestly a lot of it is over my head, but I believe I understand the basics of a COM environment and how Python may need to convert variables to different types in order to work with other software.

Some things I've found:
Any object or variable created via the illustrator-scripting-python API cannot be passed to Illustrator. For instance, to add a spot color swatch to the document and save it as a variable, you cannot use the API's method Swatches.Add(), as this will give you the same error I posted above. Instead, you must do App.ActiveDocument.Spots.Add(), then set the Name attribute of that spot, then loop through ActiveDocument.Swatches to find the name and save THAT result to a variable.

Example:

if swatchExists == False:

    perfCut = doc.Spots.Add()
    perfCut.Name = 'PerfCutContour'
    perfCut.ColorType = 2
    perfCut.Color.Black = 0
    perfCut.Color.Magenta = 0
    perfCut.Color.Cyan = 100
    perfCut.Color.Yellow = 100

    for c in doc.Swatches :
        if c.Name == 'PerfCutContour' :
            perfCutContour = c'

Originally I was trying to use Swatches.Add(), then set the "Spot" attribute of that swatch to the spot I've created in the example code above. Trying to set the Spot attribute never worked and only gave me the same conversion error that I posted at the top.

Other notes:

  • I can save the file using doc.Save() but the only option with that is an AI file. We need PDFs specifically because our print software won't run anything else.
  • I will update this post if I find solutions or any more information

Update 9/22/21

I was able to work around this problem by simply running a Javascript file from my Python script. I wanted to avoid JS as much as possible with this project but I think this solution is fine and has been working great for me in a production environment for almost 3 weeks. When ready to save, I use the code:

doc = app.ActiveDocument
destination = C:/Whatever/Filename/ToSaveTo.pdf
arguments = [doc, destination]
app.DoJavaScriptFile(jsSave, arguments)

As for the "jsSave" file, that is a .js file in the same directory as my script. The JS is as follows:

doc = arguments[0]
destination = arguments[1]

function saveAndClose(doc, dest) {
    var saveName = new File(dest);
    saveOpts = new PDFSaveOptions();
    saveOpts.compatibility = PDFCompatibility.ACROBAT8;
    saveOpts.generateThumbnails = false;
    saveOpts.preserveEditability = false;
    doc.saveAs(saveName, saveOpts);
    doc.close();
}

saveAndClose(doc, destination)

How to inspect AI object properties?

Hey there,
first of all let me thank you for sharing this, along with ID and PS this information is extremely valuable!

I'm still a novice with Python, so maybe I'm asking something obvious and probably unrelated to this, but basically how can I inspect AI object properties and methods?

Picking from your Hello World example:

app = GetActiveObject("Illustrator.Application")
docRef = app.Documents.Add()

Doing a basic dir returns methods from the win32com.client.GetActiveObject class, so how can I get the details of the app.Documents object for example?

I don't know much (nothing probably is more appropriate) about the whole .NET/COM structure, Googling around I found that is not something easy to achieve, so I was wondering also what is your workflow and dev setup for scripting.

Thank you!

Images and Colours

How would I go about adding images using this?
Also, when I attempt to add a fillColor (on a rectangle like you made in the hello world tutorial) I get the error: TypeError: Cannot put <win32com.gen_py.None.CMYKColor> in VARIANT would you be able to provide examples for these things?
Cheers

"Self" property explanation

Hey, I stumbled upon your repo and tried to use it in a project. (https://github.com/SaadBazaz/auto-corona-graphics-generator) However, I need help in Exporting.
I'm using the direct Illustrator COM but there's no reference on how to Export.

In the API you've provided as well (illustrator_2020.py), there's a function:
Document.Export( )
But it contains an argument called "self" which I don't understand.

Any guidance would be appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.