Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tk_mac

macrumors member
Original poster
Dec 26, 2019
82
8
I have a Java program that I run in Eclipse which executes AppleScript for certain tasks. It works fine in Mojave, but in Big Sur, the AppleScripts fail with the error "AppleScript Not authorized to send Apple events to [app] (-1743)"

In Mojave, the user is prompted (one time on the first run) with "'Eclipse.app' wants access to control '[app being controlled by Applescript]'" Once this prompt is approved, then Eclipse is added in the System Preferences under Security & Privacy -> Privacy -> Automation and a checkbox is included and activate for each of the applications that the AppleScript manipulates (Finder and iTunes or Music app depending on OS version), and thereafter, my Java program can run its AppleScripts.

In Big Sur, the prompts never appear and the AppleScript immediately errors out. Furthermore, I see no way to manually add approvals under System Preferences -> Security & Privacy -> Privacy -> Automation and there is no way I can find for how I can approve this automation on my own Mac.

How do I invoke AppleScript on Big Sur from other languages such as Java?

Please note that the Java program constructs AppleScript content at run time dependent on data, so the scripts are not fixed.

The Java code to run a script goes like this....

Java:
    String script = <Some expression to form the content of the script depending on data>;
    String[] myargs = { "osascript", "-e", script};
    try {
        Process process = runtime.exec(myargs);
        process.waitFor(); // Wait for the script to complete before checking success/fail.
        if (process.exitValue() == 0) { // Success
            //On Mojave, execution takes this path and Applescript works, after the one time user approval.
            System.out.println("success");
        } else { // Fail
            //On Big Sur, executions takes this path and outputs
            //35:57: execution error: Not authorized to send Apple events to Finder. (-1743)
            InputStream is = process.getErrorStream();
            final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        }
    } catch (Exception exc) {
        System.out.println(exc.getMessage());   
    }

Thanks in advance for any help!
 

casperes1996

macrumors 604
Jan 26, 2014
7,593
5,764
Horsens, Denmark
Does your Info.plist contain
NSAppleEventsUsageDescription
?


Does your entitlements contain
com.apple.security.automation.apple-events
?

Is your program code signed?

(Assuming you're building this into a .app bundle)
 

tk_mac

macrumors member
Original poster
Dec 26, 2019
82
8
Does your Info.plist contain
NSAppleEventsUsageDescription
?


Does your entitlements contain
com.apple.security.automation.apple-events
?

Is your program code signed?

(Assuming you're building this into a .app bundle)
I am not building the java app for distribution. I am running it in the IDE, Eclipse. There is no Info.plist, entitlements, or code signing. In Mojave, it is regarded to be Eclipse itself that is doing the automation, and there, I see Automation permissions for Eclipse to control "Finder" and "iTunes". How do I get the similar in Big Sur, for Eclipse to control "Finder" and "Music"?
 

casperes1996

macrumors 604
Jan 26, 2014
7,593
5,764
Horsens, Denmark
I am not building the java app for distribution. I am running it in the IDE, Eclipse. There is no Info.plist, entitlements, or code signing. In Mojave, it is regarded to be Eclipse itself that is doing the automation, and there, I see Automation permissions for Eclipse to control "Finder" and "iTunes". How do I get the similar in Big Sur, for Eclipse to control "Finder" and "Music"?
Hm. Sounds a tad odd to me that it'd be Eclipse needing the permissions. What happens if you run the program with java from the Terminal app? Same behaviour?
 

tk_mac

macrumors member
Original poster
Dec 26, 2019
82
8
Hm. Sounds a tad odd to me that it'd be Eclipse needing the permissions. What happens if you run the program with java from the Terminal app? Same behaviour?
I will try running it with Terminal when back on my BigSur machine. But in Mojave, Eclipse most definitely asked for the permissions and I granted them, and my code runs well. I see the Eclipse Permissions under System Preferences -> Security & Privacy -> Privacy -> Automation. How can I grant these to Big Sur, when it don't prompt me for the permissions?
 

casperes1996

macrumors 604
Jan 26, 2014
7,593
5,764
Horsens, Denmark
I will try running it with Terminal when back on my BigSur machine. But in Mojave, Eclipse most definitely asked for the permissions and I granted them, and my code runs well. I see the Eclipse Permissions under System Preferences -> Security & Privacy -> Privacy -> Automation. How can I grant these to Big Sur, when it don't prompt me for the permissions?

If the app isn't in the list already it means, as far as macOS is concerned, the app has never attempted to do anything that requires the permission. You can't forcefully add it. But you can reset the permissions database for the app, which may prompt it to ask if something has gone weirdly wrong. Use the tccutil command to achieve this

1635984101706.png
 

tk_mac

macrumors member
Original poster
Dec 26, 2019
82
8
Solved. Thanks for the replies.
In Big Sur, I had already tried the command
tccutil reset AppleEvents
and it did not seem to help, although it did remove the approvals of the other apps I had approved in Security & Privacy -> Privacy -> Automation.
Later Eclipse would not start at all giving two errors immediately at launch. "You do not have permission to open the application 'Eclipse.app'" and "eclipse quit unexpectedly". Furthermore, Eclipse did not appear in GateKeeper so I could not grant a permission for it to run. I reinstalled Eclipse IDE 2021‑09 from the .dmg provided by the Eclipse organization and found it also fixed my issue running AppleScript from Java, as I was presented with the appropriate requests for permissions when I ran my Java program.
 
  • Like
Reactions: grandM

casperes1996

macrumors 604
Jan 26, 2014
7,593
5,764
Horsens, Denmark
Solved. Thanks for the replies.
In Big Sur, I had already tried the command
tccutil reset AppleEvents
and it did not seem to help, although it did remove the approvals of the other apps I had approved in Security & Privacy -> Privacy -> Automation.
Later Eclipse would not start at all giving two errors immediately at launch. "You do not have permission to open the application 'Eclipse.app'" and "eclipse quit unexpectedly". Furthermore, Eclipse did not appear in GateKeeper so I could not grant a permission for it to run. I reinstalled Eclipse IDE 2021‑09 from the .dmg provided by the Eclipse organization and found it also fixed my issue running AppleScript from Java, as I was presented with the appropriate requests for permissions when I ran my Java program.
Peculiar, but glad it's working!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.