Hi all,
I’ve written my first script (with the help of some folks here) and would love some feedback on it.
The script take a postscript file (only one that has been created using the save as dialogue in Acrobat Pro 10.x) and re-sizes it based on a user chosen percentage.
If anyone feels like looking over my code and offering any thoughts, suggestions, etc, I’d love to hear them.
Here is the code:
# Postscript Resize Script
# © 2012 Christopher M. Zucker
set inputFile to (choose file with prompt "Select a Postscript file to resize:" of type {"ps"})
tell application "System Events" to set fileName to name of inputFile
# Get proper UNIX path to Postscript file
set p_inputFile to POSIX path of inputFile
# Get the current page X scale
set origPageXScale to ""
try
set origPageXScale to (do shell script "grep -P -o -m1 '[0-9]??\\.??[0-9]+(?=[[:space:]]-[0-9]??\\.??[0-9]+[[:space:]]scale[[:space:]][0-9][[:space:]]-[0-9]+[[:space:]]translate)' '" & p_inputFile & "'")
end try
# Get the current psge Y scale
set origPageYScale to ""
try
set origPageYScale to (do shell script "grep -P -o -m1 '[0-9]??\\.??[0-9]+(?=[[:space:]]scale[[:space:]][0-9][[:space:]]-[0-9]+[[:space:]]translate)' '" & p_inputFile & "'")
end try
# Get the current page width
set origPageWidth to ""
try
set origPageWidth to (do shell script "grep -P -o '(?<=\\<\\<\\/PageSize[[:space:]]\\[)[0-9]{3}' '" & p_inputFile & "'")
end try
# Get the current page height
set origPageHeight to ""
try
set origPageHeight to (do shell script "grep -P -o '(?<=\\<\\<\\/PageSize[[:space:]]\\[[0-9]{3}[[:space:]])[0-9]{3}' '" & p_inputFile & "'")
end try
# Ask user to input reduction percentage
set origReductionValue to ""
try
set origReductionValue to text returned of (display dialog "Please enter a reduction precentage:" default answer "100")
end try
# Choose file name for reduced postscript file and force a .ps file extension
set outputFile to (choose file name with prompt "Set file name and location of the reduced file:" default name origReductionValue & "percent_" & fileName)
set outputFile to outputFile as text
if outputFile does not end with ".ps" then
set outputFile to outputFile & ".ps"
end if
# Get proper UNIX path to reduced Postscript file
set p_outputFile to POSIX path of outputFile
# Convert reduction percentage to decimal value
set newReductionValue to origReductionValue * 0.01
# Multiply the page X scale by the reduction value and move decimal point
set newPageXScale to origPageXScale * newReductionValue
# Multiply the page Y scale by the reduction percentage
set newPageYScale to origPageYScale * newReductionValue
# Multiply the original page width by the reduction percentage
set newPageWidth to origPageWidth * newReductionValue as integer
# Convert page width to three digits
set newPageWidth to text -3 thru -1 of ("000" & newPageWidth)
# Multiply the original page height by the reduction percentage
set newPageHeight to origPageHeight * newReductionValue as integer
# Convert page height to three digits
set newPageHeight to text -3 thru -1 of ("000" & newPageHeight)
# Display "Finished" dialogue
on quit
display dialog "All Done." buttons {"Quit"}
continue quit
end quit
# Sed script to replace the original scale and PageSize and all page box values with the reduced values
do shell script "sed -e 's/" & origPageXScale & " -" & origPageYScale & " scale/" & newPageXScale & " -" & newPageYScale & " scale/g' -e 's/\\<\\<\\/PageSize \\[" & origPageWidth & " " & origPageHeight & "/\\<\\<\\/PageSize \\[" & newPageWidth & " " & newPageHeight & "/g' '" & p_inputFile & "' > '" & p_outputFile & "'"
quit
Best,
Chris