Getting approval to access the Mail app

I am writing a program that connects to AppleMail (getting the number of unread messages). It is using AppleScript to do the connection to Mail.

The issue that I am having is that I get an error saying:
Error: {
NSAppleScriptErrorAppName = Mail;
NSAppleScriptErrorBriefMessage = “Not authorized to send Apple events to Mail.”;
NSAppleScriptErrorMessage = “Not authorized to send Apple events to Mail.”;
NSAppleScriptErrorNumber = “-1743”;
NSAppleScriptErrorRange = “NSRange: {47, 12}”;
}

So, clearly this is an issue with privacy/security settings.

I have googled this, and added some items to info.plist and .entitlements but they didn’t seem to work (not even sure I added the right things).

Any advice would be great. Many thanks in advance.

Ian

Mac is running Sonoma.