LoginSignup
3
3

More than 5 years have passed since last update.

InDesign・InDesignServerのJavaScript処理系にログファイル書き出し機能を実装する

Last updated at Posted at 2015-04-24

ExtendScriptのJS処理系には console.log() が存在しない。ExtendScriptToolkitへのコンソール出力には $.writeln() が使えるが、InDesignServerの標準出力へ出力するには alert() を使う必要がある。手癖でついconsole.log()と打ちたくなってしまう。

通常版のInDesignでスクリプトの開発をしてからサーバ版へ持っていくことが多いため、出力部分の関数が違うのは地味に面倒だ(通常版で一々alert()すると使えたものではない)。また、動作を監視するためにログを取りたいが、InDesignServerにはログの書き出し機能がついていない。

そこで、ログファイル書き出し機能を付けた console.log()をオレオレ実装した。ファイル先頭のフラグをいじるだけで、通常版InDesignとサーバ版InDesign用の挙動を切り替えることができる。
(CoffeeScriptで開発しているので、適宜読み替えてください。)

console.log の実装


# サーバモード
serverMode = true
# サイレントモード
silentMode = false

console = {}
console.log = (msg) ->
  if !serverMode and !silentMode
    # 開発用の挙動
    $.writeln msg
  else if serverMode and !silentMode
    # サーバ用の挙動
    alert msg
  if serverMode
    # ログファイルの保存先を作成してキャッシュ
    @logfile = @logfile || new File '/path/to/logfile.txt'
    # 追記モードでログファイルを開いて閉じる
    @logfile.open 'a'
    @logfile.writeln msg
    @logfile.close()

使い方

console.log  "\n\n\n===#{new Date()}========================================================"
console.log "API version: #{app.scriptPreferences.version}"
console.log "InDesign version: #{app.version}"
console.log "OS: #{$.os}"
console.log "Locale: #{$.locale}\n\n"

ExtendScriptのグローバルな機能しか使用していない為、InDesign以外の他のソフトでも動作するかもしれない。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3