Suppose You want to write a Perl script that takes advantage of Keychain Access as a place to read settings from. It can be done. But what about manipulating those Keychain data? AppleScript can do that. As a generic example, "MyProgram" uses Keychain for:
[robg adds: This is a long, detailed hint that was first posted on the author's blog. I'm recreating it here in case the original ever vanishes. You can read the fully entry here or on the author's blog.]
Here's the setup AppleScript: Here is the Perl script that reads values stored in Keychain and uses them in the program (here the usage is simple printout of those values). Note that the AppleScript actually doesn't start or stop the Perl script. It just writes the RUNSTATE into Keychain. Gluing the two parts together can be accomplished in different ways. For me, the Perl script is a launchd job, thus the solution is to restart the corresponding launchd job after changing MyProgram preferences in Keychain.
Configure com.foo.MyProgram.plist file as follows and put under one of the launchd-monitored places; I chose ~/Library/LaunchAgents: And the modified AppleScript simply restarts the launchd job - meaning it will restart Perl script to read the new preferences: Hopefully it's apparent that the fields can contain anything that your Perl script uses. Store whatever varibles you want. And each key field can contain many paremeters, divided by some predefined char/string; reading them out in Perl script simply takes some modification the regex part. i.e., we have a key that's Service Name whose value is "MyProgram," and we want to store all the variables for Perl to use in the comments field, each value divided by colon (valueone:valuetwo:valuethree:valuefour:valuefive):
Then the possible Perl script would be:
Finally, some basic AppleScripts to work with Keychain Access:
[robg adds: Any errors in the above are my fault; please check the author's blog if this version doesn't work.]
- storing service name for MyProgram (which is actually the real identity for the program, while the system call security uses -s as an argument)
- username for MyProgram
- password for MyProgram
- URI for MyProgram
- runstate for MyProgram
- Name for RUNSTATE
- Account for PROGRAMID
- Where for SERVICENAME
- Comments for PROGRAMURI
- Password for PROGRAMPW
[robg adds: This is a long, detailed hint that was first posted on the author's blog. I'm recreating it here in case the original ever vanishes. You can read the fully entry here or on the author's blog.]
Here's the setup AppleScript: Here is the Perl script that reads values stored in Keychain and uses them in the program (here the usage is simple printout of those values). Note that the AppleScript actually doesn't start or stop the Perl script. It just writes the RUNSTATE into Keychain. Gluing the two parts together can be accomplished in different ways. For me, the Perl script is a launchd job, thus the solution is to restart the corresponding launchd job after changing MyProgram preferences in Keychain.
Configure com.foo.MyProgram.plist file as follows and put under one of the launchd-monitored places; I chose ~/Library/LaunchAgents: And the modified AppleScript simply restarts the launchd job - meaning it will restart Perl script to read the new preferences: Hopefully it's apparent that the fields can contain anything that your Perl script uses. Store whatever varibles you want. And each key field can contain many paremeters, divided by some predefined char/string; reading them out in Perl script simply takes some modification the regex part. i.e., we have a key that's Service Name whose value is "MyProgram," and we want to store all the variables for Perl to use in the comments field, each value divided by colon (valueone:valuetwo:valuethree:valuefour:valuefive):

•
[9,941 views]

