Not authorized to send Apple events to Finder. (error -1743)

I have a simple ApplScript which I have decided to put into a User Interface.

In Xcode, I have made an AppleScript project and started to copy the code from the AppleScript into Xcode.

From a button I have the following code

on btnFolderClick_(sender)
        set theFolderPath to (choose folder with prompt "Choose The Folder With Scans")
           
        #Count Number Of Images in Folder
        tell application "Finder"
            set numberOfImages to number of files in folder (((theFolderPath)))
        end tell
        
    end btnFolderClick_

When I Run it, I am getting the following error...

-[AppDelegate btnFolderClick:]: Not authorized to send Apple events to Finder. (error -1743)

I sort of understand the error but not sure how to proceed.

Can anyone offer any solution please.

Thank You

Not authorized to send Apple events to Finder. (error -1743)
 
 
Q