First Script

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

Hi, Chris.

I’m not able to try your script as I don’t have any postscript files with which to play. But if it’s working for you and it’s your first script, I expect you’re feeling pretty pleased at the moment. :slight_smile:

The only criticism I have from an AppleScript perspective concerns the ‘quit’ stuff at the end.

  1. ‘quit’ is a command which affects applications rather than scripts per se, so, in the absence of an explicit ‘tell’ statement, the ‘quit’ at the end goes to the application running the script. If the script’s running as an application, then it’s own applet code receives the command. Otherwise, it’ll be AppleScript Editor or whatever else is running the script.
  2. If the script’s running as an application, it’ll quit anyway when it’s finished (unless it’s been saved as “Stay Open”), so the ‘quit’ at the end isn’t actually necessary.
  3. The ‘on quit’ intercept handler is also unnecessary, since you can simply display the dialog as the last instruction in the script.
  4. It’s considered bad form to intersperse handlers with “top level” code. They should all be grouped together either at the beginning or the end. (The top-level code is technically an implicit ‘run’ handler in its own right.) Different people have different views about whether other handlers should go before or after the ‘run’ handler, but you’re free to choose as long as it’s one or the other. :wink:

The end of your script could be:


# Convert page height to three digits
set newPageHeight to text -3 thru -1 of ("000" & newPageHeight)

# 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 & "'"

# Display "Finished" dialogue
display dialog "All Done." buttons {"Quit"}

Thanks Nigel.

The script is working okay for the moment. It gets mucked up by certain conditions in the file to be processed. I have to work on the GREP searches a bit. It also can’t increase the dimensions of a postscript file, another thing to work on.

The quit stuff didn’t feel particularly elegant and was kind of a last minute addition.

I’ll make the changes that you suggested.