How to kick off ExtendScript JSX script from Powershell

0

I want to be able to execute an Adobe Illustrator ExtendScript via Windows Powershell. I believe this should be possible due to this answer that describes using VB via COM.

This is my Powershell script:

$illustratorRef = New-Object -ComObject Illustrator.Application
$conversionScript = New-Object -ComObject Scripting.FileSystemObject
$scriptFile = $conversionScript.OpenTextFile("C:\ws\ArtConversion\alert-test.jsx")
$fileContents = $scriptFile.ReadAll()
$scriptFile.Close()

$fileToRun = $fileContents + "main(arguments)"

$args = "line1", "line2"

$illustratorRef.DoJavaScript($fileToRun, $args, 1)

Here is the alert-test.jsx script:

function main(argv) {
    alert('message: ' + argv[0]);
    return argv[0];
}

Running the Powershell script opens Illustrator, but throws the following error upon encountering $illustratorRef.DoJavaScript:

Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))

I am using Adobe Illustrator 2019 CC (64bit) and Powershell 5.1.16299.666

powershell
com
adobe-illustrator
extendscript
asked on Stack Overflow Oct 25, 2018 by Sam Schneider • edited Oct 25, 2018 by Sam Schneider

1 Answer

1

I achieved my goal, but wasn't able to do it 100% with Powershell.

The 2017 Adobe Illustrator Scripting Guide contains this statement on page 22:

In VBScript, there are several ways to create an instance of Illustrator.

When referring to JavaScript however, it says:

Information on launching Illustrator from JavaScript is beyond the scope of this guide.

I couldn't find any official documentation on how to programmatically start Illustrator on Windows using other languages besides VB, so I ended up letting my Powershell script handle the heavy lifting of directory traversal and logging, while having it open Illustrator by means of a Visual Basic script.

The call from Powershell into VB looks like this:

$convertFile = "cmd /C cscript .\run-illustrator-conversion.vbs $arg1, $arg2"
$output = Invoke-Expression $convertFile

The VB script ended up looking like this:

Dim appRef
Dim javaScriptFile
Dim argsArr()

Dim fsObj : Set fsObj = CreateObject("Scripting.FileSystemObject")
Dim jsxFile : Set jsxFile = fsObj.OpenTextFile(".\script-to-run.jsx", 1, False)
Dim fileContents : fileContents = jsxFile.ReadAll
jsxFile.Close
Set jsxFile = Nothing
Set fsObj = Nothing

javascriptFile = fileContents & "main(arguments);"

Set appRef = CreateObject("Illustrator.Application.CS5")
ReDim argsArr(Wscript.Arguments.length - 1)

For i = 0 To Wscript.Arguments.length - 1
    argsArr(i) = Wscript.Arguments(i)
Next

Wscript.Echo appRef.DoJavaScript(javascriptFile, argsArr, 1)

Note: Check scripting guide to get correct string for your version of Illustrator.

answered on Stack Overflow Nov 2, 2018 by Sam Schneider

User contributions licensed under CC BY-SA 3.0