Acrobatと同様にタイプライブラリが用意されているため、VBAやVBSからIllustratorを操作することができます。
今回はIllustratorを操作して新規ドキュメントを作成、文字を追加するスクリプトを紹介します。
Option Explicit Sample Public Sub Sample() 'Illustratorのオートメーションテスト Dim appAi Dim doc Set appAi = GetIllustratorObject() If appAi Is Nothing Then MsgBox "Illustrator.Applicationオブジェクトを取得できませんでした。", vbExclamation + vbSystemModal Exit Sub End If With appAi Set doc = .Documents.Add With doc.TextFrames.Add .Top = 300 .Left = 50 .TextRange.CharacterAttributes.Size = 28 .Contents = "Name:" & appAi.Name & vbNewLine & _ "Version:" & appAi.Version & vbNewLine & _ "Build Number:" & appAi.BuildNumber & vbNewLine & _ "Locale:" & appAi.Locale End With End With End Sub Private Function GetIllustratorObject() 'Illustrator.Applicationオブジェクトを取得 Dim obj Dim ai_path Set obj = Nothing: ai_path = "" '初期化 On Error Resume Next Set obj = GetObject(, "Illustrator.Application") On Error GoTo 0 'Illustratorが起動していない場合はRunメソッドで起動 If obj Is Nothing Then ai_path = GetIllustratorExePath() With CreateObject("Scripting.FileSystemObject") If .FileExists(ai_path) = True Then CreateObject("WScript.Shell").Run """" & ai_path & """", 1, False WScript.Sleep 6000 '起動待ち On Error Resume Next Set obj = GetObject(, "Illustrator.Application") On Error GoTo 0 End If End With End If Set GetIllustratorObject = obj End Function Private Function GetIllustratorExePath() 'Illustrator.exeのパスを取得 Dim itm Dim ret Const CSIDL_COMMON_PROGRAMS = 23 '[プログラム]にあるショートカットファイルからIllustrator.exeのパスを取得 With CreateObject("Shell.Application").Namespace(CSIDL_COMMON_PROGRAMS) For Each itm In .Items If InStr(LCase(itm.Name), "illustrator") Then With CreateObject("WScript.Shell").CreateShortcut(itm.Path) ret = .TargetPath End With Exit For End If Next End With GetIllustratorExePath = ret End Function
上記コードを見れば分かる通り、Illustrator.Applicationオブジェクトの取得部分の処理が冗長になっています。
通常であれば
Set appAi = CreateObject("Illustrator.Application")
と、WordやExcelのようにすれば問題無いはずですが、Illustratorの場合はなぜか“Illustratorが起動している状態”でないと操作が上手くいきません。
(ウィンドウが表示されず、また、Visibleプロパティも読み取り専用であるため、操作不能)
- VBA – Illustrator.Application not visible?
- https://forums.adobe.com/thread/950527
- How can I start Illustrator visible?
- https://forums.adobe.com/thread/716799
そのため、上記コードでは、
- Illustratorが起動しているかどうかを判別。
- 起動していない場合は「プログラム」にあるショートカットファイルからIllustrator.exeのパスを取得し、WshShell.Runメソッドで起動。
- Illustrator.Applicationオブジェクトを取得。
といった処理を行っています。
実行ファイルのパスを取得する処理がプログラムフォルダに依存しているため、正確性には欠けるのですが、一応は目的を達成できるので、これで良しとしています。
Illustratorスクリプトの詳細については、下記サイトからリファレンスをダウンロードできるので、そちらをご参照ください。
- Illustrator Scripting | Adobe Developer Connection
- http://www.adobe.com/devnet/illustrator/scripting.html
- Illustrator Scripting | Adobe Developer Connection [ADC]
- http://www.adobe.com/jp/devnet/illustrator/scripting.html
この記事へのコメントはありません。