001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | 20241118 macOS15でエラーになりにくいように少し手直し |
005 | 20250406 macOS15.4でエラーになるのに対応した |
006 |
|
007 | 【注意】CUPSプリンタシステムを初期化します |
008 | プリンタをカスタマイズしている方は実行しないでください |
009 |
|
010 | AcrobatのPPDをGitからダウンロードするように変更しました |
011 |
|
012 | 余白0のAcraobatプリンタとDS TRUEFLOW_Jプリンタ(PS書き出し用)を |
013 | プリンタに登録します |
014 | いわゆるバーチャルプリンタです。 |
015 |
|
016 | com.cocolog-nifty.quicktimer.icefloe *) |
017 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
018 | use AppleScript version "2.8" |
019 | use framework "Foundation" |
020 | use framework "AppKit" |
021 | use framework "PrintKit" |
022 | use framework "UniformTypeIdentifiers" |
023 | use scripting additions |
024 |
|
025 | property refMe : a reference to current application |
026 |
|
027 | set appFileManager to refMe's NSFileManager's defaultManager() |
028 |
|
029 | ############################# |
030 | # 共有ディレクトリのPPDを廃止 |
031 | # makequeuesでエラーになるため |
032 | ############################# |
033 | #廃止予定ディレクトリパス |
034 | set strTmpDirPath to "/Users/Shared/Library/Printers" as text |
035 | set ocidTmpDirPathStr to refMe's NSString's stringWithString:(strTmpDirPath) |
036 | set ocidTmpDirPath to ocidTmpDirPathStr's stringByStandardizingPath() |
037 | set ocidTmpDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidTmpDirPath) isDirectory:(true) |
038 | #フォルダ生成 |
039 | set appFileManager to refMe's NSFileManager's defaultManager() |
040 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
041 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
042 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidTmpDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
043 | #注意喚起テキスト生成 |
044 | set strTmpFileName to ("このディレクトリは廃止になります.txt") as text |
045 | set strTmpFilePathURL to ocidTmpDirPathURL's URLByAppendingPathComponent:(strTmpFileName) isDirectory:(false) |
046 | set ocidTmpContentsString to refMe's NSString's stringWithString:("このディレクトリは廃止になります\n任意で削除してください") |
047 | set listDone to ocidTmpContentsString's writeToURL:(strTmpFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
048 |
|
049 | ############################# |
050 | ###AcrobatPPDの有無を確認 |
051 | ############################# |
052 | #ユーザードメインのPPD保存先 |
053 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
054 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
055 | set ocidExtractDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Printers/PPDs/Contents/Resources") isDirectory:(true) |
056 | # |
057 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
058 | set listDone to appFileManager's createDirectoryAtURL:(ocidExtractDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
059 | # |
060 | set ocidSavePPDDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:"Printers/PPDs/Contents/Resources/Acrobat" isDirectory:(true) |
061 | set ocidPPDFilePathURL to ocidSavePPDDirPathURL's URLByAppendingPathComponent:"ADPDF9J.PPD" isDirectory:(false) |
062 | ############ |
063 | set boolFileExists to (appFileManager's fileExistsAtPath:(ocidPPDFilePathURL's |path|()) isDirectory:false) |
064 |
|
065 | if boolFileExists is true then |
066 | log "PPDはインストール済み" |
067 | else if boolFileExists is false then |
068 | ############ない場合はダウンロード |
069 | #AcrobatPPDとTRUEFLOW_Jのみ |
070 | #set strZipFileURL to "https://force4u.cocolog-nifty.com/skywalker/files/acrobatppds.zip" |
071 | #Generic.ppd入り |
072 | #set strZipFileURL to "https://quicktimer.cocolog-nifty.com/icefloe/files/acrobatppds.zip" |
073 | #↑のGITHUB |
074 | set strZipFileURL to "https://github.com/force4u/AppleScript/raw/refs/heads/main/Script%20Menu/Printers/Admin/VirtualPrinter/acrobatppds.zip" |
075 | set ocidZipFilePath to (refMe's NSString's stringWithString:strZipFileURL) |
076 | set ocidZipFilePathURL to refMe's NSURL's alloc()'s initWithString:ocidZipFilePath |
077 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
078 | set listResponse to refMe's NSData's dataWithContentsOfURL:(ocidZipFilePathURL) options:(ocidOption) |error|:(reference) |
079 | if (item 2 of listResponse) = (missing value) then |
080 | log "NSData 正常処理" |
081 | set ocidZipData to (item 1 of listResponse) |
082 | else if (item 2 of listResponse) ≠ (missing value) then |
083 | set strErrorNO to (item 2 of listResponse)'s code() as text |
084 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
085 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
086 | return "エラーしました" & strErrorNO & strErrorMes |
087 | end if |
088 | #保存 |
089 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
090 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
091 | set ocidUUIDString to ocidUUID's UUIDString |
092 | set ocidSaveZipDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
093 | # |
094 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
095 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveZipDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
096 | #パス |
097 | set ocidSaveZipFilePathURL to ocidSaveZipDirPathURL's URLByAppendingPathComponent:"acrobatppds.zip" isDirectory:false |
098 | set ocidOption to (refMe's NSDataWritingAtomic) |
099 | set listDone to ocidZipData's writeToURL:(ocidSaveZipFilePathURL) options:(ocidOption) |error|:(reference) |
100 | if (item 1 of listDone) is true then |
101 | log "正常処理" |
102 | else if (item 2 of listDone) ≠ (missing value) then |
103 | set strErrorNO to (item 2 of listDone)'s code() as text |
104 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
105 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
106 | return "エラーしました" & strErrorNO & strErrorMes |
107 | end if |
108 | |
109 | ######################## |
110 | #### 解凍 |
111 | ######################## |
112 | set strZipFilePathURL to (ocidSaveZipFilePathURL's |path|()) as text |
113 | set strSaveDirPathURL to (ocidExtractDirPathURL's |path|()) as text |
114 | |
115 | ################ |
116 | #コマンド実行 |
117 | set strCommandText to ("/usr/bin/ditto -xk \"" & strZipFilePathURL & "\" \"" & strSaveDirPathURL & "\"") as text |
118 | set refResponse to doZshShellScript(strCommandText) |
119 | if refResponse is false then |
120 | return "ditto でエラーしました" |
121 | end if |
122 | |
123 | end if |
124 |
|
125 | ############################# |
126 | ###Acrobatプリンタの有無を確認 |
127 | ############################# |
128 |
|
129 | set strQueName to ("PsPrinterAcrobat9J") as text |
130 | set strModelName to ("PostScript書出用設定") as text |
131 | set strDescriptionName to ("Adobe PDF J 3018.101") as text |
132 | set ocidPPDFilePathURL to ocidSavePPDDirPathURL's URLByAppendingPathComponent:("ADPDF9J.PPD") isDirectory:(false) |
133 | set strPPDFilePath to (ocidPPDFilePathURL's |path|()) as text |
134 |
|
135 | ################ |
136 | ##リセット=削除 |
137 | set strCommandText to ("/usr/sbin/lpadmin -x \"" & strQueName & "\"") as text |
138 | set refResponse to doZshShellScript(strCommandText) |
139 |
|
140 | ################ |
141 | #プリンタ登録 |
142 | set strCommandText to ("/usr/sbin/lpadmin -p \"" & strQueName & "\" -E -v \"lpd://localhost/\" -P \"" & strPPDFilePath & "\" -D \"" & strQueName & "\" -L \"" & strModelName & "\" -o printer-is-shared=false") as text |
143 | set refResponse to doZshShellScript(strCommandText) |
144 | if refResponse is false then |
145 | return "lpadmin プリンタ登録でエラーしました" |
146 | end if |
147 |
|
148 | ################ |
149 | #プリンタ登録 |
150 | set strCommandText to ("/usr/sbin/cupsdisable \"" & strQueName & "\"") as text |
151 | set refResponse to doZshShellScript(strCommandText) |
152 | if refResponse is false then |
153 | return "cupsdisable でエラーしました" |
154 | end if |
155 |
|
156 | ############################# |
157 | ###PsPrinteTRUEFLOW_Jプリンタ |
158 | ############################# |
159 |
|
160 | set strQueName to ("PsPrinteTRUEFLOW_J") as text |
161 | set strModelName to ("PostScript書出用設定") as text |
162 | set strDescriptionName to ("DS TRUEFLOW_J V1.4") as text |
163 | set ocidPPDFilePathURL to ocidSavePPDDirPathURL's URLByAppendingPathComponent:("DS TRUEFLOW_J V1.4") isDirectory:(false) |
164 | set strPPDFilePath to (ocidPPDFilePathURL's |path|()) as text |
165 |
|
166 | ################ |
167 | #リセット=削除 |
168 | set strCommandText to ("/usr/sbin/lpadmin -x \"" & strQueName & "\"") as text |
169 | set refResponse to doZshShellScript(strCommandText) |
170 |
|
171 | ################ |
172 | #プリンタ登録 |
173 | set strCommandText to ("/usr/sbin/lpadmin -p \"" & strQueName & "\" -E -v \"lpd://localhost/\" -P \"" & strPPDFilePath & "\" -D \"" & strQueName & "\" -L \"" & strModelName & "\" -o printer-is-shared=false") as text |
174 | set refResponse to doZshShellScript(strCommandText) |
175 | if refResponse is false then |
176 | return "lpadmin プリンタ登録でエラーしました" |
177 | end if |
178 |
|
179 | ################ |
180 | #停止させる |
181 | set strCommandText to ("/usr/sbin/cupsdisable \"" & strQueName & "\"") as text |
182 | set refResponse to doZshShellScript(strCommandText) |
183 | if refResponse is false then |
184 | return "cupsdisable でエラーしました" |
185 | end if |
186 |
|
187 | ############################# |
188 | ###A:PsPrintersプリンタ削除 |
189 | ############################# |
190 | set strCommandText to ("/usr/sbin/lpadmin -x \"A:PsPrinters\"") as text |
191 | set refResponse to doZshShellScript(strCommandText) |
192 | if refResponse is false then |
193 | return "lpadmin プリンタ削除 でエラーしました" |
194 | end if |
195 |
|
196 |
|
197 | ############################# |
198 | ###プリンタプールに追加 |
199 | ############################# |
200 | set strCommandText to ("/usr/sbin/lpadmin -p \"PsPrinterAcrobat9J\" -c \"A:PsPrinters\"") as text |
201 | set refResponse to doZshShellScript(strCommandText) |
202 | if refResponse is false then |
203 | return "lpadmin プリンタプールに追加 でエラーしました" |
204 | end if |
205 |
|
206 | set strCommandText to ("/usr/sbin/lpadmin -p \"PsPrinteTRUEFLOW_J\" -c \"A:PsPrinters\"") as text |
207 | set refResponse to doZshShellScript(strCommandText) |
208 | if refResponse is false then |
209 | return "lpadmin プリンタプールに追加 でエラーしました" |
210 | end if |
211 |
|
212 |
|
213 | ############################# |
214 | ###プリンタプールの作成 |
215 | ############################# |
216 | set strCommandText to ("/usr/sbin/lpadmin -p \"A:PsPrinters\" -L \"Psプリンタのプール(class)\"") as text |
217 | set refResponse to doZshShellScript(strCommandText) |
218 | set strCommandText to ("/usr/sbin/lpadmin -p \"A:PsPrinters\" -o printer-is-shared=false") as text |
219 | set refResponse to doZshShellScript(strCommandText) |
220 | set strCommandText to ("/usr/sbin/lpadmin -p \"A:PsPrinters\" -u allow:all") as text |
221 | set refResponse to doZshShellScript(strCommandText) |
222 | set strCommandText to ("/usr/sbin/lpadmin -p \"A:PsPrinters\" -E") as text |
223 | set refResponse to doZshShellScript(strCommandText) |
224 | set strCommandText to ("/usr/sbin/cupsdisable \"A:PsPrinters\"") as text |
225 | set refResponse to doZshShellScript(strCommandText) |
226 |
|
227 |
|
228 | ############################# |
229 | ###デフォルトプリンタを指定する |
230 | ############################# |
231 | set strCommandText to ("/usr/sbin/lpadmin -d \"PsPrinterAcrobat9J\"") as text |
232 | set refResponse to doZshShellScript(strCommandText) |
233 |
|
234 |
|
235 | ############################# |
236 | ### 初期設定 PLIST 書き換え |
237 | ############################# |
238 | set ocidFalse to (refMe's NSNumber's numberWithBool:false) |
239 | ####設定ファイルのパス |
240 | set ocidLibrariesPathURLArray to (appFileManager's URLsForDirectory:(refMe's NSAllLibrariesDirectory) inDomains:(refMe's NSUserDomainMask)) |
241 | set ocidLibrariesPathURL to item 1 of ocidLibrariesPathURLArray |
242 | set ocidFilePathURL to ocidLibrariesPathURL's URLByAppendingPathComponent:"Preferences/org.cups.PrintingPrefs.plist" isDirectory:false |
243 | #####PLISTの内容を読み込んで |
244 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
245 | set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:ocidFilePathURL |error|:(reference) |
246 |
|
247 | set ocidPlistDict to item 1 of listReadPlistData |
248 | ocidPlistDict's setObject:ocidFalse forKey:("UseLastPrinter") |
249 | set ocidLastUsedPrinter to ocidPlistDict's objectForKey:("LastUsedPrinters") |
250 |
|
251 | if ocidLastUsedPrinter is not (missing value) then |
252 | ocidLastUsedPrinter's removeObjectAtIndex:0 |
253 | set recordItemDict to {Network:"192.168.0.1", PrinterID:"PsPrinterAcrobat9J"} as record |
254 | ocidLastUsedPrinter's addObject:(recordItemDict) |
255 | ###書き込み用にバイナリーデータに変換 |
256 | set ocidNSbplist to refMe's NSPropertyListBinaryFormat_v1_0 |
257 | set ocidPlistEditDataArray to refMe's NSPropertyListSerialization's dataWithPropertyList:ocidPlistDict format:(ocidNSbplist) options:0 |error|:(reference) |
258 | set ocidPlistEditData to item 1 of ocidPlistEditDataArray |
259 | set boolWritetoUrlArray to ocidPlistEditData's writeToURL:(ocidFilePathURL) options:0 |error|:(reference) |
260 | end if |
261 | ############################# |
262 | #cupsctl プリンタ共有停止 |
263 | set strCommandText to ("/usr/sbin/cupsctl --no-share-printers") as text |
264 | set refResponse to doZshShellScript(strCommandText) |
265 |
|
266 |
|
267 | ############################# |
268 | #CUPS WEBインターフェイスを有効化 |
269 | set strCommandText to ("/usr/sbin/cupsctl WebInterface=YES") as text |
270 | set refResponse to doZshShellScript(strCommandText) |
271 |
|
272 | ############################# |
273 | #makequeues USB リバインド |
274 | set strCommandText to ("/System/Library/SystemConfiguration/PrinterNotifications.bundle/Contents/MacOS/makequeues -r") as text |
275 | set refResponse to doZshShellScript(strCommandText) |
276 |
|
277 | #makequeues PPD リバインド |
278 | set strCommandText to ("/System/Library/SystemConfiguration/PrinterNotifications.bundle/Contents/MacOS/makequeues -h") as text |
279 | set refResponse to doZshShellScript(strCommandText) |
280 |
|
281 | #makequeues LPD リバインド |
282 | set strCommandText to ("/System/Library/SystemConfiguration/PrinterNotifications.bundle/Contents/MacOS/makequeues -k lpd") as text |
283 | set refResponse to doZshShellScript(strCommandText) |
284 |
|
285 | #makequeues IPP リバインド |
286 | set strCommandText to ("/System/Library/SystemConfiguration/PrinterNotifications.bundle/Contents/MacOS/makequeues -k ipp") as text |
287 | set refResponse to doZshShellScript(strCommandText) |
288 |
|
289 | #makequeues IPP リバインド |
290 | set strCommandText to ("\"/System/Library/Image Capture/Tools/use_updated_image_capture_software\"") as text |
291 | set refResponse to doZshShellScript(strCommandText) |
292 |
|
293 | ############################# |
294 | #CFPreferencesを再起動 |
295 | set strCommandText to ("/usr/bin/killall -HUP cfprefsd") as text |
296 | set refResponse to doZshShellScript(strCommandText) |
297 |
|
298 | ############################# |
299 | #CUPSプロセスを再起動 |
300 | set strCommandText to ("/usr/bin/killall -HUP com.apple.PrintKit.PrinterTool") as text |
301 | set refResponse to doZshShellScript(strCommandText) |
302 |
|
303 | #### |
304 | set strCommandText to ("/usr/bin/killall -HUP PrintUITool") as text |
305 | set refResponse to doZshShellScript(strCommandText) |
306 |
|
307 | #### |
308 | set strCommandText to ("/usr/bin/killall -HUP printtool") as text |
309 | set refResponse to doZshShellScript(strCommandText) |
310 |
|
311 |
|
312 | ############################# |
313 | #プリンタパネルを開く |
314 | tell application id "com.apple.systempreferences" to quit |
315 | delay 1 |
316 | set appWorkspace to refMe's NSWorkspace's sharedWorkspace() |
317 | set ocidOpenURL to refMe's NSURL's URLWithString:"x-apple.systempreferences:com.apple.Print-Scan-Settings.extension" |
318 | appWorkspace's openURL:ocidOpenURL |
319 |
|
320 | ############################# |
321 | #CUPSを開く |
322 | tell application "Safari" |
323 | activate |
324 | make new document with properties {name:"localhost:631"} |
325 | tell front document |
326 | open location "http://localhost:631/printers/" |
327 | end tell |
328 | end tell |
329 |
|
330 | ############################# |
331 | #プリントセンターを開く |
332 | tell application id "com.apple.printcenter" to activate |
333 |
|
334 | ############################# |
335 | #チェック |
336 | set ocidPrinterNameArray to refMe's NSPrinter's printerNames() |
337 | repeat with itemPrinterName in ocidPrinterNameArray |
338 | set ocidPsPrinter to (refMe's NSPrinter's printerWithName:(itemPrinterName)) |
339 | set strPrinterName to ocidPsPrinter's |name|() as text |
340 | log "Name:" & strPrinterName |
341 | set strPrinterType to ocidPsPrinter's type() as text |
342 | log "Type:" & strPrinterType |
343 | set strPrinterLevel to ocidPsPrinter's languageLevel() as text |
344 | log "Level:" & strPrinterLevel |
345 | set strPrinterDescription to ocidPsPrinter's deviceDescription() as record |
346 | set ocidDescriptionDict to refMe's NSMutableDictionary's alloc()'s init() |
347 | (ocidDescriptionDict's setDictionary:(strPrinterDescription)) |
348 | set ocidAllKeys to ocidDescriptionDict's allKeys() |
349 | repeat with itemkey in ocidAllKeys |
350 | set ocidValue to (ocidDescriptionDict's valueForKey:(itemkey)) |
351 | log ("Description:" & itemkey & " value: " & ocidValue & "") as text |
352 | end repeat |
353 | #この値はDeprecatedなのでこれがエラーするようになったら |
354 | #仕様が変わったって事になります |
355 | log ocidPsPrinter's isColor() as boolean |
356 | log "---\r" |
357 | |
358 | end repeat |
359 |
|
360 | ########################## |
361 | # 【通常】ZSH 実行 |
362 | to doZshShellScript(argCommandText) |
363 | set strCommandText to argCommandText as text |
364 | log "\r" & strCommandText & "\r" |
365 | set strExec to ("/bin/zsh -c '" & strCommandText & "'") as text |
366 | log "\r" & strExec & "\r" |
367 | ########## |
368 | #コマンド実行 |
369 | try |
370 | log "コマンド開始" |
371 | set strResnponse to (do shell script strExec) as text |
372 | log "コマンド終了" |
373 | on error |
374 | return false |
375 | end try |
376 | return strResnponse |
377 | end doZshShellScript |