知道如何在命令或 AppleScript 中删除 system.keychain 中的项目吗?
目前我正在开发一个小应用程序,我想删除机场设置。
首先,我使用shell命令networksetup删除首选网络,然后通过AppleScript删除keychain中记录的密码。但我发现机场的密码记录在system.keychain和login.keychain中。我编写了 AppleScript,例如:
tell application "Keychain Scripting"
set keychainName to "Login.keychain" -- "system.keychain"
set gkeyCount to count generic key of keychain keychainName
repeat with keyIndex from 1 to gkeyCount
log keyIndex
if (account of generic key keyIndex of keychain keychainName is "ABCD") then
delete generic key keyIndex of keychain keychainName
log "find the key"
end if
end repeat
end Tell
对于钥匙串“login.keychain”,没问题,但是对于钥匙串“System.keychain”,它失败并显示弹出窗口“钥匙串脚本出现错误:文件未以写入权限打开” ”。
有什么想法吗?
Currently I am developing a small application that I wanna to remove the airport settings.
Firstly, I use shell command networksetup to delete the preferred networks, and then delete the recorded-password in keychain by AppleScript. But I found that the airports' passwords recorded in both system.keychain and login.keychain. I write the AppleScript such as:
tell application "Keychain Scripting"
set keychainName to "Login.keychain" -- "system.keychain"
set gkeyCount to count generic key of keychain keychainName
repeat with keyIndex from 1 to gkeyCount
log keyIndex
if (account of generic key keyIndex of keychain keychainName is "ABCD") then
delete generic key keyIndex of keychain keychainName
log "find the key"
end if
end repeat
end tell
For keychain "login.keychain", it's no problem, but for keychain "System.keychain", it failed with a popup show me "Keychain Scripting got an error: File not open with write permission."
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:这不起作用。啊,好吧,无论如何,这只是一个猜测。我将答案留给后代。
我的猜测是问题是系统钥匙串被锁定。我无法真正测试这一点,因为我不想从我的钥匙串中删除东西,但我确实确认我的系统钥匙串默认为锁定状态,这似乎会导致您看到的错误。因此,我会做这样的事情:
这个想法是,如果需要的话,首先解锁钥匙串,然后确保在完成后重新锁定它。让我知道这是否有效——就像我说的,我没有任何想要测试的东西。
Edit: This doesn't work. Ah well, it was just a guess anyway. I'm leaving the answer up for posterity.
My guess is that the problem is that the system keychain is locked. I can't really test this, since I don't want to delete things from my keychain, but I did confirm that my system keychain defaults to being locked, and this seems like the sort of thing which would cause the error you see. Thus, I'd do something like this:
The idea is to first unlocked the keychain if you need to, and then make sure you relock it when you're done. Let me know if this does or doesn't work—like I said, I had nothing I wanted to test this on.
我用 Objective-C 得到了解决方案:
}
注意:以 root/admin 身份运行。
:)
I'v got the solution with Objective-C:
}
Note: run this as root/admin.
:)
您需要以写入权限打开文件:请参阅 books_google_com 来自 AppleScript:Mac OS X 上的脚本和自动化综合指南
You need to open the file with writing permissions: see books_google_com from AppleScript: the comprehensive guide to scripting and automation on Mac OS X