-- Prompt to select the invoice set theInvoice to choose file with prompt "Please select an invoice:" -- Get enclosing folder name tell application "Finder" to set destinationFolder to container of theInvoice log "Destination folder: " & destinationFolder tell application "Pages" activate open theInvoice log "Opening: " & theInvoice try set documentName to the name of the front document -- Set new file extension from .pages to .pdf set newFilename to my findAndReplaceInText(newPagesName, ".pages", ".pdf") log "New filename: " & newFilename set the newFilePath to (destinationFolder as string) & newFilename log "New file path: " & newFilePath -- Export to pdf log "Exporting..." export front document to file newFilePath as PDF on error errorMessage display alert "Error: " message errorMessage end try log "Closing..." close every document without saving end tell log "Done!" -- Show exported pdf in Finder tell application "Finder" activate reveal document file newFilePath log "Renaming original invoice: " & newPagesName set name of theInvoice to newPagesName end tell -- Function to find and replace text in a string on findAndReplaceInText(theText, theSearchString, theReplacementString) set AppleScript's text item delimiters to theSearchString set theTextItems to every text item of theText set AppleScript's text item delimiters to theReplacementString set theText to theTextItems as string set AppleScript's text item delimiters to "" return theText end findAndReplaceInText