[AppleScript]com.apple.print.custompapersにカスタム用紙サイズを登録する
defaults write com.apple.print.custompapers で
カスタム用紙サイズを作成します
手順は以下
1:エクセルで用紙サイズリストを作成します
2:タブ区切りテキストで保存します
3:スクリプトを実行します
こんな感じのエクセルの表を
タブ区切りテキストでかき出して
スクリプトを実行すると
カスタム用紙サイズをババっと登録できます
(*
タブ区切りテキスト内の
用紙サイズを「カスタム用紙」として登録します
【重要】インチサイズ専用
mm用を作成する場合は文中の換算部を変更すれば使えます
----inch→pt換算部
set theHeight to (theHeight * 72) as text
set theWidth to (theWidth * 72) as text
----mm→pt換算の場合
set theHeight to (theHeight * 2.8346456693) as text
set theWidth to (theWidth * 2.8346456693) as text
ユーザーの「preferences」フォルダに
com.apple.print.custompapers.backup フォルダを作ります
そこにカスタム用紙サイズファイルのバックアップを作成します
*)
on run
-----まずはバックアップを作成
-----初期設定までのパスを取得
set theUserPrefDirName to (path to preferences folder from user domain) as text
-----バックアップフォルダを作成
tell application "Finder"
try
make new folder at theUserPrefDirName with properties {name:"com.apple.print.custompapers.backup"}
end try
end tell
-----ファイル名やらディレクトリ名やら
set thePrefFile to (theUserPrefDirName & "com.apple.print.custompapers.plist") as text
set theBackupDir to (theUserPrefDirName & "com.apple.print.custompapers.backup") as text
set theBackupFileName to (theBackupDir & ":com.apple.print.custompapers.plist") as text
set theTime to do shell script "date '+%Y%m%d_%k%M%S'"
set theNewFileName to ("com.apple.print.custompapers.plist." & theTime) as text
----ファイルをコピーしてからファイル名を変更する
tell application "Finder"
try
copy file thePrefFile to folder theBackupDir
delay 1
set the name of file theBackupFileName to theNewFileName
end try
end tell
----本処理スタート
------------プロンプトの文言改行が使えます\nを入れます
set theWithPrompt to "用紙サイズタブテキストからカスタム用紙サイズを作成します\n"
------------ファイル選択ダイアログのデフォルトのディレクトリ
tell application "Finder"
set theDefaultLocation to (container of (path to me)) as alias
end tell
------------Uniform Type Identifier指定
set theFileTypeList to "public.plain-text,com.apple.traditional-mac-plain-text" as text
------------↑のファイルタイプをリスト形式に整形する
set AppleScript's text item delimiters to {","}
set theFileTypeList to every text item of theFileTypeList
------------ダイアログを出して選択されたファイルは「open」に渡す
open (choose file default location theDefaultLocation ¬
with prompt theWithPrompt ¬
of type theFileTypeList ¬
invisibles true ¬
with multiple selections allowed without showing package contents)
end run
on open objOpen
------元々エイリアスだけどお約束で
set theFileAlias to objOpen as alias
------選択したファイルのUNIXパスを取得
set theUnixPass to POSIX path of theFileAlias as text
-----選んだファイルを読み込む
set theData to (do shell script "cat '" & theUnixPass & "'") as «class utf8»
------改行毎にリストにする
set AppleScript's text item delimiters to {"\r"}
set retListData to (every text item of theData) as list
------リストに変更した後で項目数を数える
set numListLine to (count of retListData) as number
set numListLine to numListLine - 1 as number
---2行目から読み込むための初期化
set numLine to 2 as number
repeat numListLine times
-----初期化
set theBottom to "" as text
set theLeft to "" as text
set theRight to "" as text
set theTop to "" as text
set theId to "" as text
set theName to "" as text
set theId to "" as text
-----1行読み込む
set theDataListLine to (item numLine of retListData) as text
-----カンマがあれば取り除く
set theDataListLine to doReplace(theDataListLine, ",", "")
set AppleScript's text item delimiters to {"\t"}
-----タブ毎にリスト項目にする
set retListDataOfLine to (every text item of theDataListLine) as list
set AppleScript's text item delimiters to {""}
-----各項目を取り出す
set theName to (item 1 of retListDataOfLine) as text
set theWidth to (item 2 of retListDataOfLine) as number
set theHeight to (item 3 of retListDataOfLine) as number
set theTop to (item 4 of retListDataOfLine) as text
set theBottom to (item 5 of retListDataOfLine) as text
set theLeft to (item 6 of retListDataOfLine) as text
set theRight to (item 7 of retListDataOfLine) as text
-----固定項目 プリンターとカスタム用紙サイズ
set thePrinter to " " as text
set theCustom to "TRUE" as text
----inch→pt換算部
set theHeight to (theHeight * 72) as text
set theWidth to (theWidth * 72) as text
----mm→pt換算の場合
(*
set theHeight to (theHeight * 2.8346456693) as text
set theWidth to (theWidth * 2.8346456693) as text
*)
-----項目の値が空だった場合の処理
if theId is "" then
set theId to theName as text
else
set theId to theId as text
end if
if theTop is "" then
set theTop to "0" as text
else
set theTop to theTop as text
end if
if theBottom is "" then
set theBottom to "0" as text
else
set theBottom to theBottom as text
end if
if theLeft is "" then
set theLeft to "0" as text
else
set theLeft to theLeft as text
end if
if theRight is "" then
set theRight to "0" as text
else
set theRight to theRight as text
end if
--------defaults write com.apple.print.custompapers
-----theName
set theNameCom to "defaults write com.apple.print.custompapers \"" & theName & "\" -dict-add name -string \"" & theName & "\"" as text
set theNameCom to doReplace(theNameCom, "\"\"", "\"")
do shell script theNameCom
-----theId
set theIdCom to "defaults write com.apple.print.custompapers \"" & theName & "\" -dict-add id -string \"" & theId & "\"" as text
set theIdCom to doReplace(theIdCom, "\"\"", "\"")
do shell script theIdCom
-----Printer
set thePrinterCom to "defaults write com.apple.print.custompapers \"" & theName & "\" -dict-add printer -string \"" & thePrinter & "\"" as text
set thePrinterCom to doReplace(thePrinterCom, "\"\"", "\"")
do shell script thePrinterCom
-----Custom
set theCustomCom to "defaults write com.apple.print.custompapers \"" & theName & "\" -dict-add custom -bool " & theCustom & "" as text
set theCustomCom to doReplace(theCustomCom, "\"\"", "\"")
do shell script theCustomCom
-----height
set theHeightCom to "defaults write com.apple.print.custompapers \"" & theName & "\" -dict-add height -float " & theHeight & "" as text
set theHeightCom to doReplace(theHeightCom, "\"\"", "\"")
do shell script theHeightCom
-----width
set theWidthCom to "defaults write com.apple.print.custompapers \"" & theName & "\" -dict-add width -float " & theWidth & "" as text
set theWidthCom to doReplace(theWidthCom, "\"\"", "\"")
do shell script theWidthCom
-----top
set theTopCom to "defaults write com.apple.print.custompapers \"" & theName & "\" -dict-add top -float " & theTop & "" as text
set theTopCom to doReplace(theTopCom, "\"\"", "\"")
do shell script theTopCom
-----bottom
set theBottomCom to "defaults write com.apple.print.custompapers \"" & theName & "\" -dict-add bottom -float " & theBottom & "" as text
set theBottomCom to doReplace(theBottomCom, "\"\"", "\"")
do shell script theBottomCom
-----left
set theLeftCom to "defaults write com.apple.print.custompapers \"" & theName & "\" -dict-add left -float " & theLeft & "" as text
set theLeftCom to doReplace(theLeftCom, "\"\"", "\"")
do shell script theLeftCom
-----right
set theRightCom to "defaults write com.apple.print.custompapers \"" & theName & "\" -dict-add right -float " & theRight & "" as text
set theRightCom to doReplace(theRightCom, "\"\"", "\"")
do shell script theRightCom
-----カウントアップ
set numLine to numLine + 1 as number
end repeat
end open
--------------文字の置き換えサブルーチン
to doReplace(theText, theOrgStr, theNewStr)
set theOldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to theOrgStr
set theTmpList to every text item of theText
set AppleScript's text item delimiters to theNewStr
set theReplaceStr to theTmpList as text
set AppleScript's text item delimiters to theOldDelim
return theReplaceStr
end doReplace
| 固定リンク
「Print」カテゴリの記事
- [macOS13]CUPS2.3.4での混乱(2023.07.06)
- [Print]キーノートとパワーポイントのデフォルト用紙サイズをカスタム用紙サイズに追加(2021.11.21)
- プリンターCUPSのリセット(2021.11.04)
- PostScriptとサイズの数値の丸めの違い(2021.07.04)
- [AppleScripts]PS書出し用のバーチャルプリンタを追加する(簡易版)(2021.07.04)