I have an AppleScript that merges several images into a PDF, renames them, OCRs them, and uses the metadata on the first image to set the custom metadata for the pdf. It works - until it tries to delete the original images. The error message does not appear, suggesting the error is deeper.
-- a script to merge image files intoo a PDF, OCR it, use the metadata from the first image as metadata for the PDF,
-- and delete originals
-- currently does not delete orignials, and does not display the error message
tell application id "DNtp"
if selection is {} then error "Select some documents"
set theSelection to selection
set numItems to the (number of items in theSelection) - 1
set mergedName to the name of the first item of theSelection & "-" & the name of the last item of theSelection
set tempDate to creation date of the first item of theSelection
-- Set the metadata of the merged record based on the metadata of the first item in the selection
set firstItemMetadata to custom meta data of item 1 of theSelection
-- merge records
set mergedRecord to (merge records theSelection in current group)
set the name of mergedRecord to mergedName
set the creation date of mergedRecord to tempDate
set custom meta data of mergedRecord to firstItemMetadata
-- OCR the merged PDF
set theSourceGroup to location of mergedRecord
with timeout of 600 seconds
ocr record mergedRecord to theSourceGroup
end timeout
-- Delete the original items
repeat with thisItem in theSelection
try
delete record thisItem
on error errMsg
display dialog "Error deleting item: " & errMsg
end try
end repeat
end tell
Which error is deeper, and which error message does not appear? I didn’t see anything about an error in the thread you linked to (but then I did not really re-read all those posts).
This error in this part of the script does not appear:
-- Delete the original items
repeat with thisItem in theSelection
try
delete record thisItem
on error errMsg
display dialog "Error deleting item: " & errMsg
end try
end repeat
The script runs mostly well, merging the images into a PDF, naming it correctly, and performing OCR but if does not delete the original images. The error message does not appear here, suggesting that it is not trying to delete any of the original images.
tell application id "DNtp"
if selection is {} then error "Select some documents"
set theSelection to selection
set mergedName to the name of the first item of theSelection & "-" & the name of the last item of theSelection
set tempDate to creation date of the first item of theSelection
-- Set the metadata of the merged record based on the metadata of the first item in the selection
set firstItemMetadata to custom meta data of item 1 of theSelection
-- Merge records
set mergedRecord to (merge records theSelection in current group)
set the name of mergedRecord to mergedName
set the creation date of mergedRecord to tempDate
-- OCR records with Nitro PDF Pro
set theSourceGroup to location of mergedRecord
set thePath to path of mergedRecord
tell application "Nitro PDF Pro"
open (POSIX file thePath) as alias
-- does the document need to be OCR'd?
get the needs ocr of document 1
if result is true then
tell document 1
ocr
repeat while performing ocr
delay 1
end repeat
delay 1
close with saving
end tell
--In PDFpen, when no documents are open, window 1 is "Preferences"
--If other documents are open, do not close the App.
if name of window 1 is "Preferences" then
tell application "Nitro PDF Pro"
quit
end tell
end if
else
-- Scan Doc was previously OCR'd or is already a text type PDF.
tell document 1
close without saving
end tell
--In PDFpen, when no documents are open, window 1 is "Preferences"
--If other documents are open, do not close the App.
if name of window 1 is "Preferences" then
tell application "Nitro PDF Pro"
quit
end tell
end if
end if
end tell
-- Check if the mergedRecord exists before attempting to delete original items
if exists mergedRecord then
-- Delete the original items
repeat with thisItem in theSelection
try
delete record thisItem
on error errMsg
display dialog "Error deleting item: " & errMsg
end try
end repeat
else
display dialog "Merged record not found."
end if
end tell
I have no clue about coding but I can logically put logical pieces together to make scripts work. well sometimes. so for the OCR part, I had to adapt it to nitro pdfpen pr -a script I also got somewhere from this forum. I can’t recall. Anyway, I used this ocr because yours wasn’t working for me. for the deletion, I first of all checked if the merged file exist. Using this bits together I can able to use your idea without problems. your script should actually work. maybe with this idea you can make it work better.
I have worked it out now. I’ve tested this on multiple documents and it seems to work well:
-- wrap script in error handling try loop
try
tell application id "DNtp"
-- Check if there are any images selected. If not, display an error message and exit the script.
if selection is {} then error "Select some documents"
-- Get the selected images.
set theSelection to selection
-- Store UUIDs for deletion later
set uuidsToDelete to {}
repeat with thisItem in theSelection
set end of uuidsToDelete to uuid of thisItem
end repeat
-- Get the number of selected images.
set numItems to (number of items in theSelection) - 1
-- Create a name for the merged PDF file.
set mergedName to the name of the first item of theSelection & "-" & the name of the last item of theSelection
-- Get the creation date of the first image.
set tempDate to creation date of the first item of theSelection
-- Set the metadata of the merged PDF file based on the metadata of the first image.
set firstItemMetadata to custom meta data of item 1 of theSelection
-- Merge the images into a PDF file.
set mergedRecord to (merge records theSelection in current group)
-- Set the name and creation date of the merged PDF file.
set the name of mergedRecord to mergedName
set the creation date of mergedRecord to tempDate
-- Set the custom metadata of the merged PDF file.
set custom meta data of mergedRecord to firstItemMetadata
-- Delete the original images
-- to aviod user confusion, this should happen before ocr
-- which can then run as a background process
repeat with uuid in uuidsToDelete
set thisItem to get record with uuid uuid
move record thisItem to (trash group of database of thisItem)
end repeat
-- OCR the merged PDF file and delete the original mergedRecord.
set ItemPath to path of mergedRecord -- Get the path of mergedRecord
if type of mergedRecord is PDF document and word count of mergedRecord is 0 then -- Check if it's a PDF with no OCR yet
with timeout of 600 seconds -- Adjust timeout to your needs
try
set TrashItem to mergedRecord -- Set the item to be potentially moved to trash later
set mergedRecord to ocr file ItemPath with waiting for reply -- OCR the file from its path and wait for the process to complete
on error error_message number error_number
if the error_number is not -128 then
display dialog "An error occurred during OCR: " & error_message
end if
end try
end timeout
end if
end tell
on error errMsg
display dialog "An error occurred: " & errMsg
end try