gpt4 book ai didi

macos - 在 Xamarin 中使用 NSAppleEventsUsageDescription 获取登录项

转载 作者:行者123 更新时间:2023-12-03 17:46:31 29 4
gpt4 key购买 nike

我需要从代码中运行 AppleScript 来获取 LoginItems 名称。我可以从 Xcode 项目中做到这一点。但是当我对 Xamarin 执行相同操作时,它失败了。

我在 Xamarin 项目中缺少什么?任何获得登录项目的帮助将不胜感激。

Xcode 项目代码:

NSString *script = @"tell application \"System Events\" to get the path of every login item";

NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:script];

NSDictionary* errorDict;

NSAppleEventDescriptor *descriptor = [appleScript executeAndReturnError: &errorDict];

Xcode 项目 plist 值: enter image description here最后的值:

我在 Xamarin 中做了类似的事情:

var script = "tell application \"System Events\" to get the path of every login item";
var appleScript = new NSAppleScript(script);
var descriptor = appleScript.ExecuteAndReturnError(out var errorInfo);

info.plist

enter image description here

我得到的 Xamarin errorInfo 值是:

{{
NSAppleScriptErrorAppName = "System Events";
NSAppleScriptErrorBriefMessage = "Not authorized to send Apple events to System Events.";
NSAppleScriptErrorMessage = "Not authorized to send Apple events to System Events.";
NSAppleScriptErrorNumber = "-1743";
NSAppleScriptErrorRange = "NSRange: {44, 4}";
}}

但它不应该发生,因为我在 plist 中添加了 Privacy 部分。

最佳答案

获取错误消息/编号并拥有正确的 Info.plist 条目:

<key>NSAppleEventsUsageDescription</key>
<string>StackOverflow needs access to your System Events 😎</string>

注意:您应该通过编辑器与 VS IDE 仔细检查 Info.plist 文件,以确保 NSAppleEventsUsageDescription key 已正确定义,如上所示。

意味着您没有获得安全弹出窗口来授予权限。

enter image description here

这意味着用户在最初请求时拒绝了权限访问。

打开:

System Preferences / Security & Privacy / Privacy Tab / Automation

在列表中找到您的应用并手动授予其权限:

enter image description here

注意:我一直通过 Xamarin.Mac 使用 AppleScript|JavaScript 进行 macOS 自动化,所以我知道 Xamarin 不是问题所在。如果事实证明这不是自动化权限最初被拒绝,我想听听有关该问题的信息,因为这是我见过的唯一一个问题(我的所有代码,5 年以上,检查该错误并指示让用户手动更正问题,因为他们在第一次询问时拒绝访问)。

关于macos - 在 Xamarin 中使用 NSAppleEventsUsageDescription 获取登录项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56166380/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com