Quantcast
Channel: Planet DTP@jp
Viewing all articles
Browse latest Browse all 4816

FORCE: [AppleScript]com.apple.print.custompapersにカスタム用紙サイズを登録する

$
0
0

defaults write com.apple.print.custompapers で
カスタム用紙サイズを作成します

手順は以下

1:エクセルで用紙サイズリストを作成します
2:タブ区切りテキストで保存します
3:スクリプトを実行します


こんな感じのエクセルの表を

Comappleprintcustompapers002


タブ区切りテキストでかき出して

Comappleprintcustompapers003


スクリプトを実行すると
カスタム用紙サイズをババっと登録できます

Comappleprintcustompapers001


(*
タブ区切りテキスト内の
用紙サイズを「カスタム用紙」として登録します

【重要】インチサイズ専用
mm用を作成する場合は文中の換算部を変更すれば使えます
----inchpt換算部
set theHeight to (theHeight * 72) as text
set theWidth to (theWidth * 72) as text
----mmpt換算の場合

set theHeight to (theHeight * 2.8346456693) as text
set theWidth to (theWidth * 2.8346456693) as text


ユーザーの「preferences」フォルダに
com.apple.print.custompapers.backup フォルダを作ります
そこにカスタム用紙サイズファイルのバックアップを作成します
*)



onrun

-----まずはバックアップを作成
-----初期設定までのパスを取得
settheUserPrefDirNameto (path topreferences folderfromuser domain) astext
-----バックアップフォルダを作成
tellapplication"Finder"
try
makenewfolderattheUserPrefDirNamewith properties {name:"com.apple.print.custompapers.backup"}
endtry
endtell
-----ファイル名やらディレクトリ名やら
setthePrefFileto (theUserPrefDirName& "com.apple.print.custompapers.plist") astext
settheBackupDirto (theUserPrefDirName& "com.apple.print.custompapers.backup") astext
settheBackupFileNameto (theBackupDir& ":com.apple.print.custompapers.plist") astext
settheTimetodo shell script"date '+%Y%m%d_%k%M%S'"
settheNewFileNameto ("com.apple.print.custompapers.plist." & theTime) astext
----ファイルをコピーしてからファイル名を変更する
tellapplication"Finder"
try
copyfilethePrefFiletofoldertheBackupDir
delay 1
setthenameoffiletheBackupFileNametotheNewFileName
endtry
endtell

----本処理スタート

------------プロンプトの文言改行が使えます\nを入れます
settheWithPromptto"用紙サイズタブテキストからカスタム用紙サイズを作成します\n"
------------ファイル選択ダイアログのデフォルトのディレクトリ
tellapplication"Finder"
settheDefaultLocationto (containerof (path tome)) asalias
endtell
------------Uniform Type Identifier指定
settheFileTypeListto"public.plain-text,com.apple.traditional-mac-plain-text" astext
------------のファイルタイプをリスト形式に整形する
setAppleScript's text item delimitersto {","}
settheFileTypeListtoeverytext itemoftheFileTypeList
------------ダイアログを出して選択されたファイルは「open」に渡す
open (choose filedefault locationtheDefaultLocation¬
with prompttheWithPrompt¬
of typetheFileTypeList¬
invisiblestrue¬
withmultiple selections allowedwithoutshowing package contents)
endrun

onopenobjOpen
------元々エイリアスだけどお約束で
settheFileAliastoobjOpenasalias
------選択したファイルのUNIXパスを取得
settheUnixPasstoPOSIX pathoftheFileAliasastext
-----選んだファイルを読み込む
settheDatato (do shell script"cat '" & theUnixPass& "'") as«class utf8»


------改行毎にリストにする
setAppleScript's text item delimitersto {"\r"}
setretListDatato (everytext itemoftheData) aslist
------リストに変更した後で項目数を数える
setnumListLineto (countofretListData) asnumber
setnumListLinetonumListLine - 1 asnumber
---2行目から読み込むための初期化
setnumLineto 2 asnumber

repeatnumListLinetimes
-----初期化
settheBottomto"" astext
settheLeftto"" astext
settheRightto"" astext
settheTopto"" astext
settheIdto"" astext
settheNameto"" astext
settheIdto"" astext
-----1行読み込む
settheDataListLineto (itemnumLineofretListData) astext
-----カンマがあれば取り除く
settheDataListLinetodoReplace(theDataListLine, ",", "")
setAppleScript's text item delimitersto {"\t"}
-----タブ毎にリスト項目にする
setretListDataOfLineto (everytext itemoftheDataListLine) aslist
setAppleScript's text item delimitersto {""}
-----各項目を取り出す
settheNameto (item 1 ofretListDataOfLine) astext
settheWidthto (item 2 ofretListDataOfLine) asnumber
settheHeightto (item 3 ofretListDataOfLine) asnumber
settheTopto (item 4 ofretListDataOfLine) astext
settheBottomto (item 5 ofretListDataOfLine) astext
settheLeftto (item 6 ofretListDataOfLine) astext
settheRightto (item 7 ofretListDataOfLine) astext
-----固定項目 プリンターとカスタム用紙サイズ
setthePrinterto" " astext
settheCustomto"TRUE" astext
----inchpt換算部
settheHeightto (theHeight * 72) astext
settheWidthto (theWidth * 72) astext
----mmpt換算の場合
(*
set theHeight to (theHeight * 2.8346456693) as text
set theWidth to (theWidth * 2.8346456693) as text
*)
-----項目の値が空だった場合の処理
iftheIdis"" then
settheIdtotheNameastext
else
settheIdtotheIdastext
endif
iftheTopis"" then
settheTopto"0" astext
else
settheToptotheTopastext
endif
iftheBottomis"" then
settheBottomto"0" astext
else
settheBottomtotheBottomastext
endif
iftheLeftis"" then
settheLeftto"0" astext
else
settheLefttotheLeftastext
endif
iftheRightis"" then
settheRightto"0" astext
else
settheRighttotheRightastext
endif

--------defaults write com.apple.print.custompapers
-----theName
settheNameComto"defaults write com.apple.print.custompapers \"" & theName& "\" -dict-add name -string \"" & theName& "\"" astext
settheNameComtodoReplace(theNameCom, "\"\"", "\"")
do shell scripttheNameCom

-----theId
settheIdComto"defaults write com.apple.print.custompapers \"" & theName& "\" -dict-add id -string \"" & theId& "\"" astext
settheIdComtodoReplace(theIdCom, "\"\"", "\"")
do shell scripttheIdCom

-----Printer
setthePrinterComto"defaults write com.apple.print.custompapers \"" & theName& "\" -dict-add printer -string \"" & thePrinter& "\"" astext
setthePrinterComtodoReplace(thePrinterCom, "\"\"", "\"")
do shell scriptthePrinterCom

-----Custom
settheCustomComto"defaults write com.apple.print.custompapers \"" & theName& "\" -dict-add custom -bool " & theCustom& "" astext
settheCustomComtodoReplace(theCustomCom, "\"\"", "\"")
do shell scripttheCustomCom

-----height
settheHeightComto"defaults write com.apple.print.custompapers \"" & theName& "\" -dict-add height -float " & theHeight& "" astext
settheHeightComtodoReplace(theHeightCom, "\"\"", "\"")
do shell scripttheHeightCom

-----width
settheWidthComto"defaults write com.apple.print.custompapers \"" & theName& "\" -dict-add width -float " & theWidth& "" astext
settheWidthComtodoReplace(theWidthCom, "\"\"", "\"")
do shell scripttheWidthCom

-----top
settheTopComto"defaults write com.apple.print.custompapers \"" & theName& "\" -dict-add top -float " & theTop& "" astext
settheTopComtodoReplace(theTopCom, "\"\"", "\"")
do shell scripttheTopCom

-----bottom
settheBottomComto"defaults write com.apple.print.custompapers \"" & theName& "\" -dict-add bottom -float " & theBottom& "" astext
settheBottomComtodoReplace(theBottomCom, "\"\"", "\"")
do shell scripttheBottomCom

-----left
settheLeftComto"defaults write com.apple.print.custompapers \"" & theName& "\" -dict-add left -float " & theLeft& "" astext
settheLeftComtodoReplace(theLeftCom, "\"\"", "\"")
do shell scripttheLeftCom

-----right
settheRightComto"defaults write com.apple.print.custompapers \"" & theName& "\" -dict-add right -float " & theRight& "" astext
settheRightComtodoReplace(theRightCom, "\"\"", "\"")
do shell scripttheRightCom


-----カウントアップ
setnumLinetonumLine + 1 asnumber
endrepeat




endopen




--------------文字の置き換えサブルーチン
todoReplace(theText, theOrgStr, theNewStr)
settheOldDelimtoAppleScript's text item delimiters
setAppleScript's text item delimiterstotheOrgStr
settheTmpListtoeverytext itemoftheText
setAppleScript's text item delimiterstotheNewStr
settheReplaceStrtotheTmpListastext
setAppleScript's text item delimiterstotheOldDelim
returntheReplaceStr
enddoReplace


「Na_custompapers.rtf」をダウンロード

「Na_custompapers.zip」をダウンロード


Viewing all articles
Browse latest Browse all 4816

Trending Articles