Archive for the 'Preview.app' Category

02/08 Preview.appで最低限のScript命令実行機能をイネーブルに

たまたま、Mac OS X Hintsに掲載されているのを見つけ、半信半疑で実行してみたら……open、print、quit、closeなどの基礎的な命令をPreview.appが受け付けるようになりました。

なんでも、AppleのAppleScript製品担当のSal Soghoianがコメント欄にそのような投稿をしたのだとか(汗) そもそも、なぜそのようなゲリラ的なことをやるのか? Mac OS Xの製品版のPreview.appにちゃんとしたScript対応機能を持たせればそれでよいのではないか?

疑問は尽きないのですが、とりあえずSal Soghoianが書いた内容であれば、とくに転載しても問題はないでしょう。

# 注意:本Scriptを実行する前にPreview.appのファイルを必ずバックアップしておいてください

処理内容自体は、Info.plistにエントリを追加しているだけなので、Xcode Toolsに入っている「Property List Editor」を使ったほうが安全と思われます。

スクリプト名:Preview.appに最低限のScript命令実行機能をイネーブルに
try
  tell application Finder
    set the Preview_app to (application file id com.apple.Preview“) as alias
  end tell
  
set the plist_filepath to the quoted form of ((POSIX path of the Preview_app) & Contents/Info“)
  
do shell script defaults write & the plist_filepath & space & NSAppleScriptEnabled -bool YES
end try

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

10/21 PDFのページ数を数えるv3

PDFのページ数を数えるScriptを久しぶりに動かしてみたら、Mac OS X 10.5.5上で期待どおりの動作を行わなかったので、修正を行ってみました。

画面上のウィンドウの文字やメニューの状態を検出して処理を行うUI Element Scripting(GUI Scripting)を用いた場合に、アプリケーションの画面構成が変更になると当初の想定どおりの動作が期待できなくなってしまう、その実例といえるでしょうか。

preview_app.jpg

Mac OS X 10.4と10.5のPreview.appでは、PDFのページ数表示が異なります。Mac OS X 10.5では当初は異なる表示を行っていたのですが、途中で変更になったようです。

とりあえず、極力バージョンに依存しないよう配慮して修正してみました。

スクリプト名:PDFのページ数を数えるv3
set a to choose file
set pNumStr to getPDFPages(a) of me
set pNum to pNumStr as number

指定PDFのページ数をPreview.appを使って数える。要・GUI Scriptingオン
Mac OS X 10.4および10.5対応
ただし、日本語環境であることが動作の前提条件
on getPDFPages(aFile)
  tell application Preview to open aFile
  
activate application Preview
  
tell application System Events
    tell process プレビュー
      tell window 1
        set aTitle to title
      end tell
      
keystroke w using command down
    end tell
  end tell
  
  
このあたりを変更した(v3) 10.4 & 10.5で共通ルール
  
set pRes to trimStrings(aTitle, “, “) of me
  
set pRes to repChar(pRes, ページ“, “”) of me
  
if pRes contains / then
    set aPos to offset of / in pRes
    
set pRes to text (aPos + 1) thru -1 of pRes
  end if
  
  
return pRes
end getPDFPages

任意の文字列から指定開始子、指定終了子でトリミングした文字列を取り出す
あんまり出来がよくないのでちょっと直した
on trimStrings(aString, fromStr, endStr)
  set fromLen to length of fromStr
  
set eLen to length of endStr
  
  
set sPos to offset of fromStr in aString
  
set body1 to text (sPos + fromLen) thru -1 of aString
  
set ePos to offset of endStr in body1
  
set body2 to text 1 thru (ePos - 1) of body1
  
  
return body2
end trimStrings

文字置換
on repChar(origText, targChar, repChar)
  set origText to origText as string
  
set targChar to targChar as string
  
set repChar to repChar as string
  
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to targChar
  
set tmpList to text items of origText
  
set AppleScript’s text item delimiters to repChar
  
set retText to tmpList as string
  
set AppleScript’s text item delimiters to curDelim
  
return retText
end repChar

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

07/15 PDFのページ数を数える

指定のPDFのページ数を数えます。この手の処理を行うには、GhostScriptやらpdftkをインストールして、というのがお約束のやり方です。また、Acrobat Professionalを使ってページ数を数える、というのも一般的な方法でしょう(ふつうのAcrobat Readerは、AppleScript用語辞書が見えるのにAppleScriptからのコントロールを拒絶します)。

Acrobat Professionalは高いし、GhostScriptやらpdftkがあまねくすべてのマシンにインストールされているわけではないため、もうちょっと違うアプローチを試してみたのが本Scriptです。

なんと、Preview.appでオープンしてタイトルバーの文字列を取得して、文字を切り抜きしてページ数を取得するとかいう、えらく強引な方法でやってみました。10.4.11と10.5.4で動作確認してあります。こんな方法がいいとは思わないのですが、AppleScriptからPDFのページ数を調査する手段が限られているため、「ないよりはマシ」といったレベルのScriptです。

ただし、GUI Scriptingがオンになっていないと動作しませんし、日本語環境以外では動きません。

スクリプト名:PDFのページ数を数える v2
set a to choose file
set pNumStr to getPDFPages(a) of me
set pNum to pNumStr as number

指定PDFのページ数をPreview.appを使って数える。要・GUI Scriptingオン
Mac OS X 10.4および10.5対応
ただし、日本語環境であることが動作の前提条件
on getPDFPages(aFile)
  tell application Preview to open aFile
  
activate application Preview
  
tell application System Events
    tell process プレビュー
      tell window 1
        set aTitle to title
      end tell
      
keystroke w using command down
    end tell
  end tell
  
  
set v2 to system attribute sys2
  
if v2 = 4 then
    Mac OS X 10.4.xの場合
    
set pRes to trimStrings(aTitle, “, ページ)“) of me
  else if v2 = 5 then
    Mac OS X 10.5.xの場合
    
set pRes to trimStrings(aTitle, /“, “) of me
  end if
  
return pRes
end getPDFPages

任意の文字列から指定開始子、指定終了子でトリミングした文字列を取り出す
あんまり出来がよくないのでちょっと直した
on trimStrings(aString, fromStr, endStr)
  set fromLen to length of fromStr
  
set eLen to length of endStr
  
  
set sPos to offset of fromStr in aString
  
set body1 to text (sPos + fromLen) thru -1 of aString
  
set ePos to offset of endStr in body1
  
set body2 to text 1 thru (ePos - 1) of body1
  
  
return body2
end trimStrings

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に