PDFの注釈の一覧をテキストで出力する
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | com.cocolog-nifty.quicktimer.icefloe |
005 | PDFの各ページに入っている注釈を一覧にします |
006 | 出力はページ毎 |
007 | 全ページの2種 |
008 | *) |
009 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
010 | use AppleScript version "2.8" |
011 | use framework "Foundation" |
012 | use framework "AppKit" |
013 | use framework "PDFKit" |
014 | use scripting additions |
015 | |
016 | property refMe : a reference to current application |
017 | |
018 | |
019 | ######################## |
020 | #設定項目 |
021 | #ポップアップ分も出力するか? true false |
022 | set boolAddPopup to false as boolean |
023 | #注釈が無いページも出力するか? true false |
024 | set boolZeroAno to false as boolean |
025 | |
026 | ######################## |
027 | #ダイアログ 入力 |
028 | set strName to (name of current application) as text |
029 | if strName is "osascript" then |
030 | tell application "Finder" to activate |
031 | else |
032 | tell current application to activate |
033 | end if |
034 | # デフォルトロケーション |
035 | set appFileManager to refMe's NSFileManager's defaultManager() |
036 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
037 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
038 | set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias |
039 | set listUTI to {"com.adobe.pdf"} |
040 | set strMes to ("PDFファイルを選んでください") as text |
041 | set strPrompt to ("PDFファイルを選んでください") as text |
042 | try |
043 | #ファイル選択時 |
044 | set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDesktopDirPath) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
045 | on error |
046 | log "エラーしました" |
047 | return "エラーしました" |
048 | end try |
049 | ######################## |
050 | #入力ファイルパス |
051 | set strFilePath to (POSIX path of aliasFilePath) as text |
052 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
053 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
054 | set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false) |
055 | set strFilePathURL to ocidFilePathURL's absoluteString() as text |
056 | |
057 | ######################## |
058 | #ダイアログ 出力先フォルダ |
059 | (* ファイル名から自動生成に変更 |
060 | set strName to (name of current application) as text |
061 | if strName is "osascript" then |
062 | tell application "Finder" to activate |
063 | else |
064 | tell current application to activate |
065 | end if |
066 | # デフォルトロケーション |
067 | #選択したPDFファイルと同じディレクトリ |
068 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
069 | set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias |
070 | # |
071 | set strMes to ("保存先フォルダを選んでください\nページ数が多い場合はフォルダ作成した方がいいです") as text |
072 | set strPrompt to ("保存先フォルダを選んでください\nページ数が多い場合はフォルダ作成した方がいいです") as text |
073 | try |
074 | set aliasSaveDirPath to (choose folder strMes with prompt strPrompt default location aliasContainerDirPath with invisibles and showing package contents without multiple selections allowed) as alias |
075 | on error |
076 | log "エラーしました" |
077 | return "エラーしました" |
078 | end try |
079 | *) |
080 | #出力先フォルダパス |
081 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
082 | set strSaveDirName to (ocidFilePathURL's lastPathComponent())'s mutableCopy() |
083 | (strSaveDirName's appendString:("-注釈一覧")) |
084 | #set strBaseFileName to (ocidFilePathURL's lastPathComponent()) as text |
085 | #set strSaveDirName to (strBaseFileName & "-テキスト抽出") |
086 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
087 | set ocidSaveDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strSaveDirName) isDirectory:(true) |
088 | #フォルダ作成 |
089 | set appFileManager to refMe's NSFileManager's defaultManager() |
090 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
091 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
092 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
093 | |
094 | ######################## |
095 | #NSDATA |
096 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
097 | set listReadData to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference) |
098 | set ocidReadData to (item 1 of listReadData) |
099 | |
100 | ######################## |
101 | #PDFDocument |
102 | set ocidActiveDoc to refMe's PDFDocument's alloc()'s initWithData:(ocidReadData) |
103 | #総ページ数 |
104 | set numCntPage to ocidActiveDoc's pageCount() |
105 | #出力用テキスト |
106 | set ocidAllAnnotstring to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
107 | (ocidAllAnnotstring's appendString:(strFilePathURL)) |
108 | (ocidAllAnnotstring's appendString:("\n")) |
109 | (ocidAllAnnotstring's appendString:("\n")) |
110 | #ページ数分繰り返し |
111 | repeat with itemIntNo from 0 to (numCntPage - 1) by 1 |
112 | #ページを取り出して |
113 | set ocidActivePage to (ocidActiveDoc's pageAtIndex:(itemIntNo)) |
114 | #ページ用テキスト |
115 | set ocidPageOutputString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
116 | ################## |
117 | #テキストを抽出 |
118 | set ocidAnnotationsArray to ocidActivePage's annotations() |
119 | #注釈の数 |
120 | set numCntPageAnnot to ocidAnnotationsArray's |count|() as integer |
121 | (ocidPageOutputString's appendString:(strFilePathURL)) |
122 | (ocidPageOutputString's appendString:("\n")) |
123 | set strSetValue to ((itemIntNo + 1) & "ページの注釈数: " & numCntPageAnnot) as text |
124 | (ocidPageOutputString's appendString:(strSetValue)) |
125 | (ocidPageOutputString's appendString:("\n")) |
126 | (ocidAllAnnotstring's appendString:("-------------\n")) |
127 | (ocidAllAnnotstring's appendString:(strSetValue)) |
128 | (ocidAllAnnotstring's appendString:("\n")) |
129 | #注釈の数だけ繰り返す |
130 | repeat with itemAnnotation in ocidAnnotationsArray |
131 | set strType to itemAnnotation's type() as text |
132 | if strType is "Popup" then |
133 | if boolAddPopup is true then |
134 | set strUserName to itemAnnotation's userName() as text |
135 | set dateMod to itemAnnotation's modificationDate() as date |
136 | if strType is "Stamp" then |
137 | set strContents to itemAnnotation's |stampName|() as text |
138 | else |
139 | set strContents to itemAnnotation's |contents|() as text |
140 | end if |
141 | set strSetValue to (strType & "\t" & strContents & "\t" & strUserName & "\t" & dateMod) as text |
142 | (ocidPageOutputString's appendString:(strSetValue)) |
143 | (ocidPageOutputString's appendString:("\n")) |
144 | (ocidAllAnnotstring's appendString:(strSetValue)) |
145 | (ocidAllAnnotstring's appendString:("\n")) |
146 | end if |
147 | else |
148 | set strUserName to itemAnnotation's userName() as text |
149 | set dateMod to itemAnnotation's modificationDate() as date |
150 | if strType is "Stamp" then |
151 | set strContents to itemAnnotation's |stampName|() as text |
152 | else |
153 | set strContents to itemAnnotation's |contents|() as text |
154 | end if |
155 | |
156 | set strSetValue to (strType & "\t" & strContents & "\t" & strUserName & "\t" & dateMod) as text |
157 | (ocidPageOutputString's appendString:(strSetValue)) |
158 | (ocidPageOutputString's appendString:("\n")) |
159 | (ocidAllAnnotstring's appendString:(strSetValue)) |
160 | (ocidAllAnnotstring's appendString:("\n")) |
161 | end if |
162 | end repeat |
163 | (ocidPageOutputString's appendString:("\n")) |
164 | (ocidAllAnnotstring's appendString:("\n")) |
165 | if numCntPageAnnot = 0 then |
166 | if boolZeroAno is true then |
167 | #保存するファイル名 |
168 | set strSaveFileNameText to ((itemIntNo + 1) & ".txt") as text |
169 | #保存先パス |
170 | set ocidSaveFilePathURLText to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileNameText) isDirectory:(false)) |
171 | #保存 |
172 | set listDone to (ocidPageOutputString's writeToURL:(ocidSaveFilePathURLText) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
173 | if (item 1 of listDone) is true then |
174 | log (itemIntNo + 1) & "ページ目:正常終了" as text |
175 | else if (item 1 of listDone) is false then |
176 | log (item 2 of listDone)'s localizedDescription() as text |
177 | return (itemIntNo + 1) & "ページ目:保存に失敗しました" as text |
178 | end if |
179 | end if |
180 | else |
181 | #保存するファイル名 |
182 | set strSaveFileNameText to ((itemIntNo + 1) & ".txt") as text |
183 | #保存先パス |
184 | set ocidSaveFilePathURLText to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileNameText) isDirectory:(false)) |
185 | #保存 |
186 | set listDone to (ocidPageOutputString's writeToURL:(ocidSaveFilePathURLText) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
187 | if (item 1 of listDone) is true then |
188 | log (itemIntNo + 1) & "ページ目:正常終了" as text |
189 | else if (item 1 of listDone) is false then |
190 | log (item 2 of listDone)'s localizedDescription() as text |
191 | return (itemIntNo + 1) & "ページ目:保存に失敗しました" as text |
192 | end if |
193 | |
194 | end if |
195 | end repeat |
196 | set strSaveFileNameText to ("_注釈一覧.txt") as text |
197 | #保存先パス |
198 | set ocidSaveFilePathURLText to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileNameText) isDirectory:(false)) |
199 | #保存 |
200 | set listDone to (ocidAllAnnotstring's writeToURL:(ocidSaveFilePathURLText) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
201 | if (item 1 of listDone) is true then |
202 | log (itemIntNo + 1) & "ページ目:正常終了" as text |
203 | else if (item 1 of listDone) is false then |
204 | log (item 2 of listDone)'s localizedDescription() as text |
205 | return (itemIntNo + 1) & "ページ目:保存に失敗しました" as text |
206 | end if |
207 | |
208 | |
209 | return "終了" |
AppleScriptで生成しました |
| 固定リンク