#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#メニューバーの● ■ ▶︎ の右三角をクリックして実行してください↑
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()
###################################
#####設定項目
###################################
##収集したデータの保存ファイル名
set strFileName to "PDFフォーム集計データ.txt" as text
###################################
#####入力フォルダ
###################################
###ダイアログのデフォルト
tell application "Finder"
set aliasPathToMe to path to me as alias
set aliasContainerPath to container of aliasPathToMe as alias
#####回収したPDFフォームを集めてあるフォルダ名
set aliasDefaultLocation to (folder "回収PDFサンプル" of folder aliasContainerPath) as alias
end tell
#####空のテキストファイルを作っておく
tell application "Finder"
try
make new file at aliasContainerPath with properties {name:(strFileName)}
end try
end tell
##################################################
#####回収PDFのパス関連
##################################################
###ダイアログテキスト
set strPromptText to "回収したPDFフォルダを選んでください"
try
set listResponse to (choose folder strPromptText with prompt strPromptText default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents) as list
on error
log "エラーしました"
return
end try
###フォルダーのパス関連
set aliasDirPath to (item 1 of listResponse) as alias
set strDirPath to POSIX path of aliasDirPath as text
set ocidDirPathStr to refMe's NSString's stringWithString:strDirPath
set ocidDirPath to ocidDirPathStr's stringByStandardizingPath
set ocidDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidDirPath isDirectory:true
###フォルダに内包されているコンテンツ
set listArray to {(refMe's NSURLIsDirectoryKey), (refMe's NSURLIsRegularFileKey), (refMe's NSURLPathKey)}
set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
set listFilePathArray to appFileManager's contentsOfDirectoryAtURL:ocidDirPathURL includingPropertiesForKeys:listArray options:ocidOption |error|:(reference)
set ocidFilePathArray to item 1 of listFilePathArray
###パス格納用のリスト
set ocidPathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
###URLリストをパスリストにして(ディレクトリ等を除く処理)
repeat with itemFilePath in ocidFilePathArray
set boolIsRegFile to (item 2 of (itemFilePath's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))) as boolean
set boolIsDir to (item 2 of (itemFilePath's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))) as boolean
if boolIsDir is false then
if boolIsRegFile is true then
set ocidFilePath to item 2 of (itemFilePath's getResourceValue:(reference) forKey:(refMe's NSURLPathKey) |error|:(reference))
(ocidPathArray's addObject:ocidFilePath)
end if
end if
end repeat
#####(同じフォルダ内だから)ファイル名順にソート
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:true selector:"localizedStandardCompare:"
set ocidSortedPathList to (ocidPathArray's sortedArrayUsingDescriptors:{ocidDescriptor})
##################################################
#####準備
##################################################
###########################
####acrobat起動
tell application id "com.adobe.Acrobat.Pro"
launch
end tell
###########################
####起動チェック
tell application id "com.adobe.Acrobat.Pro"
####起動確認最大5秒
repeat 5 times
activate
set boolFrontmost to frontmost as boolean
if boolFrontmost is true then
exit repeat
end if
delay 1
end repeat
end tell
##################################################
#####本処理
##################################################
####出力用テキストの初期化
set strOutPutTextTab to "" as text
###ファイルの数だけ繰り返し
repeat with itemSortedPath in ocidSortedPathList
set strFilePath to itemSortedPath as text
####ファイルを開く
tell application id "com.adobe.Acrobat.Pro"
try
do script "app.openDoc(\"" & strFilePath & "\");"
on error
open file (POSIX file strFilePath as alias)
end try
end tell
####開いたファイルのフォーム情報を取得する
tell application id "com.adobe.Acrobat.Pro"
###フォームの数を数える
do script "var numCntFild = this.numFields"
###AppleScript用にテキストにして戻す
set numCntFild to (do script "numCntFild") as text
end tell
set numCntChkFild to 0 as integer
repeat numCntFild times
tell application id "com.adobe.Acrobat.Pro"
######順番にフォーム名を取得
do script "var strFildTitle = this.getNthFieldName(" & numCntChkFild & ")"
######フォームの内容を取得
do script "var strFormValeText = this.getField(strFildTitle).value;"
######フォームの内容をAppleScriptに渡す
set strFormValue to (do script "this.getField(strFildTitle).value;") as text
end tell
###改行除去
set strFormValue to doReplace(strFormValue, "\r", "")
set strFormValue to doReplace(strFormValue, "\n", "")
######フォーム毎にタブ区切りテキストにする
set strOutPutTextTab to strOutPutTextTab & strFormValue & "\t" as text
set numCntChkFild to numCntChkFild + 1 as integer
end repeat
####1ファイル毎に改行を入れる
set strOutPutTextTab to strOutPutTextTab & "\n" as text
####開いたファイルを閉じる
tell application id "com.adobe.Acrobat.Pro"
do script "closeDoc(false);"
end tell
end repeat
log strOutPutTextTab
####結果テキスト
tell application "Finder"
set aliasFilePath to (file strFileName of folder aliasContainerPath) as alias
end tell
tell application "TextEdit"
activate
set objAvtivDoc to open file aliasFilePath
tell objAvtivDoc
activate
set its text to strOutPutTextTab
save in aliasFilePath
end tell
end tell
####文字の置き換え
to doReplace(argOrignalText, argSearchText, argReplaceText)
set strDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to argSearchText
set listDelim to every text item of argOrignalText
set AppleScript's text item delimiters to argReplaceText
set strReturn to listDelim as text
set AppleScript's text item delimiters to strDelim
return strReturn
end doReplace