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
set theWithPrompt to "用紙サイズタブテキストからカスタム用紙サイズを作成します\n"
tell application "Finder"
set theDefaultLocation to (container of (path to me)) as alias
end tell
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 (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
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
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
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
set theHeight to (theHeight * 72) as text
set theWidth to (theWidth * 72) 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
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
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
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
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
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
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
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
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
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
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