[XMP] dc:subjectとpdf:Keywordsの両方のキーワード項目を同じ内容にセットする
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | Bridgeで付与されるダブリンコアのdc:subject |
005 | Acrobatで付与されるプロパティとしてのpdf:Keywords |
006 | この2つのキーワード要素を合算して |
007 | dc:subject、pdf:Keywordsの両方の値として入れます |
008 | XMPの全てのパターンを網羅しているわけではありませんので |
009 | エラーになる場合もあります |
010 | その場合はXML操作部分を改変してください |
011 | *) |
012 | # |
013 | # com.cocolog-nifty.quicktimer.icefloe |
014 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
015 | use AppleScript version "2.8" |
016 | use framework "Foundation" |
017 | use framework "AppKit" |
018 | use framework "PDFKit" |
019 | use scripting additions |
020 | |
021 | property refMe : a reference to current application |
022 | |
023 | ############### |
024 | #Windowの有無 |
025 | tell application "Adobe Acrobat" |
026 | set numCntDoc to (count of PDF Window) as integer |
027 | if numCntDoc = 0 then |
028 | return "Windowがありません PDFを開いていません" |
029 | end if |
030 | end tell |
031 | |
032 | ############### |
033 | #ドキュメントを開いているか |
034 | tell application "Adobe Acrobat" |
035 | set numCntDoc to (count of document) as integer |
036 | if numCntDoc = 0 then |
037 | return "PDFを開いていません" |
038 | end if |
039 | end tell |
040 | |
041 | ############### |
042 | #開いているファイルパス |
043 | tell application "Adobe Acrobat" |
044 | tell front document |
045 | set aliasFilePath to (file alias) as alias |
046 | end tell |
047 | end tell |
048 | |
049 | #################### |
050 | #キーワードの取得 |
051 | tell application "Adobe Acrobat" |
052 | tell active doc |
053 | set strScriptText to ("this.info.Keywords;") as text |
054 | log strScriptText |
055 | try |
056 | set strKeywords to (do script (strScriptText)) as text |
057 | log strKeywords |
058 | on error |
059 | set strKeywords to ("") as text |
060 | end try |
061 | end tell |
062 | end tell |
063 | |
064 | #################### |
065 | #pdf:Keywordsをリストにしておく |
066 | if strKeywords is not "" then |
067 | #STRING |
068 | set ocidKeywordsString to refMe's NSMutableString's alloc()'s initWithString:(strKeywords) |
069 | #Arrayに |
070 | set ocidTmpArray to ocidKeywordsString's componentsSeparatedByString:(",") |
071 | set ocidKeywordsArray to refMe's NSMutableArray's alloc()'s initWithArray:(ocidTmpArray) |
072 | else if strKeywords is "" then |
073 | set ocidKeywordsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
074 | end if |
075 | #################### |
076 | #メタデータの取得XML |
077 | tell application "Adobe Acrobat" |
078 | tell active doc |
079 | set strScriptText to ("this.metadata;") as text |
080 | log strScriptText |
081 | try |
082 | set strXML to (do script (strScriptText)) as text |
083 | log strXML |
084 | on error |
085 | set strXML to ("") as text |
086 | end try |
087 | end tell |
088 | end tell |
089 | |
090 | #################### |
091 | #XMLDocまで |
092 | #テキスト |
093 | set ocidXMLText to (refMe's NSString's stringWithString:(strXML)) |
094 | #NSDATAに |
095 | set ocidXMLData to ocidXMLText's dataUsingEncoding:(refMe's NSUTF8StringEncoding) |
096 | #XMLDocに |
097 | set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyXML) |
098 | set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidXMLData) options:(ocidOption) |error| :(reference) |
099 | if (item 2 of listResponse) = (missing value) then |
100 | log "正常処理" |
101 | set ocidXMLDoc to (item 1 of listResponse) |
102 | else if (item 2 of listResponse) ≠ (missing value) then |
103 | log (item 2 of listResponse)'s code() as text |
104 | log (item 2 of listResponse)'s localizedDescription() as text |
105 | log "NSXMLDocumentエラー 警告がありました" |
106 | set ocidXMLDoc to (item 1 of listResponse) |
107 | end if |
108 | #XMLROOT |
109 | set ocidRootElement to ocidXMLDoc's rootElement() |
110 | # 対象エレメントを階層おって取得 |
111 | set ocidRDF to (ocidRootElement's elementsForName:("rdf:RDF"))'s firstObject() |
112 | set ocidDescription to (ocidRDF's elementsForName:("rdf:Description"))'s firstObject() |
113 | set ocidSubject to (ocidDescription's elementsForName:("dc:subject"))'s firstObject() |
114 | #dc:subjectの項目があるか? |
115 | if ocidSubject = (missing value) then |
116 | log "DCにキーワード未設定" |
117 | set boolSubject to false as boolean |
118 | else |
119 | log "DCにキーワード設定済" |
120 | set boolSubject to true as boolean |
121 | # log ocidSubject's XMLString() as text |
122 | #dc:subjectの項目 |
123 | set listResponse to (ocidDescription's nodesForXPath:("//dc:subject/rdf:Bag/rdf:li") |error| :(reference)) |
124 | if (item 2 of listResponse) = (missing value) then |
125 | log "正常処理" |
126 | set ocidSubjectArray to (item 1 of listResponse) |
127 | # log ocidSubjectArray's className() as text |
128 | else if (item 2 of listResponse) ≠ (missing value) then |
129 | log (item 2 of listResponse)'s code() as text |
130 | log (item 2 of listResponse)'s localizedDescription() as text |
131 | return "エラーしました" |
132 | end if |
133 | #キーワードがあればリストにしていく |
134 | repeat with itemSubject in ocidSubjectArray |
135 | #テキストを値として取得して |
136 | set strValue to itemSubject's stringValue() |
137 | #リストに追加 |
138 | (ocidKeywordsArray's addObject:(strValue)) |
139 | end repeat |
140 | end if |
141 | |
142 | #################### |
143 | #PDFキーワードの付与 |
144 | #カンマ区切りテキストにして |
145 | set strAddKeywords to (ocidKeywordsArray's componentsJoinedByString:("\",\"")) as text |
146 | log strAddKeywords |
147 | tell application "Adobe Acrobat" |
148 | tell active doc |
149 | #リスト形式で追加 |
150 | set strScriptText to ("this.info.Keywords=[\"" & strAddKeywords & "\"];") as text |
151 | log strScriptText |
152 | try |
153 | set strResponse to (do script (strScriptText)) as text |
154 | log strResponse |
155 | on error |
156 | set strResponse to ("") as text |
157 | end try |
158 | end tell |
159 | #保存 |
160 | #save active doc to file aliasFilePath |
161 | end tell |
162 | |
163 | #################### |
164 | #キーワードのNODE追加 |
165 | #subject |
166 | set ocidSubjectNode to (refMe's NSXMLElement's alloc()'s initWithName:("dc:subject")) |
167 | #Bag |
168 | set ocidBagNode to (refMe's NSXMLElement's alloc()'s initWithName:("rdf:Bag")) |
169 | #キーワードの分だけ |
170 | repeat with ItemKeyWords in ocidKeywordsArray |
171 | #rdf:liをテキストの値入りで作成して |
172 | set ocidAddNode to (refMe's NSXMLElement's alloc()'s initWithName:("rdf:li") stringValue:(ItemKeyWords)) |
173 | #rdf:Bagに追加していく |
174 | (ocidBagNode's addChild:(ocidAddNode)) |
175 | end repeat |
176 | #rdf:Bagができたらdc:subjectに追加 |
177 | ocidSubjectNode's addChild:(ocidBagNode) |
178 | #元からsubjectがあったか?で分岐 |
179 | if boolSubject is false then |
180 | #現時点でsubjectがないからDescriptionに追加 |
181 | ocidDescription's addChild:(ocidSubjectNode) |
182 | else if boolSubject is true then |
183 | #現時点でsubjectがあるから入れ替え |
184 | set numCntCild to ocidDescription's childCount() |
185 | #Childを順番に見ていって |
186 | repeat with itemNo from 0 to (numCntCild - 1) by 1 |
187 | set strChildName to ((ocidDescription's childAtIndex:(itemNo))'s |name|()) as text |
188 | #名前がdc:subjectになったら |
189 | if strChildName is "dc:subject" then |
190 | #今あるNODEを削除して |
191 | (ocidDescription's removeChildAtIndex:(itemNo)) |
192 | #新しいNODEをセット |
193 | (ocidDescription's addChild:(ocidSubjectNode)) |
194 | end if |
195 | end repeat |
196 | end if |
197 | ##テキスト形式に |
198 | set strOutPutXMLstring to ocidXMLDoc's XMLString() as text |
199 | |
200 | #################### |
201 | #XMPをPDFに戻す |
202 | tell application "Adobe Acrobat" |
203 | tell active doc |
204 | #XMLをセット |
205 | set strScriptText to ("this.metadata = " & strOutPutXMLstring & ";") as text |
206 | log strScriptText |
207 | try |
208 | set strKeywords to (do script (strScriptText)) as text |
209 | log strKeywords |
210 | on error |
211 | set strKeywords to ("") as text |
212 | end try |
213 | end tell |
214 | end tell |
215 | |
216 | #################### |
217 | #保存 |
218 | tell application "Adobe Acrobat" |
219 | save active doc to file aliasFilePath |
220 | end tell |
AppleScriptで生成しました |
| 固定リンク