[PDFKit]PDF選択ページを残して他のページを削除(別名保存)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # com.cocolog-nifty.quicktimer.icefloe |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "AppKit" |
009 | use framework "UniformTypeIdentifiers" |
010 | use framework "PDFKit" |
011 | use scripting additions |
012 | |
013 | property refMe : a reference to current application |
014 | |
015 | set appFileManager to refMe's NSFileManager's defaultManager() |
016 | |
017 | ############################# |
018 | ###ダイアログを前面に出す |
019 | set strName to (name of current application) as text |
020 | if strName is "osascript" then |
021 | tell application "Finder" to activate |
022 | else |
023 | tell current application to activate |
024 | end if |
025 | ############ デフォルトロケーション |
026 | set appFileManager to refMe's NSFileManager's defaultManager() |
027 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
028 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
029 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
030 | # |
031 | set listUTI to {"com.adobe.pdf"} |
032 | set strMes to ("【A】PDFファイルを選んでください") as text |
033 | set strPrompt to ("【A】PDFファイルを選んでください") as text |
034 | try |
035 | set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
036 | on error |
037 | log "エラーしました" |
038 | return "エラーしました" |
039 | end try |
040 | ##パス |
041 | set strFilePath to (POSIX path of aliasFilePath) as text |
042 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
043 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
044 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
045 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
046 | set ocidFileName to ocidBaseFilePathURL's lastPathComponent() |
047 | ##読み込み |
048 | #NSDATA |
049 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
050 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference) |
051 | if (item 2 of listResponse) = (missing value) then |
052 | set ocidReadData to (item 1 of listResponse) |
053 | else if (item 2 of listResponse) ≠ (missing value) then |
054 | set strErrorNO to (item 2 of listResponse)'s code() as text |
055 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
056 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
057 | return "エラーしました" & strErrorNO & strErrorMes |
058 | end if |
059 | #PDFDOC |
060 | set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithData:(ocidReadData) |
061 | set numCntPDFPage to ocidActivDoc's pageCount() |
062 | #Array |
063 | set ocidPageNoArrayM to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
064 | repeat with itemPageNo from 1 to (numCntPDFPage) by 1 |
065 | (ocidPageNoArrayM's addObject:(itemPageNo)) |
066 | end repeat |
067 | #ダイアログ |
068 | set listChoosePageNo to ocidPageNoArrayM as list |
069 | set strName to (name of current application) as text |
070 | if strName is "osascript" then |
071 | tell application "Finder" to activate |
072 | else |
073 | tell current application to activate |
074 | end if |
075 | set strTitle to "選んでください" as text |
076 | set strPrompt to "取り出すページ番号を選んでください" as text |
077 | try |
078 | set listResponse to (choose from list listChoosePageNo with title strTitle with prompt strPrompt default items (item 1 of listChoosePageNo) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) as list |
079 | on error |
080 | log "エラーしました" |
081 | return "エラーしました" |
082 | error "エラーしました" number -200 |
083 | end try |
084 | if listResponse = {} then |
085 | log "何も選択していない" |
086 | return |
087 | else if (item 1 of listResponse) is false then |
088 | return "キャンセルしました" |
089 | error "キャンセルしました" number -200 |
090 | end if |
091 | log listResponse |
092 | (* |
093 | PDFにはPDFDocumentとそれの下位にPDFPageがある |
094 | 注釈はPDFPage |
095 | メタデータやBookMarkはPDFDocumentに属しているので |
096 | それぞれ処理が必要 |
097 | ここでは |
098 | PDFDocumentを複製して不要なページを削除する手法を使う |
099 | ユーザーが選んだページ以外を削除する処理になる |
100 | *) |
101 | #ページを削除する事になるので後ろのページから処理 |
102 | repeat with itemPageNo from numCntPDFPage to 1 by -1 |
103 | if listResponse contains itemPageNo then |
104 | log "マッチ: " & itemPageNo |
105 | else |
106 | (ocidActivDoc's removePageAtIndex:(itemPageNo - 1)) |
107 | end if |
108 | end repeat |
109 | |
110 | ##bookmarkはROOTは残してリセット |
111 | set ocidOutLineRoot to ocidActivDoc's outlineRoot() |
112 | if ocidOutLineRoot ≠ (missing value) then |
113 | set numChild to ocidOutLineRoot's numberOfChildren() as integer |
114 | repeat with itemChildNo from (numChild - 1) to 0 by -1 |
115 | set ocidChild to (ocidOutLineRoot's childAtIndex:(itemChildNo)) |
116 | ocidChild's removeFromParent() |
117 | end repeat |
118 | end if |
119 | |
120 | #保存先パス |
121 | set strSaveExtension to ("") as text |
122 | repeat with itemPageNo in listResponse |
123 | set strSaveExtension to (strSaveExtension & itemPageNo & ".") as text |
124 | end repeat |
125 | set strSaveExtension to (strSaveExtension & "pdf") as text |
126 | set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:(strSaveExtension) |
127 | #保存オプション |
128 | set ocidOptionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
129 | ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentOwnerPasswordOption) |
130 | ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentUserPasswordOption) |
131 | ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentAccessPermissionsOption) |
132 | ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentBurnInAnnotationsOption) |
133 | ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentOptimizeImagesForScreenOption) |
134 | ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentSaveImagesAsJPEGOption) |
135 | ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentSaveTextFromOCROption) |
136 | #保存 |
137 | set boolDone to ocidActivDoc's writeToURL:(ocidSaveFilePathURL) withOptions:(ocidOptionDict) |
138 | |
139 | return boolDone |
AppleScriptで生成しました |
| 固定リンク