« [TCC]com.apple.TCC.configuration-profile-policy.ポイント | トップページ | [Keynote]用紙サイズを指定して新規書類を作る »

[Printer] PrinterProxyを作る

登録済みのプリンタの『PrinterProxy』を作成します。
プリンタIDnameとtDescriptionの両方を取得しないとダメなのが面倒です。
まぁPrinterProxyは無くても大丈夫(印刷できる)んですが
UIとしては、デザイナーさんとか?利用者は使いますからね。

20220327141224_1280x862


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

tell application "Finder"
####ライブラリフォルダ
set aliasUserLibDir to path to library folder from user domain as alias
set strUserLibDir to POSIX path of aliasUserLibDir as text
####プリンタフォルダ
set aliasUserPrinterDir to (folder "Printers" of folder aliasUserLibDir) as alias
set strUserPrinterDir to POSIX path of aliasUserPrinterDir as text
###プリンタプロキシフォルダを作る
set boolFolderExists to (exists of (folder "PrinterProxy" of folder aliasUserPrinterDir)) as boolean
if boolFolderExists is false then
make new folder at aliasUserPrinterDir with properties {name:"PrinterProxy", owner privileges:"read write", group privileges:"read write", everyones privileges:"read write", comment:"このフォルダは削除しても大丈夫です", description:"サンプルスクリプトが生成したフォルダです削除しても大丈夫です", label index:3}
end if
set aliasProxyDir to folder "PrinterProxy" of folder aliasUserPrinterDir as alias
set strProxyDir to POSIX path of aliasProxyDir as text
end tell

##############################
set thePrinterList to (do shell script "lpstat -e") as text
set AppleScript's text item delimiters to "\r"
set listPrinterList to every text item of thePrinterList as list
set AppleScript's text item delimiters to ""
set numCntPrinter to count of listPrinterList
set numChkPrNo to 1

repeat with objPrinterName in listPrinterList
####まずはプリンタのDescription名を取得する
set thePrinterDescription to (do shell script "lpstat -l -p \"" & objPrinterName & "\" | grep Description") as text
set thePrinterDescription to doReplace(thePrinterDescription, "\tDescription: ", "") as text
####プリンタプロキシへのパスを作っておく
set strAppPath to "" & strProxyDir & thePrinterDescription & ".app" as text
set aliasAppPath to POSIX file strAppPath
####すでにあるか?確認
tell application "Finder"
set boolAppPath to (exists aliasAppPath) as boolean
end tell
###なければ作る
if boolAppPath is false then
###メインのアプリケーションをコピーして
set strCommadnText to "cp -rf \"/System/Library/PrivateFrameworks/PrintingPrivate.framework/Versions/A/Plugins/PrinterProxy.app\" \"" & strProxyDir & thePrinterDescription & ".app\""
do shell script strCommadnText
###アイコンをつけます
set strCommadnText to "cp -rf \"/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/PrintCore.framework/Versions/A/Resources/GenericPostscriptPrinter.icns\" \"" & strProxyDir & thePrinterDescription & ".app/Contents/Resources/Printer.icns\""
do shell script strCommadnText
####設定ファイルパスを定義
set strXMLPath to "" & strProxyDir & thePrinterDescription & ".app/Contents/Resources/PrinterInfo.xml"
set aliasXMLPath to POSIX file strXMLPath
####プロパティリストとして新規作成
tell application "System Events"
set objPlist to make new property list file with properties {name:strXMLPath}
end tell
####項目を書き込み
tell application "System Events"
tell objPlist
make new property list item at end with properties {kind:string, name:"PrinterHost", value:"localhost"}
make new property list item at end with properties {kind:string, name:"PrinterID", value:"" & objPrinterName & ""}
make new property list item at end with properties {kind:string, name:"PrinterName", value:"" & thePrinterDescription & ""}
end tell
end tell
####plistからxmlに拡張子変更
tell application "Finder"
set strResourcesPath to ("" & strProxyDir & thePrinterDescription & ".app/Contents/Resources") as text
set aliasResourcesPath to POSIX file strResourcesPath as alias
set name of file "PrinterInfo.xml.plist" of folder aliasResourcesPath to "PrinterInfo.xml"
end tell
end if
#####出来上がり
set numChkPrNo to numChkPrNo + 1
end repeat
#####スクリプトメニューにエイリアスを作る
tell application "Finder"
set theFolderName to name of aliasProxyDir as text
set boolFolderAlias to exists of file theFolderName of folder (path to scripts folder from user domain)
if boolFolderAlias is false then
make new alias to folder aliasProxyDir at (path to scripts folder from user domain)
end if
end tell

to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace

|

« [TCC]com.apple.TCC.configuration-profile-policy.ポイント | トップページ | [Keynote]用紙サイズを指定して新規書類を作る »

Admin Printer」カテゴリの記事