Admin Printer

[makequeues]PPDから仮想プリンタ作成 macOS14のポンコツ対応(仮)


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
PPDファイルから仮想プリンタを登録します
macOS14.4.1対応
com.cocolog-nifty.quicktimer.icefloe
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

###ダイアログ
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSLocalDomainMask))
set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
set ocidContainerDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Printers/PPDs/Contents/Resources") isDirectory:(true)
set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
#
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set listUTI to {"public.data", "public.text", "dyn.ah62d4rv4ge81a6de", "org.gnu.gnu-zip-archive"} as list
set strMes to ("PPDファイルを選んでください") as text
set strPrompt to ("PPDファイルを選んでください") as text
try
  set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasContainerDirPath) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
##
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
set ocidFileName to ocidFilePathURL's lastPathComponent()
set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
set strExtensionName to ocidFilePathURL's pathExtension() as text
##
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
set ocidTmpDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Application Support/Adobe/Adobe PDF/PPD") isDirectory:(true)
set ocidTmpDirPath to ocidTmpDirPathURL's |path|
##
set boolDirExists to appFileManager's fileExistsAtPath:(ocidTmpDirPath) isDirectory:(true)
if boolDirExists is false then
  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
  set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidTmpDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
end if
if strExtensionName is "gz" then
  set ocidTmpFilePath to ocidTmpDirPath's stringByAppendingPathComponent:(ocidBaseFileName)
  set ocidPPDFilePath to ocidTmpFilePath's stringByAppendingPathExtension:("ppd")
  set boolFileExists to appFileManager's fileExistsAtPath:(ocidPPDFilePath) isDirectory:(false)
  if boolFileExists is false then
    set strCommandText to ("/usr/bin/gzip -d -c \"" & (ocidFilePath as text) & "\" > \"" & (ocidPPDFilePath as text) & "\"") as text
    try
do shell script strCommandText
    end try
  end if
  set ocidPPDFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPPDFilePath) isDirectory:false)
else
  set ocidPPDFilePathURL to ocidFilePathURL
end if

##
set listReadStings to refMe's NSString's alloc()'s initWithContentsOfURL:(ocidPPDFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
if (item 2 of listReadStings) ≠ (missing value) then
log (item 2 of listReadStings)'s localizedDescription() as text
return "PPDファイルの読み込みに失敗しました"
else
  set ocidReadStrings to (item 1 of listReadStings)
end if
##改行をUNIXに強制
set ocidReadStringsM to ocidReadStrings's stringByReplacingOccurrencesOfString:("\r\n") withString:("\n")
set ocidReadStringsM to ocidReadStringsM's stringByReplacingOccurrencesOfString:("\r") withString:("\n")
##
set ocidCharSet to refMe's NSCharacterSet's newlineCharacterSet()
set ocidPpdLineArray to ocidReadStringsM's componentsSeparatedByCharactersInSet:(ocidCharSet)
##
repeat with itemArray in ocidPpdLineArray
  set boolContain to (itemArray's containsString:("PCFileName")) as boolean
  if boolContain is true then
    set ocidTempArray to (itemArray's componentsSeparatedByString:("\""))
    set ocidPCFileName to (ocidTempArray's objectAtIndex:(1))
    set ocidPCFileNameTmp to (ocidPCFileName's stringByReplacingOccurrencesOfString:(".PPD") withString:(""))
    set strPCFileName to (ocidPCFileNameTmp's stringByReplacingOccurrencesOfString:(".ppd") withString:("")) as text
  end if
  set boolContain to (itemArray's containsString:("*ModelName:")) as boolean
  if boolContain is true then
    set ocidTempArray to (itemArray's componentsSeparatedByString:("\""))
    set strModelName to (ocidTempArray's objectAtIndex:(1)) as text
  end if
  set boolContain to (itemArray's containsString:("*NickName:")) as boolean
  if boolContain is true then
    set ocidTempArray to (itemArray's componentsSeparatedByString:("\""))
    set strNickName to (ocidTempArray's objectAtIndex:(1)) as text
  end if
  set boolContain to (itemArray's containsString:("*ShortNickName:")) as boolean
  if boolContain is true then
    set ocidTempArray to (itemArray's componentsSeparatedByString:("\""))
    set strShortNickName to (ocidTempArray's objectAtIndex:(1)) as text
  end if
end repeat
if strModelName is "" then
  set strModelName to strNickName as text
end if
##
set ocidShortPPDName to ocidBaseFileName's stringByReplacingOccurrencesOfString:(" ") withString:("")
##
set strBinPath to ("/System/Library/SystemConfiguration/PrinterNotifications.bundle/Contents/MacOS/makequeues") as text
set strURL to ("lpd://localhost:515/") as text
set strCommandText to ("\"" & strBinPath & "\" -q \"" & (ocidShortPPDName as text) & "\" -d \"" & strFilePath & "\" -v \"" & strURL & "\"") as text
log strCommandText
try
do shell script strCommandText
on error
return "makequeuesでエラーになりました"
end try
######ローケーション(場所)はmakequeuesで設定できないのでlpadminも使う
##set strCommandText to ("\"/usr/sbin/lpadmin\" -p \"" & (ocidShortPPDName as text) & "\" -D \"" & (ocidShortPPDName as text) & "\" -L \"" & strModelName & "\" -o printer-is-shared=false") as text
set strCommandText to ("\"/usr/sbin/lpadmin\" -p \"" & (ocidShortPPDName as text) & "\" -L \"" & strModelName & "\" -o printer-is-shared=false") as text
log strCommandText
try
do shell script strCommandText
  set strCommandText to ("/usr/sbin/cupsdisable \"" & (ocidShortPPDName as text) & "\"") as text
  try
do shell script strCommandText
  end try
on error
return "lpadminでエラーになりました"
end try



|

指定プリンタの用紙をカスタム用紙設定する(仮)


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
プリンタの用紙サイズの取得してカスタム用紙設定する
フジのプリンタしか確認していないので
他社のプリンタでは動作しないかもしれない
PPDの記述方法に依存があるため
com.cocolog-nifty.quicktimer.icefloe
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

set ocidPaperDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0

#プリンタを取得
set ocidPrinterArray to refMe's NSPrinter's printerNames()
set listPrinterArray to ocidPrinterArray as list
###ダイアログを前面に出す
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
###
set strTitle to ("選んでください") as text
set strPrompt to ("プリンタ名を選んでください") as text
try
  set listResponse to (choose from list listPrinterArray with title strTitle with prompt strPrompt default items (item 1 of listPrinterArray) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしましたA"
else if (item 1 of listResponse) is "キャンセル" then
return "キャンセルしましたB"
else
  set strPrinterName to (item 1 of listResponse) as text
end if


set ocidPrinter to (refMe's NSPrinter's printerWithName:(strPrinterName))
(*
この方法はmacOS14の不具合
#プリンタの名前
set strPrinterName to ocidPrinter's |name| as text
log "name:" & ocidPrinter's |name| as text
log "type:" & ocidPrinter's type as text
set ocidDescription to ocidPrinter's deviceDescription()
log "deviceDescription:" & (ocidDescription's valueForKey:("deviceDescriptionNSDeviceIsPrinter")) as text
######
*)

##
#プリンタにセットされている用紙名を取得
set strCommandText to ("/usr/bin/lpoptions -p \"" & strPrinterName & "\" -l | grep PageSize") as text
set strResponse to (do shell script strCommandText) as text
#コマンド戻り値を整形して用紙名のリストにする
set ocidResponse to (refMe's NSString's stringWithString:(strResponse))
set ocidReplacedStrings to (ocidResponse's stringByReplacingOccurrencesOfString:("PageSize/Media Size: ") withString:(""))
set ocidReplacedStrings to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:("*") withString:(""))
set ocidChrSet to (refMe's NSCharacterSet's characterSetWithCharactersInString:(" "))
set ocidPaperNameArray to (ocidReplacedStrings's componentsSeparatedByCharactersInSet:(ocidChrSet))
#用紙名の数だけ繰り返し
repeat with itemPaperName in ocidPaperNameArray
  #プリンタから用紙名を指定してサイズを取得
  set recordPaperSize to (ocidPrinter's pageSizeForPaper:(itemPaperName))
  set numWidth to (width of recordPaperSize) as text
  set numHeight to (height of recordPaperSize) as text
log recordPaperSize as list
log (itemPaperName as text) & ": " & numWidth & "x" & numHeight
  set listSetValuePaperSize to {numWidth, numHeight} as list
  if (numWidth as text) is not "0.0" then
(ocidPaperDict's setObject:(listSetValuePaperSize) forKey:(itemPaperName))
  end if
end repeat
#end repeat

set ocidAllKeys to ocidPaperDict's allKeys()
set ocidAllKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidAllKeyArray's addObjectsFromArray:(ocidAllKeys)
#並び替え
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:("localizedStandardCompare:")
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidAllKeyArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
##
set listAllKeys to ocidSortedArray as list
###ダイアログを前面に出す
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
  # tell application id "com.apple.appkit.xpc.openAndSavePanelService" to activate
else
  tell current application to activate
end if
###
set strTitle to ("選んでください") as text
set strPrompt to ("用紙名を選んでください\n用紙サイズを戻します") as text
try
  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 "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしましたA"
else if (item 1 of listResponse) is "キャンセル" then
return "キャンセルしましたB"
else
  set strPaperName to (item 1 of listResponse) as text
end if

set listGetValue to ocidPaperDict's objectForKey:(strPaperName)

set strPaperWpt to listGetValue's firstObject() as text
set strPaperHpt to listGetValue's lastObject() as text

########################
##ここからPPDF解析
set strPpdFilePath to ("/private/etc/cups/ppd/" & strPrinterName & ".ppd") as text
set ocidPpdFilePathStr to refMe's NSString's stringWithString:(strPpdFilePath)
set ocidPpdFilePath to ocidPpdFilePathStr's stringByStandardizingPath()
set ocidPpdFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPpdFilePath) isDirectory:false)
##
set listReadStrings to refMe's NSString's alloc()'s initWithContentsOfURL:(ocidPpdFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
set ocidReadStrings to (item 1 of listReadStrings)

set ocidCharSet to refMe's NSCharacterSet's newlineCharacterSet()
set ocidPpdLineArray to ocidReadStrings's componentsSeparatedByCharactersInSet:(ocidCharSet)
#
repeat with itemArray in ocidPpdLineArray
  set boolImageableArea to (itemArray's containsString:("*ImageableArea")) as boolean
  # log boolImageableArea
  # log itemArray as text
  if boolImageableArea is true then
    set ocidItemString to (itemArray's stringByReplacingOccurrencesOfString:("*ImageableArea ") withString:(""))
    set ocidCharSet to (refMe's NSMutableCharacterSet's characterSetWithCharactersInString:("/"))
(ocidCharSet's addCharactersInString:(":"))
    set ocidPaperNameArray to (ocidItemString's componentsSeparatedByCharactersInSet:(ocidCharSet))
    set strGetPaperName to (ocidPaperNameArray's firstObject()) as text
    if strPaperName is strGetPaperName then
log itemArray as text
      set ocidAreaRectString to ocidPaperNameArray's lastObject()
      set ocidRectTmpArray to (ocidAreaRectString's componentsSeparatedByString:("\""))
      set ocidRectString to (ocidRectTmpArray's objectAtIndex:(1))
      set ocidRectArray to (ocidRectString's componentsSeparatedByString:(" "))
      set ocidImageableRect to refMe's NSRect's NSMakeRect(((ocidRectArray's objectAtIndex:(0))'s floatValue), ((ocidRectArray's objectAtIndex:(1))'s floatValue), ((ocidRectArray's objectAtIndex:(2))'s floatValue), ((ocidRectArray's objectAtIndex:(3))'s floatValue))
      set strXpt to (ocidRectArray's objectAtIndex:(0)) as text
      set strYpt to (ocidRectArray's objectAtIndex:(1)) as text
      set strWpt to (ocidRectArray's objectAtIndex:(2)) as text
      set strHpt to (ocidRectArray's objectAtIndex:(3)) as text
    end if
  end if
  (*
##一部のPPDでHWMarginsが未設定なのもあるので使わなかった
set boolIMargins to (itemArray's containsString:("*HWMargins")) as boolean
if boolIMargins is true then
log itemArray as text
end if
*)
end repeat

set numXpt to strXpt as number
set numYpt to strYpt as number
set numWpt to strWpt as number
set numHpt to strHpt as number

set numPaperWpt to strPaperWpt as number
set numPaperHpt to strPaperHpt as number

(* この計算意味なかった
#log listGetValue as list
log strXpt & "," & strYpt & "," & strWpt & "," & strHpt

set numTop to (numPaperHpt - (numYpt + numHpt)) as number
set numBottom to (numYpt) as number

set numLeft to (numXpt) as number
set numRight to (numPaperWpt - (numXpt + numWpt)) as number

log "-----"
log numTop
log numHpt
log numBottom
log numTop + numHpt + numBottom
log "-----"
log numLeft
log (numWpt)
log numRight
log numLeft + numRight + numWpt
log "-----"
log numPaperWpt
log numPaperHpt
*)
(* ハードウェアマージンはあてにならん
repeat with itemArray in ocidPpdLineArray
set boolIMargins to (itemArray's hasPrefix:("*HWMargins")) as boolean
if boolIMargins is true then
set ocidItemString to (itemArray's stringByReplacingOccurrencesOfString:("*HWMargins: ") withString:(""))
set ocidMarginsArray to (ocidItemString's componentsSeparatedByString:(" "))
#*HWMargins: left bottom right top
set strLeftpt to (ocidMarginsArray's objectAtIndex:(0)) as text
set strBottompt to (ocidMarginsArray's objectAtIndex:(1)) as text
set strRightpt to (ocidMarginsArray's objectAtIndex:(2)) as text
set strToppt to (ocidMarginsArray's objectAtIndex:(3)) as text
end if
end repeat
*)

##############################
#####PLIST
##############################
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
set ocidFilePathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Preferences/com.apple.print.custompapers.plist") isDirectory:(false)

set boolDirExists to appFileManager's fileExistsAtPath:(ocidFilePathURL's |path|) isDirectory:(true)
if boolDirExists = true then
log "処理継続します"
  set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL)
else if boolDirExists = false then
log "設定ファイルcom.apple.print.custompapers.plistが無いので新規作成します"
  set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
end if


####用紙名
set ocidAllKeys to ocidPlistDict's allKeys()
set strAddPaperName to (strPaperName & "_" & strPrinterName) as text
set boolContain to ocidAllKeys's containsObject:(strAddPaperName)
if boolContain is true then
  set numCntRepeat to 1 as integer
  repeat
    set strAddPaperName to (strPaperName & "_" & numCntRepeat & "_" & strPrinterName) as text
    set boolContain to ocidAllKeys's containsObject:(strAddPaperName)
    if boolContain is false then
      exit repeat
    end if
    set numCntRepeat to numCntRepeat + 1 as integer
  end repeat
end if
###セットするDICT
set ocidSetDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
###セット
ocidSetDict's setValue:(strAddPaperName) forKey:("name")
ocidSetDict's setValue:(strAddPaperName) forKey:("id")
ocidSetDict's setValue:(strPrinterName) forKey:("printer")
ocidSetDict's setValue:(true as boolean) forKey:("custom")
#
ocidSetDict's setValue:(numPaperWpt) forKey:("width")
ocidSetDict's setValue:(numPaperHpt) forKey:("height")
#
ocidSetDict's setValue:(numXpt) forKey:("left")
ocidSetDict's setValue:(numYpt) forKey:("top")
ocidSetDict's setValue:(numYpt) forKey:("bottom")
ocidSetDict's setValue:(numXpt) forKey:("right")
###
ocidPlistDict's setObject:(ocidSetDict) forKey:(strAddPaperName)
###上書き保存
set listDone to ocidPlistDict's writeToURL:(ocidFilePathURL) |error|:(reference)
if (item 1 of listDone) is true then
log "正常終了"
else if (item 1 of listDone) is false then
return "保存に失敗しました"
end if

set strAns to ("用紙サイズ:W: " & numPaperWpt & " :H: " & numPaperHpt & "\n") as text
set strAns to strAns & ("余白:右: " & (numXpt as text) & " :左: " & (numXpt as text) & "\n") as text
set strAns to strAns & ("余白:上: " & (numYpt as text) & " :下: " & (numYpt as text) & "\n") as text

##############################
#####ダイアログ
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
activate
  end tell
else
  tell current application
activate
  end tell
end if
try
  set strMes to (strPaperName & "設定値です")
  set aliasIconPath to (POSIX file "/System/Applications/Utilities/Print Center.app/Contents/Resources/AppIcon.icns") as alias
  set recordResult to (display dialog strMes with title "戻り値です" default answer strAns buttons {"クリップボードにコピー", "キャンセル", "再実行"} default button "再実行" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer) as record
on error
return "エラーしました"
end try
if (gave up of recordResult) is true then
return "時間切れです"
end if

##############################
#####値のコピー
##############################
if button returned of recordResult is "クリップボードにコピー" then
  try
    set strText to text returned of recordResult as text
    ####ペーストボード宣言
    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
    set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
  on error
    tell application "Finder"
      set the clipboard to strText as text
    end tell
  end try
end if

try
  set strCommandText to "/usr/bin/killall cfprefsd" as text
do shell script strCommandText
end try
try
  set strCommandText to "/usr/bin/killall -9 printtool" as text
do shell script strCommandText
end try
try
  set strCommandText to "/usr/bin/killall -9 PrintUITool" as text
do shell script strCommandText
end try

##USBリバインド
do shell script "/System/Library/SystemConfiguration/PrinterNotifications.bundle/Contents/MacOS/makequeues -r"
##Update printer module cache
try
do shell script "/System/Library/SystemConfiguration/PrinterNotifications.bundle/Contents/MacOS/makequeues -u"
end try
##Update PPD based queues
do shell script "/System/Library/SystemConfiguration/PrinterNotifications.bundle/Contents/MacOS/makequeues -h"
##LPDリバインド
do shell script "/System/Library/SystemConfiguration/PrinterNotifications.bundle/Contents/MacOS/makequeues -k lpd"
##IPPリバインド
do shell script "/System/Library/SystemConfiguration/PrinterNotifications.bundle/Contents/MacOS/makequeues -k ipp"

###自分自身を再実行
if button returned of recordResult is "再実行" then
  tell application "Finder"
    set aliasPathToMe to (path to me) as alias
  end tell
run script aliasPathToMe
end if


(*

launchctlの再起動は不要だった
/usr/sbin/cupsd
/System/Library/PrivateFrameworks/PrintingPrivate.framework/Versions/A/PrintUITool
/System/Library/Frameworks/ApplicationServices.framework/Frameworks/PrintCore.framework/Versions/A/printtool

sudo launchctl unload -w /System/Library/LaunchDaemons/org.cups.cupsd.plist
sudo launchctl load -w /System/Library/LaunchDaemons/org.cups.cupsd.plist

sudo launchctl unload -w /System/Library/LaunchAgents/com.apple.printtool.agent.plist
sudo launchctl load -w /System/Library/LaunchAgents/com.apple.printtool.agent.plist

sudo launchctl unload -w /System/Library/LaunchAgents/com.apple.printuitool.agent.plist
sudo launchctl load -w /System/Library/LaunchAgents/com.apple.printuitool.agent.plist

*)

|

[bash]プリンターリセット


#!/bin/bash


/bin/echo "プリンタCUPSをリセットします"

/usr/sbin/cupsctl "WebInterface=YES"

SCRIPT_PATH="$0"
STR_FILENAME=$(/usr/bin/basename "$SCRIPT_PATH")
STR_CONTAINER_DIR_PATH=$(/usr/bin/dirname "$SCRIPT_PATH")
/bin/echo "実行中のファイルは $STR_CONTAINER_DIR_PATH/$STR_FILENAME" 1>&2


/bin/echo "CUPS登録済みプリンタ"
STR_PRINTER_NAME=$(/usr/bin/lpstat -e)

IFS=$'\n' read -r -d '' -a LIST_PRINTER <<< "$STR_PRINTER_NAME"
for ITEM_PRINTER in "${LIST_PRINTER[@]}"; do
/bin/echo "プリンタジョブ受付終了:$ITEM_PRINTER"
/usr/sbin/cupsreject "$ITEM_PRINTER"
/bin/echo "プリンタ停止:$ITEM_PRINTER"
/usr/sbin/cupsdisable "$ITEM_PRINTER"

done

/bin/echo "プリンタCUPSジョブリセット"
/usr/bin/cancel -a -x

STR_DATE=$(/bin/date +'%Y%m%d')
/bin/mkdir -p "$HOME/Documents/Apple/Printer"
/bin/mkdir -p "$HOME/Documents/Apple/Printer/$STR_DATE"
/bin/mv -f $HOME/Library/Preferences/com.apple.print* "$HOME/Documents/Apple/Printer/$STR_DATE" 2>/dev/null
/bin/echo "設定ファイルと用紙設定ファイルを"
/bin/echo "ホーム>書類フォルダ>Apple>Printer>$STR_DATEにバックアップしました"
/bin/echo "不要なら削除してもかまいません"

#/bin/echo "y\n" | /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/PrintCore.framework/Versions/A/printtool --reset
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/PrintCore.framework/Versions/A/printtool -f --reset

sleep 2
/bin/echo " "
/bin/echo "プリンタCUPSのリセットを実行しました"
/usr/bin/killall -9 PrintUITool printtool


/usr/bin/open "x-apple.systempreferences:com.apple.Print-Scan-Settings.extension?print"
/usr/bin/open -b com.apple.printcenter
/usr/bin/open "http://localhost:631"
exit 0


|

プリンタで利用可能な用紙名とサイズを取得する 修正


【スクリプトエディタで開く】|

#!/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

property refMe : a reference to current application



set ocidPaperDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0

#プリンタを取得
set ocidPrinterArray to refMe's NSPrinter's printerNames()
set listPrinterArray to ocidPrinterArray as list
###ダイアログを前面に出す
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
###
set strTitle to ("選んでください") as text
set strPrompt to ("プリンタ名を選んでください") as text
try
  set listResponse to (choose from list listPrinterArray with title strTitle with prompt strPrompt default items (item 1 of listPrinterArray) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしましたA"
else if (item 1 of listResponse) is "キャンセル" then
return "キャンセルしましたB"
else
  set strPrinterName to (item 1 of listResponse) as text
end if



#取得したプリンタの数だけ繰り返し
#repeat with itemPrinterArray in ocidPrinterArray
#プリンタ
set ocidPrinter to (refMe's NSPrinter's printerWithName:(strPrinterName))
#プリンタの名前
set strPrinterName to ocidPrinter's |name| as text
##
#プリンタにセットされている用紙名を取得
set strCommandText to ("/usr/bin/lpoptions -p \"" & strPrinterName & "\" -l | grep PageSize") as text
set strResponse to (do shell script strCommandText) as text
#コマンド戻り値を整形して用紙名のリストにする
set ocidResponse to (refMe's NSString's stringWithString:(strResponse))
set ocidReplacedStrings to (ocidResponse's stringByReplacingOccurrencesOfString:("PageSize/Media Size: ") withString:(""))
set ocidChrSet to (refMe's NSCharacterSet's characterSetWithCharactersInString:(" "))
set ocidPaperNameArray to (ocidReplacedStrings's componentsSeparatedByCharactersInSet:(ocidChrSet))
#用紙名の数だけ繰り返し
repeat with itemPaperName in ocidPaperNameArray
  #プリンタから用紙名を指定してサイズを取得
  set recordPaperSize to (ocidPrinter's pageSizeForPaper:(itemPaperName))
  set numWidth to width of recordPaperSize
  set numHeight to height of recordPaperSize
  # log (itemPaperName as text) & ": " & numWidth & "x" & numHeight
  set listSetValuePaperSize to {numWidth, numHeight} as list
  if (numWidth as text) is not "0.0" then
(ocidPaperDict's setObject:(listSetValuePaperSize) forKey:(itemPaperName))
  end if
end repeat
#end repeat

set ocidAllKeys to ocidPaperDict's allKeys()
set ocidAllKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidAllKeyArray's addObjectsFromArray:(ocidAllKeys)
#並び替え
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:("localizedStandardCompare:")
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidAllKeyArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
##
set listAllKeys to ocidSortedArray as list
###ダイアログを前面に出す
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
  # tell application id "com.apple.appkit.xpc.openAndSavePanelService" to activate
else
  tell current application to activate
end if
###
set strTitle to ("選んでください") as text
set strPrompt to ("用紙名を選んでください\n用紙サイズを戻します") as text
try
  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 "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしましたA"
else if (item 1 of listResponse) is "キャンセル" then
return "キャンセルしましたB"
else
  set strResponse to (item 1 of listResponse) as text
end if

set listGetValue to ocidPaperDict's objectForKey:(strResponse)

set strWpt to listGetValue's firstObject() as text
set strHpt to listGetValue's lastObject() as text

set ocidWpt to refMe's NSDecimalNumber's alloc()'s initWithString:(strWpt)
set ocidHpt to refMe's NSDecimalNumber's alloc()'s initWithString:(strHpt)

set ocidPtToIn to refMe's NSDecimalNumber's alloc()'s initWithString:("72")
set ocidWin to (ocidWpt's decimalNumberByDividingBy:(ocidPtToIn))
set ocidHin to (ocidHpt's decimalNumberByDividingBy:(ocidPtToIn))

#set ocidInToMm to refMe's NSDecimalNumber's alloc()'s initWithString:("25.4")
#set ocidWmm to (ocidWin's decimalNumberByMultiplyingBy:(ocidInToMm))
#set ocidHmm to (ocidHin's decimalNumberByMultiplyingBy:(ocidInToMm))

set ocidPtToMM to refMe's NSDecimalNumber's alloc()'s initWithString:("0.352777777777778")
set ocidWmm to (ocidWpt's decimalNumberByMultiplyingBy:(ocidPtToMM))
set ocidHmm to (ocidHpt's decimalNumberByMultiplyingBy:(ocidPtToMM))


###小数点4位まで
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundDown)
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setMaximumFractionDigits:(0)
set ocidWmm to (ocidFormatter's stringFromNumber:(ocidWmm))
set ocidHmm to (ocidFormatter's stringFromNumber:(ocidHmm))

set ocidWInStr to (ocidFormatter's stringFromNumber:(ocidWin))
set ocidHInStr to (ocidFormatter's stringFromNumber:(ocidHin))

set strAns to ("PT:W: " & strWpt & " :H: " & strHpt & "\n") as text
set strAns to strAns & ("IN:W: " & (ocidWInStr as text) & " :H: " & (ocidHInStr as text) & "\n") as text
set strAns to strAns & ("MM:W: " & (ocidWmm as text) & " :H: " & (ocidHmm as text) & "\n") as text

##############################
#####ダイアログ
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
activate
  end tell
else
  tell current application
activate
  end tell
end if
try
  set strMes to (strResponse & "のサイズ\n小数点以下は切り捨てています")
  set aliasIconPath to (POSIX file "/System/Applications/Utilities/Print Center.app/Contents/Resources/AppIcon.icns") as alias
  set recordResult to (display dialog strMes with title "戻り値です" default answer strAns buttons {"クリップボードにコピー", "キャンセル", "再実行"} default button "再実行" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer) as record
on error
return "エラーしました"
end try
if (gave up of recordResult) is true then
return "時間切れです"
end if
###自分自身を再実行
if button returned of recordResult is "再実行" then
  tell application "Finder"
    set aliasPathToMe to (path to me) as alias
  end tell
run script aliasPathToMe
end if
##############################
#####値のコピー
##############################
if button returned of recordResult is "クリップボードにコピー" then
  try
    set strText to text returned of recordResult as text
    ####ペーストボード宣言
    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
    set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
  on error
    tell application "Finder"
      set the clipboard to strText as text
    end tell
  end try
end if



|

[Printer]PPDから仮想プリンタ作成(bash版)


#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
################################################
###設定項目
STR_CHK_NAME="PsPrinterAcrobat9J"
STR_PPD_PATH="/Users/Shared/Library/Printers/PPDs/Contents/Resources/AcrobatPPDs/ADPDF9J.PPD"
#登録済みのプリント台数を求めて
STR_PLIST_FILEPAHT="/Library/Preferences/org.cups.printers.plist"
ROOT_ARRAY=$(/usr/libexec/PlistBuddy -c "Print:" "$STR_PLIST_FILEPAHT")
PRINTER_COUNT=$(/bin/echo "$ROOT_ARRAY" | grep -c 'printer-name')
if [ "$PRINTER_COUNT" = 0 ]; then
/bin/echo "プリンターは登録されていません"
else
  ##登録済みのプリンタがある場合はチェックする
/bin/echo "登録済みプリンタは$PRINTER_COUNT台ありました"
  for ((i = 0; i < PRINTER_COUNT; i++)); do
STR_PRINTER_NAME=$(/usr/libexec/PlistBuddy -c "Print:$i:printer-name:" "$STR_PLIST_FILEPAHT")
/bin/echo "プリンター $i の名前: $STR_PRINTER_NAME"
  done
  #登録済みでないか?チェックする
  for ((i = 0; i < PRINTER_COUNT; i++)); do
STR_PRINTER_NAME=$(/usr/libexec/PlistBuddy -c "Print:$i:printer-name:" "$STR_PLIST_FILEPAHT")
    if [ "$STR_CHK_NAME" = "$STR_PRINTER_NAME" ]; then
/bin/echo "エラー:プリンタ$STR_PRINTER_NAMEはすでに登録済みです"
      #再登録=再作成の場合はここで一度削除する
      #停止
/usr/sbin/cupsdisable "$STR_PRINTER_NAME"
      #ジョブ削除
/usr/bin/cancel -a "$STR_PRINTER_NAME"
      #プリンタ削除
/usr/sbin/lpadmin -x "$STR_PRINTER_NAME"
      #再作成の場合はこのexitは取る
      # exit 1
    fi
  done
fi

#チェックを通過した場合は新規に登録する
if [ -f "$STR_PPD_PATH" ]; then
  #プリンタ登録
/usr/sbin/lpadmin -p "$STR_CHK_NAME" -E -v "lpd://localhost/" -P "$STR_PPD_PATH" -D "$STR_CHK_NAME" -L "PS書き出し用の設定です" -o printer-is-shared=false
  if [ $? -ne 0 ]; then
/bin/echo "エラー:プリンターの登録でエラーになりました"
exit 1
  fi
  #仮想プリンタなので停止させる場合
/usr/sbin/cupsdisable "$STR_PRINTER_NAME"
else
/bin/echo "エラー:PPDファイルが存在しません"
exit 1
fi
##プリントセンターを開く
/usr/bin/open -b com.apple.printcenter
##システム設定を開く
/usr/bin/open "x-apple.systempreferences:com.apple.Print-Scan-Settings.extension"
exit 0


#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
################################################
###設定項目
STR_CHK_NAME="PsPrinteTRUEFLOW_J"
STR_PPD_PATH="/Users/Shared/Library/Printers/PPDs/Contents/Resources/AcrobatPPDs/DS TRUEFLOW_J V1.4"
#登録済みのプリント台数を求めて
STR_PLIST_FILEPAHT="/Library/Preferences/org.cups.printers.plist"
ROOT_ARRAY=$(/usr/libexec/PlistBuddy -c "Print:" "$STR_PLIST_FILEPAHT")
PRINTER_COUNT=$(/bin/echo "$ROOT_ARRAY" | grep -c 'printer-name')
if [ "$PRINTER_COUNT" = 0 ]; then
/bin/echo "プリンターは登録されていません"
else
  ##登録済みのプリンタがある場合はチェックする
/bin/echo "登録済みプリンタは$PRINTER_COUNT台ありました"
  for ((i = 0; i < PRINTER_COUNT; i++)); do
STR_PRINTER_NAME=$(/usr/libexec/PlistBuddy -c "Print:$i:printer-name:" "$STR_PLIST_FILEPAHT")
/bin/echo "プリンター $i の名前: $STR_PRINTER_NAME"
  done
  #登録済みでないか?チェックする
  for ((i = 0; i < PRINTER_COUNT; i++)); do
STR_PRINTER_NAME=$(/usr/libexec/PlistBuddy -c "Print:$i:printer-name:" "$STR_PLIST_FILEPAHT")
    if [ "$STR_CHK_NAME" = "$STR_PRINTER_NAME" ]; then
/bin/echo "エラー:プリンタ$STR_PRINTER_NAMEはすでに登録済みです"
      #再登録=再作成の場合はここで一度削除する
      #停止
/usr/sbin/cupsdisable "$STR_PRINTER_NAME"
      #ジョブ削除
/usr/bin/cancel -a "$STR_PRINTER_NAME"
      #プリンタ削除
/usr/sbin/lpadmin -x "$STR_PRINTER_NAME"
      #再作成の場合はこのexitは取る
      # exit 1
    fi
  done
fi

#チェックを通過した場合は新規に登録する
if [ -f "$STR_PPD_PATH" ]; then
  #プリンタ登録
/usr/sbin/lpadmin -p "$STR_CHK_NAME" -E -v "lpd://localhost/" -P "$STR_PPD_PATH" -D "$STR_CHK_NAME" -L "PS書き出し用の設定です" -o printer-is-shared=false
  if [ $? -ne 0 ]; then
/bin/echo "エラー:プリンターの登録でエラーになりました"
exit 1
  fi
  #仮想プリンタなので停止させる場合
/usr/sbin/cupsdisable "$STR_PRINTER_NAME"
else
/bin/echo "エラー:PPDファイルが存在しません"
exit 1
fi
##プリントセンターを開く
/usr/bin/open -b com.apple.printcenter
##システム設定を開く
/usr/bin/open "x-apple.systempreferences:com.apple.Print-Scan-Settings.extension"
exit 0


|

[Printer]余白0のppdsをダウンロード(as版)


【スクリプトエディタで開く】|

#!/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 framework "AppKit"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

#############################
###AcrobatPPDの有無を確認
#############################
set ocidUserPathArray to (appFileManager's URLsForDirectory:(refMe's NSUserDirectory) inDomains:(refMe's NSLocalDomainMask))
set ocidUserPathURL to ocidUserPathArray's objectAtIndex:0
set ocidPPDFilePathURL to ocidUserPathURL's URLByAppendingPathComponent:"Shared/Library/Printers/PPDs/Contents/Resources/Acrobat/ADPDF9J.PPD" isDirectory:true
set boolFileExists to (appFileManager's fileExistsAtPath:(ocidPPDFilePathURL's |path|()) isDirectory:false)
set ocidSaveDirPathURL to ocidUserPathURL's URLByAppendingPathComponent:"Shared/Library/Printers/PPDs/Contents/Resources/Acrobat" isDirectory:true
############PPDファイルの有無チェック
if boolFileExists is true then
log "PPDはインストール済み"
  set strPPDsFilePath to ocidPPDFilePathURL's |path|() as text
  
else
  ########################
  ###無い場合はダウンロード
  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
  set listResults to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
  #### ダウンロード
  set strZipFileURL to "https://quicktimer.cocolog-nifty.com/icefloe/files/acrobat.zip"
  set ocidZipFilePath to (refMe's NSString's stringWithString:(strZipFileURL))
  set ocidZipFilePathURL to refMe's NSURL's alloc()'s initWithString:(ocidZipFilePath)
  set ocidZipData to refMe's NSData's dataWithContentsOfURL:(ocidZipFilePathURL)
  ##保存
  set ocidTempDirPathURL to appFileManager's temporaryDirectory()
  set ocidSaveZipFilePathURL to ocidTempDirPathURL's URLByAppendingPathComponent:"acrobatppds.zip" isDirectory:false
  set boolResults to ocidZipData's writeToURL:(ocidSaveZipFilePathURL) atomically:true
  
  if boolResults is false then
return "ダウンロードに失敗しました"
  end if
  #### 解凍
  set strZipFilePathURL to ocidSaveZipFilePathURL's |path|() as text
  set strSaveDirPathURL to (ocidSaveDirPathURL's URLByDeletingLastPathComponent())'s |path|() as text
  try
    set theComandText to ("/usr/bin/ditto -xk \"" & strZipFilePathURL & "\" \"" & strSaveDirPathURL & "\"") as text
do shell script theComandText
  end try
end if

set strDirPath to (ocidSaveDirPathURL's |path|()) as text
set aliasFilePath to (ocidSaveDirPathURL's absoluteURL()) as alias
###開く
set ocidSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolSelectFileResults to ocidSharedWorkspace's selectFile:(strPPDsFilePath) inFileViewerRootedAtPath:(strDirPath)
if boolSelectFileResults = false then
  tell application "Finder"
    set refNewWindow to make new Finder window
    set target of refNewWindow to aliasFilePath
  end tell
  
end if

|

[Printer]余白0のppdsをダウンロード(bash版)


#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
#################################################

###実行しているユーザー名
CONSOLE_USER=$(/bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ { print $3 }')
/bin/echo "コンソールユーザー(scutil): $CONSOLE_USER"
###実行しているユーザー名
HOME_USER=$(/bin/echo "$HOME" | /usr/bin/awk -F'/' '{print $NF}')
/bin/echo "実行ユーザー(HOME): $HOME_USER"
###logname
LOGIN_NAME=$(/usr/bin/logname)
/bin/echo "ログイン名(logname): $LOGIN_NAME"
###UID
USER_NAME=$(/usr/bin/id -un)
/bin/echo "ユーザー名(id): $USER_NAME"
###STAT
STAT_USR=$(/usr/bin/stat -f%Su /dev/console)
/bin/echo "STAT_USR(console): $STAT_USR"
########################################
STR_URL="https://raw.githubusercontent.com/force4u/Acrobat/main/AppleScript/%E5%85%B1%E9%80%9A/AcrobatPPDs/AcrobatPPDs.zip"
/bin/echo "処理開始"
##展開先
/bin/mkdir -p "/Users/Shared/Library/Printers/PPDs/Contents/Resources"
/bin/chmod 775 "/Users/Shared/Library/Printers/PPDs/Contents/Resources"
##ダウンロード先
STR_TMPDIRPATH=$(/usr/bin/mktemp -d)
/bin/chmod 777 "$STR_TMPDIRPATH"
/bin/echo "ダウンロード開始:" "$STR_TMPDIRPATH"
##
/usr/bin/curl -s --head "$STR_URL" >/dev/null
if [ $? -ne 0 ]; then
/bin/echo "エラー:URLの生存確認で失敗しました"
exit 1
fi
###リダイレクト先のURLを取得
STR_REDIRECT_URL=$(/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' "$STR_URL")
if [ $? -ne 0 ]; then
/bin/echo "エラー:リダイレクトの取得に失敗しました"
exit 1
fi
###ファイル名を取得
STR_FILE_NAME=$(/usr/bin/basename "$STR_REDIRECT_URL")
/bin/echo "ファイル名:$PKG_FILE_NAME"
###ダウンロード
/usr/bin/curl -L --retry 3 --retry-connrefused --retry-delay 3 -o "$STR_TMPDIRPATH/$STR_FILE_NAME" "$STR_URL"
if [ $? -ne 0 ]; then
/bin/echo "エラー:ダウンロードに失敗しました"
exit 1
fi
###展開
#/usr/bin/unzip -q "$STR_TMPDIRPATH/$STR_FILE_NAME" -d "$STR_TMPDIRPATH"
#if [ $? -ne 0 ]; then
# /bin/echo "エラー:解凍に失敗しました"
# exit 1
#fi
###移動
/usr/bin/ditto -xk "$STR_TMPDIRPATH/$STR_FILE_NAME" "/Users/Shared/Library/Printers/PPDs/Contents/Resources"
if [ $? -ne 0 ]; then
/bin/echo "エラー:移動に失敗しました"
exit 1
fi
/bin/chmod -Rf 775 "/Users/Shared/Library/Printers/PPDs/Contents/Resources/AcrobatPPDs"
/bin/echo "終了しました"

/usr/bin/open -b com.apple.finder "/Users/Shared/Library/Printers/PPDs/Contents/Resources/AcrobatPPDs"
exit 0


|

用紙名から用紙サイズ


set recordPageSizePt to {|Letter|:{612, 792}, |Legal|:{612, 1008}, |Executive|:{522, 756}, |Ledger|:{1224, 792}, |Tabloid|:{792, 1224}, |Screen|:{468, 373}, |AnsiC|:{1224, 1584}, |AnsiD|:{1584, 2448}, |AnsiE|:{2448, 3168}, |AnsiF|:{2016, 2880}, |ARCHA|:{648, 864}, |ARCHB|:{864, 1296}, |ARCHC|:{1296, 1728}, |ARCHD|:{1728, 2592}, |ARCHE|:{2592, 3456}, |ARCHE1|:{2160, 3024}, |ARCHE2|:{1872, 2736}, |ARCHE3|:{1944, 2808}, |A5|:{420, 595.22}, |A4|:{595.22, 842}, |A3|:{842, 1191}, |A2|:{1191, 1684}, |A1|:{1684, 2384}, |A0|:{2384, 3370}, |A4Extra|:{667, 914}, |A3Extra|:{913, 1262}, |OversizeA2|:{1361, 1772}, |OversizeA1|:{1772, 2551}, |OversizeA0|:{2551, 3529}, |ISOB5|:{499, 709}, |ISOB4|:{709, 1001}, |ISOB2|:{1417, 2004}, |ISOB1|:{2004, 2835}, |C5|:{459, 649}, |B5|:{516, 729}, |B4|:{729, 1032}, |B3|:{1032, 1460}, |B2|:{1460, 2064}, |B1|:{2064, 2920}, |B0|:{2920, 4127}, |Oversize92|:{6624, 6624}, |Slide7.5x10|:{540, 720}, |Letter+Bleed|:{630, 810}, |Legal+Bleed|:{630, 1026}, |Tabloid+Bleed|:{810, 1242}, |A5+Bleed|:{436.54, 612.28}, |A4+Bleed|:{612.28, 858.9}, |A3+Bleed|:{858.9, 1207.56}, |A2+Bleed|:{1207.56, 1700.79}, |A1+Bleed|:{1700.79, 2400.95}, |B5+Bleed|:{532.91, 745.51}, |B4+Bleed|:{745.51, 1048.82}, |B3+Bleed|:{1048.82, 1476.85}, |B2+Bleed|:{1476.85, 2080.63}, |LetterRotated+Bleed|:{810, 630}, |LegalRotated+Bleed|:{1026, 630}, |TabloidRotated+Bleed|:{1242, 810}, |A5Rotated+Bleed|:{612.28, 436.54}, |A4Rotated+Bleed|:{858.9, 612.28}, |A3Rotated+Bleed|:{1207.56, 858.9}, |A2Rotated+Bleed|:{1700.79, 1207.56}, |A1Rotated+Bleed|:{2400.95, 1700.79}, |B5Rotated+Bleed|:{745.51, 532.91}, |B4Rotated+Bleed|:{1048.82, 745.51}, |B3Rotated+Bleed|:{1476.85, 745.51}, |B2Rotated+Bleed|:{2080.63, 1476.85}, |LetterRotated|:{792, 612}, |LegalRotated|:{1008, 612}, |TabloidRotated|:{1224, 792}, |A5Rotated|:{595, 420}, |A4Rotated|:{842, 595}, |A3Rotated|:{1191, 842}, |A2Rotated|:{1684, 1191}, |A1Rotated|:{2384, 1684}, |B5Rotated|:{729, 516}, |B4Rotated|:{1032, 729}, |B3Rotated|:{1460, 1032}, |B2Rotated|:{2064, 1460}, |郵便はがき|:{274.96, 408.19}, |往復,はがき|:{411.02, 555.59}, |4連はがきL|:{555.59, 830.55}, |4連はがき|:{558.43, 827.72}, |封筒長形3号|:{331.65, 654.8}, |封筒角形2号|:{671.81, 929.76}, |封筒角形6号|:{448.81, 637.76}, |封筒角形20号|:{638.81, 906.76}, |封筒C4|:{640.63, 907.09}, |封筒C5|:{450.71, 637.8}} as record


【スクリプトエディタで開く】|

#!/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

property refMe : a reference to current application


set recordPageSizePt to {|Letter|:{612, 792}, |Legal|:{612, 1008}, |Executive|:{522, 756}, |Ledger|:{1224, 792}, |Tabloid|:{792, 1224}, |Screen|:{468, 373}, |AnsiC|:{1224, 1584}, |AnsiD|:{1584, 2448}, |AnsiE|:{2448, 3168}, |AnsiF|:{2016, 2880}, |ARCHA|:{648, 864}, |ARCHB|:{864, 1296}, |ARCHC|:{1296, 1728}, |ARCHD|:{1728, 2592}, |ARCHE|:{2592, 3456}, |ARCHE1|:{2160, 3024}, |ARCHE2|:{1872, 2736}, |ARCHE3|:{1944, 2808}, |A5|:{420, 595.22}, |A4|:{595.22, 842}, |A3|:{842, 1191}, |A2|:{1191, 1684}, |A1|:{1684, 2384}, |A0|:{2384, 3370}, |A4Extra|:{667, 914}, |A3Extra|:{913, 1262}, |OversizeA2|:{1361, 1772}, |OversizeA1|:{1772, 2551}, |OversizeA0|:{2551, 3529}, |ISOB5|:{499, 709}, |ISOB4|:{709, 1001}, |ISOB2|:{1417, 2004}, |ISOB1|:{2004, 2835}, |C5|:{459, 649}, |B5|:{516, 729}, |B4|:{729, 1032}, |B3|:{1032, 1460}, |B2|:{1460, 2064}, |B1|:{2064, 2920}, |B0|:{2920, 4127}, |Oversize92|:{6624, 6624}, |Slide7.5x10|:{540, 720}, |Letter+Bleed|:{630, 810}, |Legal+Bleed|:{630, 1026}, |Tabloid+Bleed|:{810, 1242}, |A5+Bleed|:{436.54, 612.28}, |A4+Bleed|:{612.28, 858.9}, |A3+Bleed|:{858.9, 1207.56}, |A2+Bleed|:{1207.56, 1700.79}, |A1+Bleed|:{1700.79, 2400.95}, |B5+Bleed|:{532.91, 745.51}, |B4+Bleed|:{745.51, 1048.82}, |B3+Bleed|:{1048.82, 1476.85}, |B2+Bleed|:{1476.85, 2080.63}, |LetterRotated+Bleed|:{810, 630}, |LegalRotated+Bleed|:{1026, 630}, |TabloidRotated+Bleed|:{1242, 810}, |A5Rotated+Bleed|:{612.28, 436.54}, |A4Rotated+Bleed|:{858.9, 612.28}, |A3Rotated+Bleed|:{1207.56, 858.9}, |A2Rotated+Bleed|:{1700.79, 1207.56}, |A1Rotated+Bleed|:{2400.95, 1700.79}, |B5Rotated+Bleed|:{745.51, 532.91}, |B4Rotated+Bleed|:{1048.82, 745.51}, |B3Rotated+Bleed|:{1476.85, 745.51}, |B2Rotated+Bleed|:{2080.63, 1476.85}, |LetterRotated|:{792, 612}, |LegalRotated|:{1008, 612}, |TabloidRotated|:{1224, 792}, |A5Rotated|:{595, 420}, |A4Rotated|:{842, 595}, |A3Rotated|:{1191, 842}, |A2Rotated|:{1684, 1191}, |A1Rotated|:{2384, 1684}, |B5Rotated|:{729, 516}, |B4Rotated|:{1032, 729}, |B3Rotated|:{1460, 1032}, |B2Rotated|:{2064, 1460}, |郵便はがき|:{274.96, 408.19}, |往復,はがき|:{411.02, 555.59}, |4連はがきL|:{555.59, 830.55}, |4連はがき|:{558.43, 827.72}, |封筒長形3号|:{331.65, 654.8}, |封筒角形2号|:{671.81, 929.76}, |封筒角形6号|:{448.81, 637.76}, |封筒角形20号|:{638.81, 906.76}, |封筒C4|:{640.63, 907.09}, |封筒C5|:{450.71, 637.8}} as record
#
set ocidPageSizePtDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidPageSizePtDict's setDictionary:(recordPageSizePt)
set ocidAllKeys to ocidPageSizePtDict's allKeys()
#
set ocidPageNameArray to refMe's NSMutableArray's arrayWithArray:(ocidAllKeys)
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:("localizedStandardCompare:")
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidPaperNameArray to ocidPageNameArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
set listPaperSizeName to ocidPaperNameArray as list
###ダイアログ
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
try
  set listResponse to (choose from list listPaperSizeName with title "選んでください" with prompt "用紙を選んでください" default items (item 1 of listPaperSizeName) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしました"
end if
set strResponse to (item 1 of listResponse) as text

#####ポイントサイズ
set listPaperSizePt to ocidPageSizePtDict's valueForKey:(strResponse)
log listPaperSizePt as list
set intWidthPt to (item 2 of listPaperSizePt) as number
set intHeightPt to (item 1 of listPaperSizePt) as number
#####ミリサイズ
set numVar to 25.4 as number
set numWidthMM to ((intWidthPt / 72) * numVar) as number
set numHeightMM to ((intHeightPt / 72) * numVar) as number




|

プリンタで利用可能な用紙名とサイズを取得する


【スクリプトエディタで開く】|

#!/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

property refMe : a reference to current application

#プリンタを取得
set ocidPrinterArray to refMe's NSPrinter's printerNames()
#取得したプリンタの数だけ繰り返し
repeat with itemPrinterArray in ocidPrinterArray
  #プリンタ
  set ocidPrinter to (refMe's NSPrinter's printerWithName:(itemPrinterArray))
  #プリンタの名前
  set strPrinterName to ocidPrinter's |name| as text
  #プリンタにセットされている用紙名を取得
  set strCommandText to ("/usr/bin/lpoptions -p \"" & strPrinterName & "\" -l | grep PageSize") as text
  set strResponse to (do shell script strCommandText) as text
  #コマンド戻り値を整形して用紙名のリストにする
  set ocidResponse to (refMe's NSString's stringWithString:(strResponse))
  set ocidReplacedStrings to (ocidResponse's stringByReplacingOccurrencesOfString:("PageSize/Media Size: ") withString:(""))
  set ocidChrSet to (refMe's NSCharacterSet's characterSetWithCharactersInString:(" "))
  set ocidPaperNameArray to (ocidReplacedStrings's componentsSeparatedByCharactersInSet:(ocidChrSet))
  #用紙名の数だけ繰り返し
  repeat with itemPaperName in ocidPaperNameArray
    #プリンタから用紙名を指定してサイズを取得
    set recordPaperSize to (ocidPrinter's pageSizeForPaper:(itemPaperName))
    set numWidth to width of recordPaperSize
    set numHeight to height of recordPaperSize
log (itemPaperName as text) & ": " & numWidth & "x" & numHeight
  end repeat
  
  
end repeat



|

プリンタ名の取得


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application


##プリンタの名前
set ocidPrinterNameArray to refMe's NSPrinter's printerNames()
set listPrinterName to ocidPrinterNameArray as list
log listPrinterName
##プリンタのタイプ=PPD名
set ocidPrinterTypeArray to refMe's NSPrinter's printerTypes()
set listPrinterType to ocidPrinterTypeArray as list
log listPrinterType
##プリンタの台数
set intCntArray to (count of listPrinterType) as integer
log intCntArray
##プリンタを順番に処理
repeat with itemPrinterName in listPrinterName
  set ocidPrinter to (refMe's NSPrinter's printerWithName:(itemPrinterName))
log ocidPrinter's |name|() as text
log ocidPrinter's type() as text
log ocidPrinter's deviceDescription() as record
  
  
  
end repeat


|

より以前の記事一覧

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom