VBScript

Adobe Illustratorを操作するVBScript

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プロパティも読み取り専用であるため、操作不能)

そのため、上記コードでは、

  1. Illustratorが起動しているかどうかを判別。
  2. 起動していない場合は「プログラム」にあるショートカットファイルからIllustrator.exeのパスを取得し、WshShell.Runメソッドで起動。
  3. Illustrator.Applicationオブジェクトを取得。

といった処理を行っています。

実行ファイルのパスを取得する処理がプログラムフォルダに依存しているため、正確性には欠けるのですが、一応は目的を達成できるので、これで良しとしています。

Illustratorスクリプトの詳細については、下記サイトからリファレンスをダウンロードできるので、そちらをご参照ください。

2017年5月の人気記事前のページ

Illustratorに登録されたPDFプリセットを列挙するVBScript次のページ

関連記事

  1. Windows関連

    Windows 8を従来のスタイルに変更するスクリプト

    2012/3/2 追記:下記情報はWindows Develope…

  2. Office関連

    古い形式のWordテンプレートを新しい形式に一括変換するVBScript

    古い形式のWordテンプレート(dot)を新しい形式(dotx,dot…

  3. VBScript

    Windows Updateの更新履歴をCSV(UTF-8)で保存するVBScript

    以前書いたスクリプトが出てきました。Windows Updateの…

  4. Windows 10

    Microsoft Edgeを操作するVBScript

    「Microsoft Edgeを操作するVBAマクロ(WebDrive…

  5. Office関連

    「2014年12月のWindows Update以降コマンドボタンが使えなくなった」トラブルへのFi…

    当ブログでも「KB2553154の更新プログラムをアンインストールする…

  6. VBScript

    クリップボードに文字列をコピーする

    2012/4/3 追記:関連記事として「clipコマンドを利用してクリ…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP