Bates numbering with Hazel and PDFpen 7

I am trying to get Hazel to watch a folder, and when a PDF enters the folder, to run the PDF pen 7 Bates numbering AppleScript. However, when I embed the AppleScript into Hazel, and try to compile it, I get an error message. See the AppleScript below.
The error message says "expected 'end' but found 'on' ", and it points to the leading-zero subroutine (that begins after the 'on') as a problem.
Does anyone know how to make it work?
Note: I got the regular page numbering AppleScript from PDF pen 7 to work (as an embedded script), but I need the Bates numbering feature.
Thanks for any help you can offer. Ty.
*********BATES NUMBER SCRIPT FROM PDFPEN 7 ********
-- This script places a page number in the bottom left corner of each page using a bates numbering scheme.
tell application "PDFpenPro 7"
if (count documents) > 0 then
-- Get value for what should be prepended to beginning of numbering scheme.
set prepend_value to text returned of (display dialog ¬
"Enter Bates numbering prefix:" with title ¬
"Bates Numbering" default answer ¬
"" buttons {"Cancel", "OK"} ¬
default button "OK")
set pageCount to count (pages of document 1)
repeat with pageNumber from 1 to pageCount
set thePage to page pageNumber of document 1
set currentPage to prepend_value & my add_leading_zeros(pageNumber, 5) as rich text
make new text imprint at end of imprints of thePage with properties {rich text:currentPage, x position:36, y position:36, height:16, width:540}
end repeat
return pageCount
end if
end tell
-- A sub-routine for adding leading zeros to numbers:
-- Courtesy of Apple
-- http://www.apple.com/applescript/sbrt/sbrt-02.html
on add_leading_zeros(this_number, max_leading_zeros)
set the threshold_number to (10 ^ max_leading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (this_number as text)) as string
else
return this_number as text
end if
end add_leading_zeros
***
***************************
The error message says "expected 'end' but found 'on' ", and it points to the leading-zero subroutine (that begins after the 'on') as a problem.
Does anyone know how to make it work?
Note: I got the regular page numbering AppleScript from PDF pen 7 to work (as an embedded script), but I need the Bates numbering feature.
Thanks for any help you can offer. Ty.
*********BATES NUMBER SCRIPT FROM PDFPEN 7 ********
-- This script places a page number in the bottom left corner of each page using a bates numbering scheme.
tell application "PDFpenPro 7"
if (count documents) > 0 then
-- Get value for what should be prepended to beginning of numbering scheme.
set prepend_value to text returned of (display dialog ¬
"Enter Bates numbering prefix:" with title ¬
"Bates Numbering" default answer ¬
"" buttons {"Cancel", "OK"} ¬
default button "OK")
set pageCount to count (pages of document 1)
repeat with pageNumber from 1 to pageCount
set thePage to page pageNumber of document 1
set currentPage to prepend_value & my add_leading_zeros(pageNumber, 5) as rich text
make new text imprint at end of imprints of thePage with properties {rich text:currentPage, x position:36, y position:36, height:16, width:540}
end repeat
return pageCount
end if
end tell
-- A sub-routine for adding leading zeros to numbers:
-- Courtesy of Apple
-- http://www.apple.com/applescript/sbrt/sbrt-02.html
on add_leading_zeros(this_number, max_leading_zeros)
set the threshold_number to (10 ^ max_leading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (this_number as text)) as string
else
return this_number as text
end if
end add_leading_zeros
***
***************************