001 | #!/usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | # Acrobat不要 細かい設定は移動しないので印刷用 |
---|
004 | # ページの回転は考慮している |
---|
005 | #左ページが先 右ページが後 |
---|
006 | # com.cocolog-nifty.quicktimer.icefloe |
---|
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
008 | ##自分環境がos12なので2.8にしているだけです |
---|
009 | use AppleScript version "2.8" |
---|
010 | use framework "Foundation" |
---|
011 | use framework "AppKit" |
---|
012 | use framework "PDFKit" |
---|
013 | use scripting additions |
---|
014 | |
---|
015 | property refMe : a reference to current application |
---|
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 | set listUTI to {"com.adobe.pdf"} |
---|
031 | set strMes to ("PDF ファイルを選んでください") as text |
---|
032 | set strPrompt to ("PDFファイルを選んでください") as text |
---|
033 | try |
---|
034 | 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 |
---|
035 | on error |
---|
036 | log "エラーしました" |
---|
037 | return "エラーしました" |
---|
038 | end try |
---|
039 | set strFilePath to (POSIX path of aliasFilePath) as text |
---|
040 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
---|
041 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
---|
042 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
---|
043 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
---|
044 | set ocidFileExtension to ocidFilePathURL's pathExtension() |
---|
045 | set ocidPrefixName to ocidFileName's stringByDeletingPathExtension |
---|
046 | set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
---|
047 | |
---|
048 | ################################### |
---|
049 | ###ファイル名 |
---|
050 | set strPrefixName to ocidPrefixName as text |
---|
051 | set strFileExtension to ocidFileExtension as text |
---|
052 | set strDefaultName to (strPrefixName & ".見開き分割." & strFileExtension) as text |
---|
053 | set strPromptText to "保存ファイル名前を決めてください" |
---|
054 | set strMesText to "保存ファイル名前を決めてください" |
---|
055 | set aliasDefaultLocation to (ocidContainerDirURL's absoluteURL) as alias |
---|
056 | ####実在しない『はず』なのでas «class furl»で |
---|
057 | set aliasSaveFilePath to (choose file name strMesText default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl» |
---|
058 | set strSaveFilePath to (POSIX path of aliasSaveFilePath) as text |
---|
059 | set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath) |
---|
060 | set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath() |
---|
061 | set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false) |
---|
062 | ###拡張子取得 |
---|
063 | set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text |
---|
064 | ###ダイアログで拡張子を取っちゃった時対策 |
---|
065 | if strFileExtensionName is not strFileExtension then |
---|
066 | set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:strFileExtension |
---|
067 | end if |
---|
068 | |
---|
069 | |
---|
070 | |
---|
071 | ###保存するPDF |
---|
072 | set ocidSavePDFdoc to refMe's PDFDocument's alloc()'s init() |
---|
073 | ###元になるPDF |
---|
074 | ##NSDATAに読み込む |
---|
075 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
---|
076 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference) |
---|
077 | if (item 2 of listResponse) = (missing value) then |
---|
078 | log "正常処理" |
---|
079 | set ocidReadData to (item 1 of listResponse) |
---|
080 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
081 | set strErrorNO to (item 2 of listResponse)'s code() as text |
---|
082 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
---|
083 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
084 | return "エラーしました" & strErrorNO & strErrorMes |
---|
085 | end if |
---|
086 | |
---|
087 | ################################ |
---|
088 | set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithData:(ocidReadData) |
---|
089 | set numCntPage to ocidActivDoc's pageCount() |
---|
090 | ## |
---|
091 | set numSetPageNo to 0 as integer |
---|
092 | |
---|
093 | repeat with itemReadPageNo from 0 to (numCntPage - 1) by 1 |
---|
094 | #ページ取り出し |
---|
095 | set ocidReadPageR to (ocidActivDoc's pageAtIndex:(itemReadPageNo)) |
---|
096 | set ocidReadPageL to (ocidActivDoc's pageAtIndex:(itemReadPageNo)) |
---|
097 | #保存用のPDFにインサートして |
---|
098 | (ocidSavePDFdoc's insertPage:(ocidReadPageR) atIndex:(numSetPageNo)) |
---|
099 | set numSetPageNo to (numSetPageNo + 1) as integer |
---|
100 | (ocidSavePDFdoc's insertPage:(ocidReadPageL) atIndex:(numSetPageNo)) |
---|
101 | set numSetPageNo to (numSetPageNo + 1) as integer |
---|
102 | end repeat |
---|
103 | set boolDone to (ocidSavePDFdoc's writeToURL:(ocidSaveFilePathURL)) |
---|
104 | if boolDone is true then |
---|
105 | log "正常処理" |
---|
106 | else if boolDone is false then |
---|
107 | return false |
---|
108 | end if |
---|
109 | |
---|
110 | |
---|
111 | ################################ |
---|
112 | set ocidSavePDFdoc to (missing value) |
---|
113 | ##NSDATAに読み込む |
---|
114 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
---|
115 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference) |
---|
116 | if (item 2 of listResponse) = (missing value) then |
---|
117 | log "正常処理" |
---|
118 | set ocidSaveData to (item 1 of listResponse) |
---|
119 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
120 | set strErrorNO to (item 2 of listResponse)'s code() as text |
---|
121 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
---|
122 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
123 | return "エラーしました" & strErrorNO & strErrorMes |
---|
124 | end if |
---|
125 | ### |
---|
126 | set ocidSavePDFdoc to refMe's PDFDocument's alloc()'s initWithData:(ocidSaveData) |
---|
127 | set numCntPage to ocidSavePDFdoc's pageCount() |
---|
128 | ### |
---|
129 | repeat with itemReadPageNo from 0 to (numCntPage - 1) by 2 |
---|
130 | ##########R |
---|
131 | #保存用のPDFからPDFページを取り出してサイズセット |
---|
132 | set ocidSavePageR to (ocidSavePDFdoc's pageAtIndex:(itemReadPageNo)) |
---|
133 | #表示サイズCROPを取得 |
---|
134 | set ocidSaveRectR to (ocidSavePageR's boundsForBox:(refMe's kPDFDisplayBoxCropBox)) |
---|
135 | set numOriginRx to refMe's NSMinX(ocidSaveRectR) |
---|
136 | set numOriginRy to refMe's NSMinY(ocidSaveRectR) |
---|
137 | set numPageRw to refMe's NSWidth(ocidSaveRectR) |
---|
138 | set numPageRh to refMe's NSHeight(ocidSaveRectR) |
---|
139 | # |
---|
140 | set numPageRotationR to ocidSavePageR's |rotation|() |
---|
141 | log numPageRotationR |
---|
142 | if numPageRh > numPageRw then |
---|
143 | if numPageRotationR = 0 then |
---|
144 | set ocidSetRectR to refMe's NSRect's NSMakeRect(numOriginRx, numOriginRy, numPageRw, (numPageRh / 2)) |
---|
145 | else if numPageRotationR = 90 then |
---|
146 | set ocidSetRectR to refMe's NSRect's NSMakeRect(numOriginRx, numOriginRy, numPageRw, (numPageRh / 2)) |
---|
147 | else if numPageRotationR = 180 then |
---|
148 | set ocidSetRectR to refMe's NSRect's NSMakeRect(numOriginRx, numOriginRy, numPageRw, (numPageRh / 2)) |
---|
149 | else if numPageRotationR = 270 then |
---|
150 | set numSetOriginRy to ((numPageRh / 2)) as number |
---|
151 | set ocidSetRectR to refMe's NSRect's NSMakeRect(numOriginRx, numSetOriginRy, numPageRw, (numPageRh / 2)) |
---|
152 | else if numPageRotationR = 360 then |
---|
153 | set ocidSetRectR to refMe's NSRect's NSMakeRect(numOriginRx, numOriginRy, numPageRw, (numPageRh / 2)) |
---|
154 | end if |
---|
155 | else |
---|
156 | if numPageRotationR = 0 then |
---|
157 | set ocidSetRectR to refMe's NSRect's NSMakeRect(numOriginRx, numOriginRy, (numPageRw / 2), numPageRh) |
---|
158 | else if numPageRotationR = 90 then |
---|
159 | set ocidSetRectR to refMe's NSRect's NSMakeRect(numOriginRx, numOriginRy, (numPageRw / 2), numPageRh) |
---|
160 | else if numPageRotationR = 180 then |
---|
161 | set numSetOriginRx to ((numPageRw / 2)) as number |
---|
162 | set ocidSetRectR to refMe's NSRect's NSMakeRect(numSetOriginRx, numOriginRy, (numPageRw / 2), numPageRh) |
---|
163 | else if numPageRotationR = 270 then |
---|
164 | set ocidSetRectR to refMe's NSRect's NSMakeRect(numOriginRx, numOriginRy, (numPageRw / 2), numPageRh) |
---|
165 | else if numPageRotationR = 360 then |
---|
166 | set ocidSetRectR to refMe's NSRect's NSMakeRect(numOriginRx, numOriginRy, (numPageRw / 2), numPageRh) |
---|
167 | end if |
---|
168 | end if |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | # |
---|
174 | (ocidSavePageR's setBounds:(ocidSetRectR) forBox:(refMe's kPDFDisplayBoxCropBox)) |
---|
175 | (ocidSavePageR's setBounds:(ocidSetRectR) forBox:(refMe's kPDFDisplayBoxBleedBox)) |
---|
176 | (ocidSavePageR's setBounds:(ocidSetRectR) forBox:(refMe's kPDFDisplayBoxTrimBox)) |
---|
177 | (ocidSavePageR's setBounds:(ocidSetRectR) forBox:(refMe's kPDFDisplayBoxMediaBox)) |
---|
178 | |
---|
179 | #########L |
---|
180 | set ocidSavePageL to (ocidSavePDFdoc's pageAtIndex:(itemReadPageNo + 1)) |
---|
181 | #表示サイズCROPを取得 |
---|
182 | set ocidSaveRectL to (ocidSavePageL's boundsForBox:(refMe's kPDFDisplayBoxCropBox)) |
---|
183 | |
---|
184 | set numOriginLx to refMe's NSMinX(ocidSaveRectL) |
---|
185 | set numOriginLy to refMe's NSMinY(ocidSaveRectL) |
---|
186 | set numPageLw to refMe's NSWidth(ocidSaveRectL) |
---|
187 | set numPageLh to refMe's NSHeight(ocidSaveRectL) |
---|
188 | set numPageRotationL to ocidSavePageL's |rotation|() |
---|
189 | # |
---|
190 | if numPageLh > numPageLw then |
---|
191 | if numPageRotationL = 0 then |
---|
192 | set numSetOriginLy to ((numPageLh / 2)) as number |
---|
193 | set ocidSetRectL to refMe's NSLect's NSMakeRect(numOriginLx, numSetOriginLy, numPageLw, (numPageLh / 2)) |
---|
194 | else if numPageRotationL = 360 then |
---|
195 | set numSetOriginLy to ((numPageLh / 2)) as number |
---|
196 | set ocidSetRectL to refMe's NSLect's NSMakeRect(numOriginLx, numSetOriginLy, numPageLw, (numPageLh / 2)) |
---|
197 | else if numPageRotationL = 90 then |
---|
198 | set numSetOriginLy to ((numPageLh / 2)) as number |
---|
199 | set ocidSetRectL to refMe's NSLect's NSMakeRect(numOriginLx, numSetOriginLy, numPageLw, (numPageLh / 2)) |
---|
200 | else if numPageRotationL = 180 then |
---|
201 | set numSetOriginLy to ((numPageLh / 2)) as number |
---|
202 | set ocidSetRectL to refMe's NSLect's NSMakeRect(numOriginLx, numSetOriginLy, numPageLw, (numPageLh / 2)) |
---|
203 | else if numPageRotationL = 270 then |
---|
204 | set ocidSetRectL to refMe's NSLect's NSMakeRect(numOriginLx, numOriginLy, numPageLw, (numPageLh / 2)) |
---|
205 | end if |
---|
206 | else |
---|
207 | if numPageRotationL = 0 then |
---|
208 | set numSetOriginLx to ((numPageLw / 2)) as number |
---|
209 | set ocidSetRectL to refMe's NSLect's NSMakeRect(numSetOriginLx, numOriginLy, (numPageLw / 2), numPageLh) |
---|
210 | else if numPageRotationL = 360 then |
---|
211 | set numSetOriginLx to ((numPageLw / 2)) as number |
---|
212 | set ocidSetRectL to refMe's NSLect's NSMakeRect(numSetOriginLx, numOriginLy, (numPageLw / 2), numPageLh) |
---|
213 | else if numPageRotationL = 90 then |
---|
214 | set numSetOriginLx to ((numPageLw / 2)) as number |
---|
215 | set ocidSetRectL to refMe's NSLect's NSMakeRect(numSetOriginLx, numOriginLy, (numPageLw / 2), numPageLh) |
---|
216 | else if numPageRotationL = 180 then |
---|
217 | set ocidSetRectL to refMe's NSLect's NSMakeRect(numOriginLx, numOriginLy, (numPageLw / 2), numPageLh) |
---|
218 | else if numPageRotationL = 270 then |
---|
219 | set numSetOriginLx to ((numPageLw / 2)) as number |
---|
220 | set ocidSetRectL to refMe's NSLect's NSMakeRect(numSetOriginLx, numOriginLy, (numPageLw / 2), numPageLh) |
---|
221 | end if |
---|
222 | end if |
---|
223 | log ocidSetRectL |
---|
224 | (ocidSavePageL's setBounds:(ocidSetRectL) forBox:(refMe's kPDFDisplayBoxCropBox)) |
---|
225 | (ocidSavePageL's setBounds:(ocidSetRectL) forBox:(refMe's kPDFDisplayBoxBleedBox)) |
---|
226 | (ocidSavePageL's setBounds:(ocidSetRectL) forBox:(refMe's kPDFDisplayBoxTrimBox)) |
---|
227 | (ocidSavePageL's setBounds:(ocidSetRectL) forBox:(refMe's kPDFDisplayBoxMediaBox)) |
---|
228 | |
---|
229 | end repeat |
---|
230 | |
---|
231 | set boolDone to (ocidSavePDFdoc's writeToURL:(ocidSaveFilePathURL)) |
---|
232 | if boolDone is true then |
---|
233 | log "正常処理" |
---|
234 | else if boolDone is false then |
---|
235 | return false |
---|
236 | end if |
---|