Error in Apple script for saving password-protected PDF

Can anyone please help me find a solution for the error I'm getting? MacOS is Monterrey and this script used to work in Big Sur.

It seems it is unable to find splitter group 3 (which would the page style). And the same happens when it tries to find "PDF" splitter and save as PDF.

I would appreciate any help/suggestions you can provide.

-- Don't change this

set FixedPassword to "somePW"

--Change the following for each Review accordingly

set exam to "2"
set session to "5"
set VarPassword to "anotherPW"

set outpath to "/Users/user/Dropbox/Physics" & " - " & "Current/2023-1-Spring/UF/2." & " " & "Chapter" & " " & "Reviews/" & ¬
	"Exam" & " " & exam & " " & "Session" & " " & session & "/E" & exam & "S" & session & ".pdf"

set SaveFolder to "/Users/user/Dropbox/Physics" & " - " & "Current/2023-1-Spring/UF/2." & " " & "Chapter" & " " & "Reviews/" & ¬
	"Exam" & " " & exam & " " & "Session" & " " & session

set FileName to "E" & exam & "S" & session & " " & "(Password" & " - " & VarPassword & ").pdf"


--Saving PDF with password protection
tell application "Preview"
	activate
	open outpath
end tell


activate application "Preview"
tell application "System Events"
	tell process "Preview"
		keystroke "p" using command down
		delay 0.5
		tell front window
			repeat until exists sheet 1
				delay 1
			end repeat
			tell sheet 1
				tell splitter group 3
					click pop up button 3
					click menu item "Review 216 by 279 mm" of menu 1 of pop up button 3
				end tell
				tell splitter group 3
					click menu button "PDF"
					repeat until exists menu 1 of menu button "PDF"
						delay 3
					end repeat
					click menu item "Save as PDF" of menu 1 of menu button "PDF"
				end tell
			end tell
		end tell
		
		-- Make sure the save dialog is visible
		repeat until exists sheet 1 of sheet 1 of front window
			delay 5
		end repeat
		
		tell sheet 1 of sheet 1 of front window
			click button "Security Options..."
		end tell
		
		tell window "PDF Security Options"
			set selected to true
			set focused to true
			(* click the checkbox to on *)
			-- NOTE: for some reason there is a delay of about 6 seconds here, I do not know why
			tell checkbox "Require password to open document"
				click
			end tell
			(* add the password and confirm *)
			keystroke VarPassword
			keystroke (ASCII character 9)
			keystroke VarPassword
			
			tell its checkbox "Require password to copy text, images and other content"
				click
			end tell
			(* add the password and confirm *)
			keystroke FixedPassword
			keystroke (ASCII character 9)
			keystroke FixedPassword
			
			click button "OK"
		end tell
		
		repeat until exists sheet 1 of sheet 1 of front window
			delay 0.2
		end repeat
		
		-- Press command+shift+g to show the "Go" drop down sheet
		keystroke "g" using {command down, shift down}
		repeat until exists sheet of sheet 1 of sheet 1 of front window
			delay 0.2
		end repeat
		
		delay 0.5
		keystroke SaveFolder
		delay 0.5
		
		click button "Go" of sheet of sheet 1 of sheet 1 of front window
		
		-- Now that we are in our desired folder, set the file name and save
		set value of text field 1 of sheet 1 of sheet 1 of front window to FileName
		
		click button "Save" of sheet 1 of sheet 1 of front window
		
	end tell
end tell

But it gives the following error: https://i.stack.imgur.com/uDYDC.jpg

Your approach seems far from the correct answer.

My AppleScript works on macOS 13.3beta now.

use AppleScript version "2.8"
use scripting additions
use framework "Foundation"
use framework "PDFKit"


property kCGPDFContextOwnerPassword : a reference to current application's kCGPDFContextOwnerPassword
property NSDictionary : a reference to current application's NSDictionary
property kCGPDFContextUserPassword : a reference to current application's kCGPDFContextUserPassword
property |NSURL| : a reference to current application's |NSURL|
property PDFDocument : a reference to current application's PDFDocument

set userPassword to "12345"
set ownerPassword to "01234"

set aPath to (choose file of type {"com.adobe.pdf"} with prompt "Select PDF to Encrypt")
set newF to choose file name with prompt "Select path & file name of password protected PDF"
set aRes to my encryptPDF(aPath, newF, ownerPassword, userPassword)




on encryptPDF(aPath, newF, ownerPassword, userPassword)
	set aURL to |NSURL|'s fileURLWithPath:(POSIX path of aPath)
	set aPDFdoc to PDFDocument's alloc()'s initWithURL:aURL
	
	set anEncF to aPDFdoc's isEncrypted()
	
	--Not encrypted
	if anEncF = false then
		set newFp to POSIX path of newF
		if newFp does not end with ".pdf" then
			set newFp to newFp & ".pdf"
		end if
		(*
CFStringRef kCGPDFContextAuthor;
CFStringRef kCGPDFContextCreator;
CFStringRef kCGPDFContextTitle;
CFStringRef kCGPDFContextOwnerPassword;
CFStringRef kCGPDFContextUserPassword;
CFStringRef kCGPDFContextAllowsPrinting;-
CFStringRef kCGPDFContextAllowsCopying;--boolean CFStringRef kCGPDFContextOutputIntent;-
CFStringRef kCGPDFContextOutputIntents;
CFStringRef kCGPDFContextSubject;
CFStringRef kCGPDFContextKeywords;
CFStringRef kCGPDFContextEncryptionKeyLength;
*)
		set aDic to NSDictionary's dictionaryWithObjects:{ownerPassword, userPassword} forKeys:{kCGPDFContextOwnerPassword, kCGPDFContextUserPassword}
		
		set aRes to aPDFdoc's writeToFile:newFp withOptions:aDic
		return aRes as boolean
	else
		--Already Encrypted
		return false
	end if
	
end encryptPDF

Error in Apple script for saving password-protected PDF
 
 
Q