-2

I have a couple of AppleScript apps that stays in the menu tray and I want its app icon not to appear on the dock. I tried following the solution in this old post (10yrs ago) but it no longer works, it throws a security error that I couldn't resolve via System Preferences settings.

Here is the error:

Not authorized to send Apple events to System Events.

Not authorized to send Apple events to System Events. (-1743)

Surprisingly, when I did a small quick one, it worked.

use framework "Foundation"
use framework "AppKit"

use scripting additions

property StatusItem : missing value
property selectedMenu : ""
property defaults : class "NSUserDefaults"
property internalMenuItem : class "NSMenuItem"
property externalMenuItem : class "NSMenuItem"
property newMenu : class "NSMenu"

makeStatusBar()
makeMenus()

on makeStatusBar()
    set bar to current application's NSStatusBar's systemStatusBar
    set StatusItem to bar's statusItemWithLength:-1.0

    -- set up the initial NSStatusBars title
    StatusItem's setTitle:"poc"

    -- set up the initial NSMenu of the statusbar
    set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"
newMenu's setDelegate:me
    StatusItem's setMenu:newMenu
end makeStatusBar

on makeMenus()
    newMenu's removeAllItems() -- remove existing menu items

    set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:"Quit" action:("quitAction:") keyEquivalent:"")
    (newMenu's addItem:thisMenuItem)
    (thisMenuItem's setTarget:me) -- required for enabling the menu     item
end makeMenus

on idle
    log "test"
end idle


on quitAction:sender
    quit me
end quitAction:

I guess my next step is to add the functionality one at a time to figure out which statement is triggering the problem.

user3579815
  • 522
  • 3
  • 12
  • 1
    Just declaring your application to be an agent shouldn’t have anything to do with security preferences - what is the error? You might also post some code demonstrating the issue. – red_menace Jun 11 '21 at 12:38
  • re menu tray, I don't know what it's called Mac OS, in Windows it is system tray. I'm referring to the place where we see the time, siri, username, battery, wifi. – user3579815 Jun 11 '21 at 13:31
  • JSYK IMO Posting _code_ that actually works without posting the _code_ that doesn't work, does not help us to determine why your real _code_ is having an issue. – user3439894 Jun 11 '21 at 13:51
  • The error is due to security settings - if your application uses another’s scripting terminology (`System Events`, for example), it needs to be given accessibility permissions. Check that the application has been added to the `Security & Privacy` preferences. – red_menace Jun 11 '21 at 15:47
  • Yeah I know, the code that don't work has a lots if imported libraries and would so it would be tricky to post all of it. So far the discussion has helped me get enough information to figure out what the issue is. I'll can update it once I find the culprit. @red_menace I added it in the Security & Privacy, remember that the failing script works normally, but only stops to work after modifying the Info.plist. – user3579815 Jun 12 '21 at 02:12
  • I think I found the issue. So what I did is: after modifying the plist file, I had to remove the app and re-add it to the Security & Privacy. If this is not done, I get the security error, and trying to re-run the script app results in no feedback, I don't see the app in the menu nor in the dock. So thank you, for helping me unlock the mystery. I'm just glad I could now run more scripts without cluttering my dock. – user3579815 Jun 12 '21 at 03:40
  • 1
    Use the _command_ `defaults write '/path/to/name.app/Contents/Info.plist' LSUIElement -bool yes` and do this before your app gets added to **System Preferences** > **Security & Privacy** > **Privacy**. If it was added before then remove it, modify the Info.plist _file_ and add it back. See if that makes a difference. – user3439894 Jun 12 '21 at 03:47
  • 1
    Be careful when modifying the application, as the system will see it as a different one. The best approach after making changes is to just release a new code-signed version and re-add it to the accessibility permissions. – red_menace Jun 12 '21 at 03:48

1 Answers1

0

The LSUIElement key still works in Big Sur, except that it requires an 'boolean' value, not a 'string' value. At least, adding the following key-value pair to the info.plist of an applescript app works to hide its dock icon on my Big Sur installation.

<key>LSUIElement</key>
</true>

That being said, check if your app has the appropriate privileges in System Preferences > Security & Privacy > Privacy > Automation. You should see your app listed, with System Events checked under its name.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17