You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Mac命令行工具授予EKEventStore访问权限

Fixing Command-Line App Access to Mac Calendar (Mojave/Xcode 10.1)

I’ve run into this exact issue before—command-line tools don’t play by the same rules as GUI apps when it comes to macOS privacy permissions. Let’s break down why your code isn’t working and how to fix it:

1. Make Sure Your Info.plist is Actually Being Used

Xcode’s default command-line tool template doesn’t embed the Info.plist into the executable, so macOS can’t see your NSCalendarsUsageDescription message. Here’s how to fix that:

  • Select your command-line target in Xcode.
  • Head to Build Settings.
  • Find the Info.plist File setting and confirm it points to your plist file (double-check you’ve added the NSCalendarsUsageDescription key with a clear reason, like "Access calendars to view and manage events").
  • Look for Create Info.plist Section in Binary and set it to YES. This embeds the plist directly into your tool so macOS can read it.

2. Code-Sign Your Tool (Even Self-Signed Works)

Mojave’s privacy system requires apps to be code-signed before it’ll show permission prompts. You don’t need an Apple Developer account for this—just create a self-signed certificate:

  • Open Keychain Access → go to Certificate Assistant → Create a Certificate.
  • Name it something like "My CLI Cert", pick Code Signing as the type, and hit Create.
  • Back in Xcode, go to your target’s Build Settings → find Code Signing Identity and select your new certificate.
  • Rebuild your tool.

3. Trigger the Permission Prompt with a Dummy UI

The problem is, command-line tools don’t have a UI context, so macOS won’t pop up the permission dialog on its own. We can work around this by creating a tiny temporary GUI app to trigger the prompt:

import Cocoa
import EventKit

// Set up a minimal app context to get the permission prompt to show
let app = NSApplication.shared
app.setActivationPolicy(.regular)
app.activate(ignoringOtherApps: true)

// Request calendar access
let eventStore = EKEventStore()
eventStore.requestAccess(to: .event) { granted, error in
    if granted {
        print("Success! Calendar access granted.")
        // Do your event store work here
    } else {
        print("Oops, access denied: \(error?.localizedDescription ?? "No error message")")
    }
    // Quit the dummy app once we're done
    NSApplication.shared.terminate(nil)
}

// Run the app loop to handle the permission dialog
app.run()

This will briefly show a blank window, bring up the permission prompt, then close once you choose Allow or Deny.

4. Manual Permission Grant (If All Else Fails)

If the above steps don’t trigger the prompt, you can manually add your tool to the allowed list:

  • Open System Preferences → Security & Privacy → Privacy tab → Calendars.
  • Click the lock to make changes, then drag your built executable (from Xcode’s Products folder) into the list.
  • Check the box next to your tool to grant access.

Keep in mind that if you plan to share this tool with others, you’ll need proper Apple Developer code-signing and notarization, but for personal use, the self-signed certificate and above steps should work perfectly.

内容的提问来源于stack exchange,提问作者Tony

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:58:06