set theLeft to "0" as text
set theRight to "0" as text
set theTop to "0" as text
set theBottom to "0" as text
set theId to "" as text
set theName to "" as text
set thePrinter to " " as text
set theCustom to "TRUE" as text
set theWidth to "" as text
set theHeight to "" as text
set theUserPrefDirName to (path to preferences folder from user domain) as text
try
make new folder at theUserPrefDirName with properties {name:"com.apple.print.custompapers.backup"}
end try
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'") as text
set theNewFileName to ("com.apple.print.custompapers.plist." & theTime & ".plist") 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
tell application "Adobe Illustrator"
activate
tell current document
tell artboard 1
set theProp to properties
set theRect to (artboard rectangle of theProp) as list
end tell
end tell
end tell
set theWl to (item 1 of theRect) as number
set theHt to (item 2 of theRect) as number
set theWr to (item 3 of theRect) as number
set theHb to (item 4 of theRect) as number
set theWidthPx to theWl - theWr as number
set theHightPx to theHt - theHb as number
if theWidthPx < 0 then set theWidthPx to -theWidthPx
if theHightPx < 0 then set theHightPx to -theHightPx
set theWidthMM to (((theWidthPx / 72) * 25.4) as integer) as text
set theHeightMM to (((theHightPx / 72) * 25.4) as integer) as text
set theWidth to (theWidthPx) as text
set theHeight to (theHightPx) as text
set theName to (theWidthMM & "x" & theHeightMM) as text
set theId to (theWidthMM & "x" & theHeightMM) as text
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
return "Done"
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