Archive for the 'Skim' Category

12/10 Skimで、現在表示中のページを別ファイルにPDF形式で書き出す

オープンソースのPDFビューワー「Skim」で、現在表示中のPDFのページを別のPDFに書き出すAppleScriptです。

最初は、現在表示中のページだけを残して、他のページを「削除する」AppleScriptを書いていたのですが、Skimではpageを削除できないことにあとから気付きました。

そこで、急遽「ページごとに別ファイルに書き出す」Scriptを機能ダウンさせて現在のページのみPDFで別ファイルに書き出すように変更。

スクリプト名:Skimで、現在表示中のページを別ファイルにPDF形式で書き出す
tell application “Skim”
  tell document 1
    set aBounds to selection bounds
    
tell current page
      set grabData to grab for aBounds as PDF
    end tell
  end tell
end tell

set newAlias to choose file name
write_to_file(grabData, newAlias, false) of me

–ファイルの追記ルーチン「write_to_file」
–追記データ、追記対象ファイル、boolean(trueで追記)
on write_to_file(this_data, target_file, append_data)
  try
    set the target_file to the target_file as text
    
set the open_target_file to open for access file target_file with write permission
    
if append_data is false then set eof of the open_target_file to 0
    
write this_data to the open_target_file starting at eof
    
close access the open_target_file
    
return true
  on error error_message
    try
      close access file target_file
    end try
    
return error_message
  end try
end write_to_file

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

こちらが、動かないほうのバージョンです。コンパイル(構文確認)も通るのですが、アプリケーション側がサポートしていないオブジェクトとコマンドの組み合わせだと動かないという、いい見本です。

スクリプト名:Skimで、現在表示中のページ以外をすべて削除する(ダメ)
–そもそも、pageをdeleteすることができなかった
–→ 現在のページだけをExportする方向で書き直し

tell application “Skim”
  tell document 1
    set curP to index of current page –現在表示中のページ番号(index)を取得
    
set totalP to count every page –全ページ数を取得
    
    
    
if totalP = 1 then
      display dialog “1ページしか存在しない書類は処理対象外です” buttons {“OK”} default button 1 with icon 1
      
return
    end if
    
    
if curP = 1 then
      –現在表示中のページが1ページ目だった場合の処理
      
delete (pages 2 thru -1)
      
    else if curP = totalP then
      –現在表示中のページが最終ページだった場合の処理
      
delete (pages 1 thru -2)
      
    else
      –その他 通常処理
      
delete (pages 1 thru (curP - 1))
      
delete (pages (curP + 1) thru -1)
      
    end if
    
  end tell
end tell

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

09/25 指定フォルダ内のPDFの1ページ目のみ取り出して別フォルダに保存 v2

指定フォルダ内のPDFの1ページ目のみ取り出して、別フォルダに1ページ目だけのPDFを保存するAppleScriptです。

オープンソースなのにAppleScript対応度のきわめて高いPDFビューワー「Skim」をコントロールして、PDFの指定ページ上にコンテンツが存在しないかのチェック、およびページ切り出しを行っています。

ここでいうPDFは、ドキュメントスキャナーで読み込んだPDFではなく、何らかのアプリケーションから書き出したPDF(文字情報が選択できる)を意図しています。

大量のHTMLを印刷する際に、1枚ずつプリンタに送ると時間がかかるため、CLIのHTMLレンダラー(wkpdf)にかけて大量のPDFを生成。この大量のPDFを1つのドキュメントにまとめてプリンタに送れば、印刷時間の圧倒的な短縮が可能になるのではないか、という話を実現する過程で作成したものです。

だいたいは思い通りに行ったのですが、誤算がひとつ。HTMLレンダラーが1ページ分のコンテンツも2ページにまたがってPDF出力(しかも2ページ目は空白)することが多く……単純に1ページ目のみ抽出しようとしても、今度は本当にHTMLの分量が多くて複数ページにまたがってレンダリングされている場合もあったために、単純作業で2ページ以降を捨てるのでは困るということになりました。

そこで、ほとんど文字情報から構成されるコンテンツであったため、2ページ目に何か文字の情報がないかSkimをコントロールしてAppleScriptでチェックし、存在しなければ1ページ目のみ抽出。存在している場合には抽出処理を行わないようにしました。

人間が目で見て確認しなくてよいので、作業の飛躍的な高速化を実現できました。もちろん、印刷もあっという間。

もし、文字情報が入っていない画像系のPDFが対象だったら……解像度を指定してPhotoshopで読み込み、ヒストグラムを取得するなどして情報の有無を判定すると楽ができてよいでしょう。

スクリプト名:指定フォルダ内のPDFの1ページ目のみ取り出して別フォルダに保存 v2
set inFol to choose folder with prompt “処理対象のPDFが入っているフォルダを指定”

set dtPath to choose folder with prompt “出力先フォルダを選択”
set dtPathStr to dtPath as string

tell application “Finder”
  tell folder inFol
    set aFileList to (every file whose name ends with “.pdf”) as alias list
  end tell
end tell

–それぞれのPDFの1ページ目のみ抽出
repeat with i in aFileList
  trimPDF(i, dtPathStr) of me
end repeat

–PDFの最初のページのみトリミング
on trimPDF(aFile, outFol)
  tell application “Skim”
    close every document
    
    
open aFile
    
    
tell document 1
      set cCount to count every page
      
set cDigit to length of (cCount as string)
      
      
set docName to name
      
      
tell page 1
        set bList to bounds
      end tell
      
      
if cCount > 1 then
        tell page 2
          set aProp to properties
          
set aText to first item of text of aProp
        end tell
        
        
if aText is not equal to “” then
          
          
display dialog “書類「” & docName & “」は、2ページ目に実際に内容が存在するため、2ページ目を削除せずそのままコピーします。”
          
          
tell application “Finder”
            set target_file to outFol & docName
            
            
duplicate aFile to file target_file
            
          end tell
          
          
return –ここが重要
          
        end if
      end if
    end tell
    
    
tell document 1
      tell page 1
        set anImage to grab for bList –type “PDF”
      end tell
    end tell
    
    
set target_file to outFol & docName
    
write_to_file(anImage, target_file, false) of me
    
    
close every document
    
  end tell
end trimPDF

on makeFN(aNum, aDigit)
  set aText to “00000000000″ & (aNum as text)
  
set aLen to length of aText
  
set aRes to text (aLen - aDigit) thru -1 of aText
  
return aRes
end makeFN

–ファイルの追記ルーチン「write_to_file」
–追記データ、追記対象ファイル、boolean(trueで追記)
on write_to_file(this_data, target_file, append_data)
  try
    set the target_file to the target_file as text
    
set the open_target_file to open for access file target_file with write permission
    
if append_data is false then set eof of the open_target_file to 0
    
write this_data to the open_target_file starting at eof
    
close access the open_target_file
    
return true
  on error error_message
    try
      close access file target_file
    end try
    
return error_message
  end try
end write_to_file

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

10/02 SkimでPDFをページごとに分割する

オープンソースのPDFビューワー「Skim」でPDF書類をページごとに分割するAppleScriptです。

たいていオープンソースのアプリケーションといえばAppleScriptへの対応はなかったり中途半端だったり、勘違いした実装を行っているようなもの(FireFoxあたりがその代表例)が普通ですが、このSkimは例外中の例外。バージョンアップとともにAppleScriptの対応度がアップするなど、なかなか見逃せないアプリケーションです。

複数のPDFを1つの書類にまとめる機能を持つものは多々あります。しかし、探してみると1つのPDF書類をページごとに分解する機能を持つものはそれほど見かけません。

調べてみたら、AdobeのAcrobat Reader、Smile SoftwareのPDF Pen、そしてSkimぐらい(以前はPDFtoolkitが使えたのですが、Mac OS X 10.4までしか対応していなかったので)。そのうち、AppleScriptに(まともに)対応しているのがPDFpenとSkim。フリーで入手できるものでいえばSkimしかありません。

そんなわけで、SkimをAppleScriptから制御してページごとにPDFを分解させてみました。

ちなみに、PDFpenには標準でPDFをページ単位に分解するAppleScriptが添付されており、PDFpenのScriptメニューから実行できるようになっていますが、同じデータを処理させてみたところSkimの方が高速に処理できました。

スクリプト名:SkimでPDFをページごとに分割する
set dtPath to path to desktop from user domain
set dtPathStr to dtPath as string

tell application “Skim”
  tell document 1
    set cCount to count every page
    
set cDigit to length of (cCount as string)
    
    
set docName to name
    
    
tell page 1
      set bList to bounds
    end tell
  end tell
  
  
repeat with i from 1 to cCount
    tell document 1
      tell page i
        set anImage to grab for bList –type “PDF”
      end tell
    end tell
    
    
set target_file to dtPathStr & makeFN(i, cDigit) of me & “_” & docName
    
write_to_file(anImage, target_file, false) of me
    
  end repeat
  
end tell

on makeFN(aNum, aDigit)
  set aText to “00000000000″ & (aNum as text)
  
set aLen to length of aText
  
set aRes to text (aLen - aDigit) thru -1 of aText
  
return aRes
end makeFN

–ファイルの追記ルーチン「write_to_file」
–追記データ、追記対象ファイル、boolean(trueで追記)
on write_to_file(this_data, target_file, append_data)
  try
    set the target_file to the target_file as text
    
set the open_target_file to open for access file target_file with write permission
    
if append_data is false then set eof of the open_target_file to 0
    
write this_data to the open_target_file starting at eof
    
close access the open_target_file
    
return true
  on error error_message
    try
      close access file target_file
    end try
    
return error_message
  end try
end write_to_file

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

03/30 Skim 1.3.5でsave命令のオプションが変更に

オープンソースのPDFビューワーなのになぜかAppleScript系の機能が充実している「Skim」

skim0.jpg

バージョン1.3.4から1.3.5へのアップデート時に、例によってAppleScript用語辞書をHTMLに書き出してdiff(Apple謹製のFileMerge。Xcode Toolsに入っている)で差分チェックを行ってみたところ、saveコマンドのオプションに変更が行われたことが明らかになりました。

実際にサンプルを書いて本当に使えるかどうかチェックしてみないと「使えるようになった」と結論づけることはできませんが、dviファイルといえば……TeX(テフ)ですか(汗) 使ったことはありませんが、昔……工学部の友人たちがこれでレポートを書かされて苦労していました。今はWordなどで提出できるようですが…………

skim1.jpg
▲FileMergeで差分チェックを行った結果

skim2.jpg
▲Skim 1.3.5のAppleScript用語辞書

02/05 Skimでコメントつき注釈(note)の情報を取得する

Skimで、GUI側からコメント(extended text)つきの注釈(note)を作成し、その情報を取得してみました。

skim8.jpg

スクリプト名:Skimでコメントつき注釈(note)の情報を取得する
tell application Skim
  tell document 1
    tell note 1
      properties
    end tell
  end tell
end tell

> {type:anchored note, extended text:”なんてこったい!”, icon:comment icon, page:page 1 of document “InDesign CS3 スクリプティング ガイド AS.pdf” of application “Skim”, class:note, bounds:{453, 740, 469, 724}, text:”ほげほげーー”, color:{64639, 65535, 26043, 65535}}

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

02/05 Skimで注釈(note)を削除する

Skimで現在オープン中のPDF(document 1)の、指定ページ内の注釈(note)を削除します。

スクリプト名:Skimで注釈(note)を削除する
tell application Skim
  tell document 1
    tell page 1
      delete every note
    end tell
  end tell
end tell

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

02/05 Skimで注釈(note)を新規作成する

Skimで、現在オープン中のPDF(document 1)に対して、注釈(note)を作成します。意外とあっさりできてしまい、拍子抜けするほどです。

skim7.jpg

スクリプト名:Skimで注釈(note)を新規作成する
tell application Skim
  tell document 1
    tell page 1
      make new note with properties {type:text note, dash pattern:{}, font color:{0, 0, 0, 65535}, font size:36, bounds:{277, 573, 480, 532}, line style:solid line, font name:”LucidaGrande“, class:note, line width:1.0, text:”注釈だよー2“, color:{0, 65535, 26043, 65535}}
    end tell
  end tell
end tell
> note 1 of page 1 of document “InDesign CS3 スクリプティング ガイド AS.pdf” of application “Skim”

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

02/05 Skimで注釈(note)の情報を取得する

Skimで、PDF上にユーザーが作成した注釈(note)の情報を取得します。

skim6.jpg

スクリプト名:Skimで注釈(note)の情報を取得する
tell application Skim
  tell document 1
    tell note 1
      properties
    end tell
  end tell
end tell

> {type:text note, dash pattern:{}, font color:{0, 0, 0, 65535}, font size:36, bounds:{277, 573, 480, 532}, line style:solid line, page:page 11 of document “InDesign CS3 スクリプティング ガイド AS.pdf” of application “Skim”, font name:”LucidaGrande”, class:note, line width:1.0, text:”注釈だよー”, color:{64639, 65535, 26043, 65535}}

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

02/05 Skimで選択中の範囲をファイルに書き出す

Skimの選択ツールで選択している範囲(selection bounds)をPDFに書き出すAppleScriptです。

たとえば、AdobeがInDesignに添付している「InDesign CS3スクリプティング ガイドAS」をSkimでオープンし、選択ツールで範囲を選択し……本Scriptを実行すると、選択範囲が新規作成されたPDFファイルに書き出されます。

skim3.jpg

skim4.jpg

スクリプト名:Skimで選択中の範囲をファイルに書き出す
tell application Skim
  tell document 1
    set aBounds to selection bounds
    
tell page 4
      set grabData to grab for aBounds astype PDF
    end tell
  end tell
end tell

set newAlias to choose file name
write_to_file(grabData, newAlias, false) of me

ファイルの追記ルーチン「write_to_file」
追記データ、追記対象ファイル、boolean(trueで追記)
on write_to_file(this_data, target_file, append_data)
  try
    set the target_file to the target_file as text
    
set the open_target_file to open for access file target_file with write permission
    
if append_data is false then set eof of the open_target_file to 0
    
write this_data to the open_target_file starting at eof
    
close access the open_target_file
    
return true
  on error error_message
    try
      close access file target_file
    end try
    
return error_message
  end try
end write_to_file

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

02/05 Skimでアプリケーション自身の情報を取得する

Skimのアプリケーション自体の設定情報を取得します。Preferencesの画面とかなり一致している部分があり、気合いが入っていることが伺われます。

skim5.jpg

スクリプト名:Skimでアプリケーション自身の情報を取得する
tell application Skim
  properties
end tell
> {default note line widths:{text note line width:1.0, line note line width:2.0, freehand note line width:2.0, circle note line width:2.0, box note line width:2.0}, default note dash patterns:{text note dash pattern:{}, line note dash pattern:{}, freehand note dash pattern:{}, circle note dash pattern:{}, box note dash pattern:{}}, default note line styles:{text note line style:solid line, line note line style:solid line, freehand note line style:solid line, circle note line style:solid line, box note line style:solid line}, frontmost:false, default note icon type:note icon, default full screen view settings:{display box:media box, display mode:single page}, version:”1.2.1″, name:”Skim”, default note colors:{highlight note color:{65535, 65535, 0, 65535}, line note color:{0, 0, 65535, 65535}, circle note interior color:{0, 0, 0, 0}, circle note color:{65535, 0, 0, 65535}, box note color:{0, 65535, 0, 65535}, underline note color:{0, 32768, 0, 65535}, strike out note color:{65535, 0, 0, 65535}, text note color:{65535, 65535, 32768, 65535}, freehand note color:{65535, 0, 0, 65535}, text note font color:{0, 0, 0, 65535}, box note interior color:{0, 0, 0, 0}, anchored note color:{65535, 65535, 32768, 65535}}, default note start line style:no line ending, class:application, default view settings:{display box:crop box, auto scales:false, book:false, scale factor:1.0, display mode:single page continuous, displays page breaks:true}, default note end line style:open arrow line ending, background color:{32769, 32769, 32769, 65535}, full screen background color:{16384, 16384, 16384, 65535}}

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

02/05 SkimでPDFの指定ページ内の情報を取得する

Skimで、PDFの指定ページ内の情報を取得します。本文テキストが取得できるのが素敵です。

skim11.jpg

スクリプト名:SkimでPDFの指定ページ内の情報を取得する
tell application Skim
  tell document 1
    tell page 1
      properties
    end tell
  end tell
end tell

(*
{text:”Device Driver for
Mac OS X
Mac OS X用のデバイスドライバをUSB Mouseのドライバを例に
紹介します。
稲田元彦
inADA
2003.08.26
修正2003.12.05
Copyright (C) 2003 inADA.
All rights reserved.”, content bounds:{69, 615, 533, 70}, rotation:0, label:”1″, index:1, class:page, media bounds:{0, 842, 595, 0}, bounds:{0, 842, 595, 0}}
*)

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

02/05 SkimでPDFの各種情報を取得する

オープンソースのソフトウェアといえば、AppleScript非対応とかAppleScript対応機能の実装方法が間違っているとかAppleScript対応機能が機能していないといった話のオンパレードですが、例外のソフトウェアがひとつ。オープンソースのPDFビューワー「Skim」がそれです。

PDFビューワーといえば、Mac OS X標準装備の「Preview.app」があるので「何をいまさら」という感もないわけではありませんでしたが……ダウンロードしてびっくり、AppleScript対応機能がひじょうに充実。

ある意味、Skimは「最強のPDFビューワー」といっても過言ではありません。ページ数が分るとかそういう話は当然で、なんとPDF本文のテキストが取得できてしまいます。しかも、ページを指定して該当ページ上のテキストだけ取得するといった処理が可能です。

skim1.jpg

スクリプト名:SkimでPDFの各種情報を取得する
tell application Skim
  tell document 1
    properties
  end tell
end tell

> {modified:false, info:{producer:”Mac OS X 10.3.1 Quartz PDFContext”, encrypted:false, creation date:date “2003年 12月 6日 土曜日 2:55:13 PM”, modification date:date “2003年 12月 6日 土曜日 2:59:19 PM”, creator:”AppleWorks”, file name:”MacOSX_Driver1.pdf”, physical size:2.985984E+6, tags:{}, page count:15, page size:”21.0 x 29.7 cm (21.0 x 29.7 cm)”, allows printing:true, allows copying:true, rating:0.0, logical size:2.983359E+6, version:”1.4″, file size:”2.8 MB (2,983,359 bytes)”}, path:”/Users/maro/Documents/Develop/ドキュメント類/MacOSX_Driver1.pdf”, selection:{}, name:”MacOSX_Driver1.pdf”, text:”Device Driver for Mac OS X (中略) Copyright (C) 2003 inADA. All rights reserved.”, view settings:{display box:crop box, auto scales:false, book:false, scale factor:0.707115232944, display mode:single page continuous, displays page breaks:true}, active note:missing value, file:file “Cherry:Users:maro:Documents:Develop:ドキュメント類:MacOSX_Driver1.pdf”, class:document, current page:page 1 of document “MacOSX_Driver1.pdf” of application “Skim”, selection bounds:{0, 0, 0, 0}, selection page:missing value}

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