Hello i'm working on little service for os x, i was wondering how can i grab t开发者_JAVA百科he MobileMe account name (if person is logged in in osx) for further use in script?
Would the following snippet do what you ask?
dscl localhost -readpl /Local/Default/Users/$(id -un) dsAttrTypeNative:LinkedIdentity mac.com | grep -A1 '>name<' | sed -n 's/.*string>\(.*\)<\/string>.*/\1/p'
The first item reads the mac.com linked identity dict from directory services for the user that's making the request, the next element finds the name field and the last element takes the user's .mac name and displays it.
to put it in the clipboard, you issue that command, with the following ending:
dscl localhost -readpl /Local/Default/Users/$(id -un) dsAttrTypeNative:LinkedIdentity mac.com | grep -A1 '>name<' | sed -n 's/.*string>\(.*\)<\/string>.*/\1/p' | pbcopy
which copies the value into the clipboard (that bit I missed in the question earlier)
Since MobileMe stores certificates and public/private keys in the keychain, you can use
security find-identity -v
To generate a list of valid identities, from where you can extract the MobileMe username out of the identity with (MobileMe Sharing Certificate)
in its name.
To do this, I could only come up with this working, albeit ugly solution that uses sed
to print the characters between the double quotes and awk
to print only the username:
security find-identity -v | grep MobileMe | sed -n 's/\(.*\)\"\(.*\)\"\(.*\)/\2/p' | awk '{print $1}'
(If someone finds a more elegant solution for this, I´d be happy to hear!)
精彩评论