我正在尝试使用AppleScript来控制ApplePhotos
来自 QT C++ 应用程序中的应用程序,该应用程序必须在 macOS App Store 所需的沙箱环境中运行。
我尝试通过 QProcess 启动来运行 AppleScriptosascript
, 像这样 :
const QString aScript = QString(
"tell application \"Photos\"\n"
" set selMedia to (get media items whose id contains \"%1\")\n"
" if not (album \"Trash from %2\" exists) then\n"
" make new album named \"Trash from %2\"\n"
" end if\n"
" add selMedia to album \"Trash from %2\"\n"
"end tell").arg(fileNameNoExt, APP_NAME);
QProcess script;
script.setProgram("osascript");
script.setArguments(QStringList() <<
"-e" << aScript);
script.start();
script.waitForFinished();
int exitCode = script.exitCode();
if(exitCode!=0){
QString outMsg = script.readAllStandardOutput();
QString errorOut = script.readAllStandardError();
// Warn user and output message...
}
这在沙箱之外工作得很好,将库中所需的项目添加到所需的相册中。然而,在沙箱内,我得到了errAEPrivilegeError
。寻找在Console
对于错误事件,它指出:
AppleEvents/sandbox: Returning errAEPrivilegeError/-10004 and denying dispatch of event core/getd from process '<private>'/0x0-0x4d84d8, pid=31654, because it is not entitled to send an AppleEvent to this process.
这是我签署应用程序时的权利文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.scripting-targets</key>
<dict>
<key>com.apple.Photos</key>
<array>
<string>com.apple.Photos.library.read-write</string>
</array>
</dict>
</dict>
</plist>
当应用程序首次运行(沙盒)并启动 AppleScript 时,我收到系统权限框,表示该应用程序要求控制“照片”应用程序。当然,我单击“确定”,但随后出现错误。在下次运行时,由于已经授予了权限,因此不会出现系统权限框,但它仍然会失败。当我使用终端命令重置权限时tccutil reset All [my app bundle id]
,然后在下次执行应用程序和 AppleScript 时,我再次获得权限框。
如果我删除com.apple.security.scripting-targets
权利,我没有收到系统权限请求,并且错误不同:从我的应用程序中,错误输出表明照片未运行,从控制台它说由于沙箱而被拒绝。如果我保留 scripting-targets 和 com.apple.Photos 但删除 library.read-write 我会再次收到 errAEPrivilegeError 。
我试图只测试make new album named "Trash [...]"
但这仍然失败并出现 errAEPrivilegeError。即使只告诉照片quit
与AppleScript(根据Photos.app/Contents/Resources/Photos.sdef
除了脚本之外不需要任何特定权限,我还得到 errAEPrivilegeError。唯一有效的命令是activate
.
另外,如果我尝试不将 qprocess 作为单独的进程运行,QProcess::execute
(如果沙箱问题可能是因为它不是运行脚本的主应用程序?),我仍然得到相同的 errAEPrivilegeError。
我缺少什么才能让它工作,或者在沙箱中时我应该使用什么其他方法从 C++/QT 运行 applescript ?