PDFパスワード設定 (UIの呼び出しを SystemUIServer に変更した)
最新版・修正版はカテゴリートップから見ていってください
PDFKit DocumentPermissions
ソース | |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | 別名で保存からアプリケーションにすると便利です |
005 | |
006 | パスワードは自動生成UUIDを使っています |
007 | PWを記載したテキストファイルも同時に保存します |
008 | |
009 | v2 選択内容に依存がある場合の対応をした |
010 | v3 UIの呼び出しを SystemUIServer に変更した |
011 | v3.1 微修正 使っていないframeworkをコメントアウト |
012 | com.cocolog-nifty.quicktimer.icefloe *) |
013 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
014 | use AppleScript version "2.8" |
015 | use framework "Foundation" |
016 | use framework "AppKit" |
017 | use framework "PDFKit" |
018 | #use framework "Quartz" |
019 | #use framework "QuartzCore" |
020 | #use framework "CoreGraphics" |
021 | use scripting additions |
022 | |
023 | property refMe : a reference to current application |
024 | |
025 | ####所有者パスワード |
026 | property boolOwnerPW : true as boolean |
027 | ####アクアセス権 |
028 | ## property intAllowNo : 0 as integer |
029 | (* |
030 | 例:全部OKの場合 255 全部ロックは0 |
031 | 許可する番号を『足し算』する |
032 | 低解像度印刷 (*1*) refMe's PDFAllowsLowQualityPrinting |
033 | 高解像度印刷 (*2*) refMe's PDFAllowsHighQualityPrinting |
034 | 文書に変更 (*4*) refMe's PDFAllowsDocumentChanges |
035 | アッセンブリ (*8*) refMe's PDFAllowsDocumentAssembly |
036 | コンテンツコピー(*16*) refMe's PDFAllowsContentCopying |
037 | アクセシビリティ(*32*) refMe's PDFAllowsContentAccessibility |
038 | コメント注釈 (*64*) refMe's PDFAllowsCommenting |
039 | フォーム入力 (*128*) refMe's PDFAllowsFormFieldEntry |
040 | |
041 | log refMe's PDFAllowsLowQualityPrinting as integer |
042 | log refMe's PDFAllowsHighQualityPrinting as integer |
043 | log refMe's PDFAllowsDocumentChanges as integer |
044 | log refMe's PDFAllowsDocumentAssembly as integer |
045 | log refMe's PDFAllowsContentCopying as integer |
046 | log refMe's PDFAllowsContentAccessibility as integer |
047 | log refMe's PDFAllowsCommenting as integer |
048 | log refMe's PDFAllowsFormFieldEntry as integer |
049 | |
050 | *) |
051 | |
052 | on run |
053 | ###ダイアログ |
054 | set strName to (name of current application) as text |
055 | if strName is "osascript" then |
056 | tell application "SystemUIServer" to activate |
057 | else |
058 | tell current application to activate |
059 | end if |
060 | set appFileManager to refMe's NSFileManager's defaultManager() |
061 | set ocidUserDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
062 | set ocidUserDesktopPath to ocidUserDesktopPathArray's objectAtIndex:(0) |
063 | set listChooseFileUTI to {"com.adobe.pdf"} as list |
064 | set strPromptText to "PDFファイルを選んでください" as text |
065 | tell application "SystemUIServer" |
066 | activate |
067 | set listDropObject to (choose file with prompt strPromptText default location (ocidUserDesktopPath as alias) of type listChooseFileUTI with invisibles and multiple selections allowed without showing package contents) as list |
068 | end tell |
069 | tell application "SystemUIServer" to quit |
070 | #選択済みファイルリストをOpenに渡す |
071 | open listDropObject |
072 | tell application "SystemUIServer" to quit |
073 | end run |
074 | |
075 | on open listDropObject |
076 | ############################## |
077 | set recordOption to {|0開封PW有|:999, |1低解像度印刷|:1, |2高解像度印刷|:2, |4文書に変更|:4, |8内容変更|:8, |16コンテンツコピー|:16, |32補助装置利用可|:32, |64コメント注釈|:64, |128フォーム入力|:128} as record |
078 | #↑のレコードからキーだけを取り出す |
079 | set ocidOptionDict to refMe's NSMutableDictionary's alloc()'s init() |
080 | ocidOptionDict's setDictionary:(recordOption) |
081 | #キーを取り出して |
082 | set ocidAllKeys to ocidOptionDict's allKeys() |
083 | #正順に並び替え |
084 | set ocidSortedArray to ocidAllKeys's sortedArrayUsingSelector:("localizedStandardCompare:") |
085 | #ダイアログ用にArrayをリストにしておく |
086 | set listAllKeys to ocidSortedArray as list |
087 | ###ダイアログを前面に出す |
088 | set strName to (name of current application) as text |
089 | if strName is "osascript" then |
090 | tell application "SystemUIServer" to activate |
091 | else |
092 | tell current application to activate |
093 | end if |
094 | ### |
095 | set strTitle to "『禁止設定する』オプション選択(複数可)" as text |
096 | set strPrompt to "『禁止設定』するオプションを選択してください\r何も選択しない=全部許可\r何も選択しない場合所有者PWの設定とページ抽出とコンテンツの変更は禁止になります\r\r複数選択はコマンド⌘キーを押しながらクリック\r組み合わせ留意点\r例:\r低解像度印刷の禁止は<高解像度の印刷の禁止に内包されるので無意味な選択になる組み合わせがあります" as text |
097 | try |
098 | tell application "SystemUIServer" |
099 | activate |
100 | set listResponse to (choose from list listAllKeys with title strTitle with prompt strPrompt default items (item 1 of listAllKeys) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed and empty selection allowed) as list |
101 | end tell |
102 | on error |
103 | tell application "SystemUIServer" to quit |
104 | log "エラーしました" |
105 | return "エラーしました" |
106 | error "エラーしました" number -200 |
107 | end try |
108 | tell application "SystemUIServer" to quit |
109 | if listResponse = {} then |
110 | log "何も選択していない" |
111 | set boolOpenPW to false as boolean |
112 | set intAllowNo to 0 as integer |
113 | else if (item 1 of listResponse) is false then |
114 | return "キャンセルしました" |
115 | error "キャンセルしました" number -200 |
116 | else |
117 | set boolOpenPW to false as boolean |
118 | set intAllowNo to 0 as integer |
119 | repeat with itemResponse in listResponse |
120 | set intValue to (ocidOptionDict's valueForKey:(itemResponse)) as integer |
121 | #戻り値が999ならOPEN時のパスワード設定をする |
122 | if intValue = 999 then |
123 | set boolOpenPW to true as boolean |
124 | else |
125 | set intAllowNo to intAllowNo + intValue |
126 | end if |
127 | end repeat |
128 | end if |
129 | ##許可番号 |
130 | set intAllowNo to (255 - intAllowNo) as integer |
131 | |
132 | ######################################## |
133 | #実際の処理するPDFのURLを格納するリスト |
134 | set ocidDropPathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
135 | ###ドロップされたエイリアスの数だけ繰り返し |
136 | repeat with itemDropObject in listDropObject |
137 | ###処理除外するエイリアスを判定する |
138 | set strFilePaht to (POSIX path of itemDropObject) as text |
139 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePaht)) |
140 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
141 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath)) |
142 | ###フォルダか?判定 |
143 | set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference)) |
144 | if (item 3 of listResponse) = (missing value) then |
145 | log "getResourceValue 正常処理" |
146 | set ocidURLvalue to (item 2 of listResponse) |
147 | else if (item 3 of listResponse) ≠ (missing value) then |
148 | set strErrorNO to (item 2 of listDone)'s code() as text |
149 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
150 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
151 | return "getResourceValue エラーしました" & strErrorNO & strErrorMes |
152 | end if |
153 | #フォルダじゃ無いって事は? |
154 | if ocidURLvalue = (refMe's NSNumber's numberWithBool:false) then |
155 | set listURLvalue to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference)) |
156 | if (item 3 of listResponse) = (missing value) then |
157 | log "getResourceValue 正常処理" |
158 | #UTIを取得して |
159 | set strURLvalue to (item 2 of listURLvalue)'s identifier() as text |
160 | else if (item 3 of listResponse) ≠ (missing value) then |
161 | set strErrorNO to (item 2 of listDone)'s code() as text |
162 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
163 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
164 | return "getResourceValue エラーしました" & strErrorNO & strErrorMes |
165 | end if |
166 | ###getResourceValueの戻り値がNULLだった場合対策 |
167 | if strURLvalue is "" then |
168 | tell application "Finder" |
169 | set objInfo to info for aliasFilePath |
170 | set strURLvalue to type identifier of objInfo as text |
171 | end tell |
172 | end if |
173 | ###UTIがPDFのエイリアスだけ処理する |
174 | if strURLvalue is "com.adobe.pdf" then |
175 | #####PDFDocumentとして読み込み |
176 | set ocidChkDoc to (refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL)) |
177 | ######################################## |
178 | #暗号化チェック |
179 | set boolEncrypted to ocidChkDoc's isEncrypted() |
180 | if boolEncrypted is true then |
181 | set strName to (name of current application) as text |
182 | if strName is "osascript" then |
183 | tell application "SystemUIServer" to activate |
184 | else |
185 | tell current application to activate |
186 | end if |
187 | tell application "SystemUIServer" |
188 | activate |
189 | set strMes to "エラー:すでに暗号化されているので変更できません" as text |
190 | display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3 |
191 | end tell |
192 | tell application "SystemUIServer" to quit |
193 | refMe's NSLog("■:" & strMes) |
194 | return strMes |
195 | end if |
196 | ######################################## |
197 | #ロック確認 |
198 | set boolLocked to ocidChkDoc's isLocked() |
199 | log boolLocked |
200 | if boolLocked is true then |
201 | set strMes to "エラー:すでにパスワードでロックされているので変更できません" as text |
202 | set strName to (name of current application) as text |
203 | if strName is "osascript" then |
204 | tell application "SystemUIServer" to activate |
205 | else |
206 | tell current application to activate |
207 | end if |
208 | tell application "SystemUIServer" |
209 | activate |
210 | display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3 |
211 | refMe's NSLog("■:" & strMes) |
212 | end tell |
213 | tell application "SystemUIServer" to quit |
214 | return strMes |
215 | end if |
216 | (ocidDropPathURLArray's addObject:(ocidFilePathURL)) |
217 | else |
218 | set strMes to "エラー:PDF専用です" as text |
219 | set strName to (name of current application) as text |
220 | if strName is "osascript" then |
221 | tell application "SystemUIServer" to activate |
222 | else |
223 | tell current application to activate |
224 | end if |
225 | tell application "SystemUIServer" |
226 | activate |
227 | display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3 |
228 | end tell |
229 | tell application "SystemUIServer" to quit |
230 | refMe's NSLog("■:" & strMes) |
231 | return strMes |
232 | end if |
233 | else |
234 | set strMes to "エラー:PDF専用です" as text |
235 | set strName to (name of current application) as text |
236 | if strName is "osascript" then |
237 | tell application "SystemUIServer" to activate |
238 | else |
239 | tell current application to activate |
240 | end if |
241 | tell application "SystemUIServer" |
242 | activate |
243 | display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3 |
244 | end tell |
245 | tell application "SystemUIServer" to quit |
246 | refMe's NSLog("■:" & strMes) |
247 | return strMes |
248 | end if |
249 | end repeat |
250 | ####本処理 |
251 | repeat with itemPathURL in ocidDropPathURLArray |
252 | set ocidFileName to itemPathURL's lastPathComponent() |
253 | set strBaseFileName to ocidFileName's stringByDeletingPathExtension() as text |
254 | ###PW設定は複製したPDFに対して行うため保存先ディレクトリを作る |
255 | set strDirName to (strBaseFileName & "_PW設定済") |
256 | set ocidContainerDirURL to itemPathURL's URLByDeletingLastPathComponent() |
257 | set ocidDistFolderURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strDirName)) |
258 | set ocidSavePdfURL to (ocidDistFolderURL's URLByAppendingPathComponent:(ocidFileName)) |
259 | ####ファイル移動先 |
260 | set appFileManager to refMe's NSFileManager's defaultManager() |
261 | set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s init()) |
262 | (ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)) |
263 | set listDone to (appFileManager's createDirectoryAtURL:(ocidDistFolderURL) withIntermediateDirectories:true attributes:ocidAttrDict |error|:(reference)) |
264 | if (item 1 of listDone) is true then |
265 | log "createDirectoryAtURL 正常処理" |
266 | else if (item 2 of listDone) ≠ (missing value) then |
267 | set strErrorNO to (item 2 of listDone)'s code() as text |
268 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
269 | refMe's NSLog("■:移動でエラー" & strErrorNO & strErrorMes) |
270 | return "createDirectoryAtURL エラーしました" & strErrorNO & strErrorMes |
271 | end if |
272 | |
273 | ####コピー |
274 | set listDone to (appFileManager's copyItemAtURL:(itemPathURL) toURL:(ocidSavePdfURL) |error|:(reference)) |
275 | if (item 1 of listDone) is true then |
276 | log "copyItemAtURL 正常処理" |
277 | else if (item 2 of listDone) ≠ (missing value) then |
278 | set strErrorNO to (item 2 of listDone)'s code() as text |
279 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
280 | refMe's NSLog("■:コピーでエラー" & strErrorNO & strErrorMes) |
281 | return "copyItemAtURL エラーしました" & strErrorNO & strErrorMes |
282 | end if |
283 | |
284 | #####PDFDocumentとして読み込み |
285 | set ocidActivDoc to (refMe's PDFDocument's alloc()'s initWithURL:(ocidSavePdfURL)) |
286 | ######################################## |
287 | #パスワード生成 UUIDを利用 |
288 | set strTextFileName to strBaseFileName & ".pw.txt" |
289 | set ocidTextFilePathURL to (ocidDistFolderURL's URLByAppendingPathComponent:strTextFileName) |
290 | #####生成したUUIDからハイフンを取り除く |
291 | set ocidUUIDString to (refMe's NSMutableString's alloc()'s init()) |
292 | set ocidConcreteUUID to refMe's NSUUID's UUID() |
293 | (ocidUUIDString's setString:(ocidConcreteUUID's UUIDString())) |
294 | set ocidUUIDRange to (ocidUUIDString's rangeOfString:ocidUUIDString) |
295 | (ocidUUIDString's replaceOccurrencesOfString:("-") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidUUIDRange) |
296 | |
297 | set strOwnerPassword to ocidUUIDString as text |
298 | ##保存用テキストにする |
299 | set strTextFile to "所有者用Pw(教えてはいけないヤツ)\n" & strOwnerPassword & "\n" as text |
300 | if boolOpenPW is true then |
301 | #####生成したUUIDからハイフンを取り除く |
302 | set ocidUUIDString to (refMe's NSMutableString's alloc()'s init()) |
303 | set ocidConcreteUUID to refMe's NSUUID's UUID() |
304 | (ocidUUIDString's setString:(ocidConcreteUUID's UUIDString())) |
305 | set ocidUUIDRange to (ocidUUIDString's rangeOfString:ocidUUIDString) |
306 | (ocidUUIDString's replaceOccurrencesOfString:("-") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidUUIDRange) |
307 | ##保存用テキストにする |
308 | set strUserPassword to ocidUUIDString as text |
309 | set strTextFile to strTextFile & "\n利用者用Pw(他者に教える場合はこちら↓)\n\n\n" as text |
310 | set strTextFile to strTextFile & "別に送付しましたPDFの開封パスワードです。\nコピペ時には改行が入らないように留意ください\n" as text |
311 | set strTextFile to strTextFile & "\n" & strUserPassword & "\n" as text |
312 | |
313 | end if |
314 | set ocidPWString to (refMe's NSString's stringWithString:strTextFile) |
315 | set ocidUUIDData to (ocidPWString's dataUsingEncoding:(refMe's NSUTF8StringEncoding)) |
316 | ##PWをテキストで保存する |
317 | set boolResults to (ocidUUIDData's writeToURL:ocidTextFilePathURL atomically:true) |
318 | ######################################## |
319 | #保存OPTION |
320 | set ocidOptionDict to (refMe's NSMutableDictionary's alloc()'s init()) |
321 | if boolOwnerPW = true then |
322 | (ocidOptionDict's setObject:(strOwnerPassword) forKey:(refMe's PDFDocumentOwnerPasswordOption)) |
323 | end if |
324 | if boolOpenPW = true then |
325 | ####開封パスワード |
326 | (ocidOptionDict's setObject:(strUserPassword) forKey:(refMe's PDFDocumentUserPasswordOption)) |
327 | end if |
328 | ############################## |
329 | ###セキュリティ設定 |
330 | ###PDFKitのバグ対策もかねて |
331 | log intAllowNo |
332 | #個別の依存でよくある誤りを修正しておく |
333 | if intAllowNo is 191 then |
334 | #注釈禁止の場合ドキュメント変更禁止が必要なので追加しておく |
335 | set intAllowNo to (intAllowNo - (refMe's PDFAllowsDocumentChanges as integer)) as integer |
336 | else if intAllowNo is 254 then |
337 | #低解像度印刷禁止の場合高解像度印刷禁止も必要なので追加しておく |
338 | set intAllowNo to (intAllowNo - (refMe's PDFAllowsHighQualityPrinting as integer)) as integer |
339 | else if intAllowNo is 247 then |
340 | #アッセンブリ禁止の場合はドキュメントに変更も禁止の必要がある |
341 | set intAllowNo to (intAllowNo - (refMe's PDFAllowsDocumentChanges as integer)) as integer |
342 | else if intAllowNo is 127 then |
343 | #フォーム入力禁止の場合はドキュメントに変更も禁止+注釈禁止の必要がある |
344 | set intAllowNo to (intAllowNo - (refMe's PDFAllowsDocumentChanges as integer) - (refMe's PDFAllowsCommenting as integer)) as integer |
345 | end if |
346 | log intAllowNo |
347 | (ocidOptionDict's setObject:(intAllowNo) forKey:(refMe's PDFDocumentAccessPermissionsOption)) |
348 | ######################################## |
349 | #保存 |
350 | set boolResults to (ocidActivDoc's writeToURL:(ocidSavePdfURL) withOptions:(ocidOptionDict)) |
351 | end repeat |
352 | ######################################## |
353 | #保存先を開く |
354 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
355 | set boolDone to appSharedWorkspace's openURL:(ocidDistFolderURL) |
356 | if boolDone is true then |
357 | log "openURL 正常処理" |
358 | else if boolDone is false then |
359 | refMe's NSLog("■:フォルダオープンでエラー") |
360 | log "openURL エラーしました" |
361 | end if |
362 | tell application "SystemUIServer" to quit |
363 | end open |
AppleScriptで生成しました |
PDFパスワード設定.scpt
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | 別名で保存からアプリケーションにすると便利です |
005 | v2 選択内容に依存がある場合の対応をした |
006 | v3 UIの呼び出しを SystemUIServer に変更した |
007 | com.cocolog-nifty.quicktimer.icefloe *) |
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
009 | use AppleScript version "2.8" |
010 | use framework "Foundation" |
011 | use framework "AppKit" |
012 | use framework "PDFKit" |
013 | use framework "Quartz" |
014 | use framework "QuartzCore" |
015 | use framework "CoreGraphics" |
016 | use scripting additions |
017 | |
018 | property refMe : a reference to current application |
019 | |
020 | ####所有者パスワード |
021 | property boolOwnerPW : true as boolean |
022 | ####アクアセス権 |
023 | ## property intAllowNo : 0 as integer |
024 | (* |
025 | 例:全部OKの場合 255 全部ロックは0 |
026 | 許可する番号を『足し算』する |
027 | 低解像度印刷 (*1*) refMe's PDFAllowsLowQualityPrinting |
028 | 高解像度印刷 (*2*) refMe's PDFAllowsHighQualityPrinting |
029 | 文書に変更 (*4*) refMe's PDFAllowsDocumentChanges |
030 | アッセンブリ (*8*) refMe's PDFAllowsDocumentAssembly |
031 | コンテンツコピー(*16*) refMe's PDFAllowsContentCopying |
032 | アクセシビリティ(*32*) refMe's PDFAllowsContentAccessibility |
033 | コメント注釈 (*64*) refMe's PDFAllowsCommenting |
034 | フォーム入力 (*128*) refMe's PDFAllowsFormFieldEntry |
035 | |
036 | log refMe's PDFAllowsLowQualityPrinting as integer |
037 | log refMe's PDFAllowsHighQualityPrinting as integer |
038 | log refMe's PDFAllowsDocumentChanges as integer |
039 | log refMe's PDFAllowsDocumentAssembly as integer |
040 | log refMe's PDFAllowsContentCopying as integer |
041 | log refMe's PDFAllowsContentAccessibility as integer |
042 | log refMe's PDFAllowsCommenting as integer |
043 | log refMe's PDFAllowsFormFieldEntry as integer |
044 | |
045 | *) |
046 | |
047 | on run |
048 | ###ダイアログ |
049 | set strName to (name of current application) as text |
050 | if strName is "osascript" then |
051 | tell application "SystemUIServer" to activate |
052 | else |
053 | tell current application to activate |
054 | end if |
055 | set appFileManager to refMe's NSFileManager's defaultManager() |
056 | set ocidUserDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
057 | set ocidUserDesktopPath to ocidUserDesktopPathArray's objectAtIndex:0 |
058 | set listChooseFileUTI to {"com.adobe.pdf"} |
059 | set strPromptText to "PDFファイルを選んでください" as text |
060 | tell application "SystemUIServer" |
061 | activate |
062 | set listDropObject to (choose file with prompt strPromptText default location (ocidUserDesktopPath as alias) of type listChooseFileUTI with invisibles and multiple selections allowed without showing package contents) as list |
063 | end tell |
064 | tell application "SystemUIServer" to quit |
065 | #選択済みファイルリストをOpenに渡す |
066 | open listDropObject |
067 | end run |
068 | |
069 | on open listDropObject |
070 | ############################## |
071 | set recordOption to {|0開封PW有|:999, |1低解像度印刷|:1, |2高解像度印刷|:2, |4文書に変更|:4, |8内容変更|:8, |16コンテンツコピー|:16, |32補助装置利用可|:32, |64コメント注釈|:64, |128フォーム入力|:128} as record |
072 | #↑のレコードからキーだけを取り出す |
073 | set ocidOptionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
074 | ocidOptionDict's setDictionary:(recordOption) |
075 | #キーを取り出して |
076 | set ocidAllKeys to ocidOptionDict's allKeys() |
077 | #正順に並び替え |
078 | set ocidSortedArray to ocidAllKeys's sortedArrayUsingSelector:("localizedStandardCompare:") |
079 | #ダイアログ用にArrayをリストにしておく |
080 | set listAllKeys to ocidSortedArray as list |
081 | ###ダイアログを前面に出す |
082 | set strName to (name of current application) as text |
083 | if strName is "osascript" then |
084 | tell application "SystemUIServer" to activate |
085 | else |
086 | tell current application to activate |
087 | end if |
088 | ### |
089 | set strTitle to "『禁止設定する』オプション選択(複数可)" as text |
090 | set strPrompt to "『禁止設定』するオプションを選択してください\r何も選択しない=全部許可\r何も選択しない場合所有者PWの設定とページ抽出とコンテンツの変更は禁止になります\r\r複数選択はコマンド⌘キーを押しながらクリック\r組み合わせ留意点\r例:\r低解像度印刷の禁止は<高解像度の印刷の禁止に内包されるので無意味な選択になる組み合わせがあります" as text |
091 | try |
092 | tell application "SystemUIServer" |
093 | activate |
094 | set listResponse to (choose from list listAllKeys with title strTitle with prompt strPrompt default items (item 1 of listAllKeys) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed and empty selection allowed) as list |
095 | end tell |
096 | on error |
097 | tell application "SystemUIServer" to quit |
098 | log "エラーしました" |
099 | return "エラーしました" |
100 | error "エラーしました" number -200 |
101 | end try |
102 | tell application "SystemUIServer" to quit |
103 | if listResponse = {} then |
104 | log "何も選択していない" |
105 | set boolOpenPW to false as boolean |
106 | set intAllowNo to 0 as integer |
107 | else if (item 1 of listResponse) is false then |
108 | return "キャンセルしました" |
109 | error "キャンセルしました" number -200 |
110 | else |
111 | set boolOpenPW to false as boolean |
112 | set intAllowNo to 0 as integer |
113 | repeat with itemResponse in listResponse |
114 | set intValue to (ocidOptionDict's valueForKey:(itemResponse)) as integer |
115 | #戻り値が999ならOPEN時のパスワード設定をする |
116 | if intValue = 999 then |
117 | set boolOpenPW to true as boolean |
118 | else |
119 | set intAllowNo to intAllowNo + intValue |
120 | end if |
121 | end repeat |
122 | end if |
123 | ##許可番号 |
124 | set intAllowNo to (255 - intAllowNo) as integer |
125 | |
126 | ######################################## |
127 | #実際の処理するPDFのURLを格納するリスト |
128 | set ocidDropPathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
129 | ###ドロップされたエイリアスの数だけ繰り返し |
130 | repeat with itemDropObject in listDropObject |
131 | ###処理除外するエイリアスを判定する |
132 | set strFilePaht to (POSIX path of itemDropObject) as text |
133 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePaht)) |
134 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
135 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath)) |
136 | ###フォルダか?判定 |
137 | set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error| :(reference)) |
138 | if (item 3 of listResponse) = (missing value) then |
139 | log "getResourceValue 正常処理" |
140 | set ocidURLvalue to (item 2 of listResponse) |
141 | else if (item 3 of listResponse) ≠ (missing value) then |
142 | set strErrorNO to (item 2 of listDone)'s code() as text |
143 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
144 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
145 | return "getResourceValue エラーしました" & strErrorNO & strErrorMes |
146 | end if |
147 | #フォルダじゃ無いって事は? |
148 | if ocidURLvalue = (refMe's NSNumber's numberWithBool:false) then |
149 | set listURLvalue to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference)) |
150 | if (item 3 of listResponse) = (missing value) then |
151 | log "getResourceValue 正常処理" |
152 | #UTIを取得して |
153 | set strURLvalue to (item 2 of listURLvalue)'s identifier() as text |
154 | else if (item 3 of listResponse) ≠ (missing value) then |
155 | set strErrorNO to (item 2 of listDone)'s code() as text |
156 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
157 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
158 | return "getResourceValue エラーしました" & strErrorNO & strErrorMes |
159 | end if |
160 | ###getResourceValueの戻り値がNULLだった場合対策 |
161 | if strURLvalue is "" then |
162 | tell application "Finder" |
163 | set objInfo to info for aliasFilePath |
164 | set strURLvalue to type identifier of objInfo as text |
165 | end tell |
166 | end if |
167 | ###UTIがPDFのエイリアスだけ処理する |
168 | if strURLvalue is "com.adobe.pdf" then |
169 | #####PDFDocumentとして読み込み |
170 | set ocidChkDoc to (refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL)) |
171 | ######################################## |
172 | #暗号化チェック |
173 | set boolEncrypted to ocidChkDoc's isEncrypted() |
174 | if boolEncrypted is true then |
175 | set strName to (name of current application) as text |
176 | if strName is "osascript" then |
177 | tell application "SystemUIServer" to activate |
178 | else |
179 | tell current application to activate |
180 | end if |
181 | tell application "SystemUIServer" |
182 | activate |
183 | set strMes to "エラー:すでに暗号化されているので変更できません" as text |
184 | display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3 |
185 | end tell |
186 | tell application "SystemUIServer" to quit |
187 | refMe's NSLog("■:" & strMes) |
188 | return strMes |
189 | end if |
190 | ######################################## |
191 | #ロック確認 |
192 | set boolLocked to ocidChkDoc's isLocked() |
193 | log boolLocked |
194 | if boolLocked is true then |
195 | set strMes to "エラー:すでにパスワードでロックされているので変更できません" as text |
196 | set strName to (name of current application) as text |
197 | if strName is "osascript" then |
198 | tell application "SystemUIServer" to activate |
199 | else |
200 | tell current application to activate |
201 | end if |
202 | tell application "SystemUIServer" |
203 | activate |
204 | display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3 |
205 | refMe's NSLog("■:" & strMes) |
206 | end tell |
207 | tell application "SystemUIServer" to quit |
208 | return strMes |
209 | end if |
210 | (ocidDropPathURLArray's addObject:(ocidFilePathURL)) |
211 | else |
212 | set strMes to "エラー:PDF専用です" as text |
213 | set strName to (name of current application) as text |
214 | if strName is "osascript" then |
215 | tell application "SystemUIServer" to activate |
216 | else |
217 | tell current application to activate |
218 | end if |
219 | tell application "SystemUIServer" |
220 | activate |
221 | display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3 |
222 | end tell |
223 | tell application "SystemUIServer" to quit |
224 | refMe's NSLog("■:" & strMes) |
225 | return strMes |
226 | end if |
227 | else |
228 | set strMes to "エラー:PDF専用です" as text |
229 | set strName to (name of current application) as text |
230 | if strName is "osascript" then |
231 | tell application "SystemUIServer" to activate |
232 | else |
233 | tell current application to activate |
234 | end if |
235 | tell application "SystemUIServer" |
236 | activate |
237 | display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3 |
238 | end tell |
239 | tell application "SystemUIServer" to quit |
240 | refMe's NSLog("■:" & strMes) |
241 | return strMes |
242 | end if |
243 | end repeat |
244 | ####本処理 |
245 | repeat with itemPathURL in ocidDropPathURLArray |
246 | set ocidFileName to itemPathURL's lastPathComponent() |
247 | set strBaseFileName to ocidFileName's stringByDeletingPathExtension() as text |
248 | ###PW設定は複製したPDFに対して行うため保存先ディレクトリを作る |
249 | set strDirName to (strBaseFileName & "_PW設定済") |
250 | set ocidContainerDirURL to itemPathURL's URLByDeletingLastPathComponent() |
251 | set ocidDistFolderURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strDirName)) |
252 | set ocidSavePdfURL to (ocidDistFolderURL's URLByAppendingPathComponent:(ocidFileName)) |
253 | ####ファイル移動先 |
254 | set appFileManager to refMe's NSFileManager's defaultManager() |
255 | set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
256 | (ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)) |
257 | set listDone to (appFileManager's createDirectoryAtURL:(ocidDistFolderURL) withIntermediateDirectories:true attributes:ocidAttrDict |error| :(reference)) |
258 | if (item 1 of listDone) is true then |
259 | log "createDirectoryAtURL 正常処理" |
260 | else if (item 2 of listDone) ≠ (missing value) then |
261 | set strErrorNO to (item 2 of listDone)'s code() as text |
262 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
263 | refMe's NSLog("■:移動でエラー" & strErrorNO & strErrorMes) |
264 | return "createDirectoryAtURL エラーしました" & strErrorNO & strErrorMes |
265 | end if |
266 | |
267 | ####コピー |
268 | set listDone to (appFileManager's copyItemAtURL:(itemPathURL) toURL:(ocidSavePdfURL) |error| :(reference)) |
269 | if (item 1 of listDone) is true then |
270 | log "copyItemAtURL 正常処理" |
271 | else if (item 2 of listDone) ≠ (missing value) then |
272 | set strErrorNO to (item 2 of listDone)'s code() as text |
273 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
274 | refMe's NSLog("■:コピーでエラー" & strErrorNO & strErrorMes) |
275 | return "copyItemAtURL エラーしました" & strErrorNO & strErrorMes |
276 | end if |
277 | |
278 | #####PDFDocumentとして読み込み |
279 | set ocidActivDoc to (refMe's PDFDocument's alloc()'s initWithURL:(ocidSavePdfURL)) |
280 | ######################################## |
281 | #パスワード生成 UUIDを利用 |
282 | set strTextFileName to strBaseFileName & ".pw.txt" |
283 | set ocidTextFilePathURL to (ocidDistFolderURL's URLByAppendingPathComponent:strTextFileName) |
284 | #####生成したUUIDからハイフンを取り除く |
285 | set ocidUUIDString to (refMe's NSMutableString's alloc()'s initWithCapacity:0) |
286 | set ocidConcreteUUID to refMe's NSUUID's UUID() |
287 | (ocidUUIDString's setString:(ocidConcreteUUID's UUIDString())) |
288 | set ocidUUIDRange to (ocidUUIDString's rangeOfString:ocidUUIDString) |
289 | (ocidUUIDString's replaceOccurrencesOfString:("-") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidUUIDRange) |
290 | |
291 | set strOwnerPassword to ocidUUIDString as text |
292 | ##保存用テキストにする |
293 | set strTextFile to "所有者用Pw(教えてはいけないヤツ)\n" & strOwnerPassword & "\n" as text |
294 | if boolOpenPW is true then |
295 | #####生成したUUIDからハイフンを取り除く |
296 | set ocidUUIDString to (refMe's NSMutableString's alloc()'s initWithCapacity:0) |
297 | set ocidConcreteUUID to refMe's NSUUID's UUID() |
298 | (ocidUUIDString's setString:(ocidConcreteUUID's UUIDString())) |
299 | set ocidUUIDRange to (ocidUUIDString's rangeOfString:ocidUUIDString) |
300 | (ocidUUIDString's replaceOccurrencesOfString:("-") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidUUIDRange) |
301 | ##保存用テキストにする |
302 | set strUserPassword to ocidUUIDString as text |
303 | set strTextFile to strTextFile & "\n利用者用Pw(他者に教える場合はこちら↓)\n\n\n" as text |
304 | set strTextFile to strTextFile & "別に送付しましたPDFの開封パスワードです。\nコピペ時には改行が入らないように留意ください\n" as text |
305 | set strTextFile to strTextFile & "\n" & strUserPassword & "\n" as text |
306 | |
307 | end if |
308 | set ocidPWString to (refMe's NSString's stringWithString:strTextFile) |
309 | set ocidUUIDData to (ocidPWString's dataUsingEncoding:(refMe's NSUTF8StringEncoding)) |
310 | ##PWをテキストで保存する |
311 | set boolResults to (ocidUUIDData's writeToURL:ocidTextFilePathURL atomically:true) |
312 | ######################################## |
313 | #保存OPTION |
314 | set ocidOptionDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
315 | if boolOwnerPW = true then |
316 | (ocidOptionDict's setObject:(strOwnerPassword) forKey:(refMe's PDFDocumentOwnerPasswordOption)) |
317 | end if |
318 | if boolOpenPW = true then |
319 | ####開封パスワード |
320 | (ocidOptionDict's setObject:(strUserPassword) forKey:(refMe's PDFDocumentUserPasswordOption)) |
321 | end if |
322 | ############################## |
323 | ###セキュリティ設定 |
324 | ###PDFKitのバグ対策もかねて |
325 | log intAllowNo |
326 | #個別の依存でよくある誤りを修正しておく |
327 | if intAllowNo is 191 then |
328 | #注釈禁止の場合ドキュメント変更禁止が必要なので追加しておく |
329 | set intAllowNo to (intAllowNo - (refMe's PDFAllowsDocumentChanges as integer)) as integer |
330 | else if intAllowNo is 254 then |
331 | #低解像度印刷禁止の場合高解像度印刷禁止も必要なので追加しておく |
332 | set intAllowNo to (intAllowNo - (refMe's PDFAllowsHighQualityPrinting as integer)) as integer |
333 | else if intAllowNo is 247 then |
334 | #アッセンブリ禁止の場合はドキュメントに変更も禁止の必要がある |
335 | set intAllowNo to (intAllowNo - (refMe's PDFAllowsDocumentChanges as integer)) as integer |
336 | else if intAllowNo is 127 then |
337 | #フォーム入力禁止の場合はドキュメントに変更も禁止+注釈禁止の必要がある |
338 | set intAllowNo to (intAllowNo - (refMe's PDFAllowsDocumentChanges as integer) - (refMe's PDFAllowsCommenting as integer)) as integer |
339 | end if |
340 | log intAllowNo |
341 | (ocidOptionDict's setObject:(intAllowNo) forKey:(refMe's PDFDocumentAccessPermissionsOption)) |
342 | ######################################## |
343 | #保存 |
344 | set boolResults to (ocidActivDoc's writeToURL:(ocidSavePdfURL) withOptions:(ocidOptionDict)) |
345 | end repeat |
346 | ######################################## |
347 | #保存先を開く |
348 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
349 | set boolDone to appSharedWorkspace's openURL:(ocidDistFolderURL) |
350 | if boolDone is true then |
351 | log "openURL 正常処理" |
352 | else if boolDone is false then |
353 | refMe's NSLog("■:フォルダオープンでエラー") |
354 | log "openURL エラーしました" |
355 | end if |
356 | |
357 | end open |
AppleScriptで生成しました |
| 固定リンク