Is it possible to query the current user’s Apple ID from the command line

apple-idapplescriptcommand lineterminal

Is it possible to query the current user's Apple ID from the command line? I want to create an AppleScript (run-only) app that will run only for a specific user (or at least for a user signed in with a specific AppleID), but I can't find any command (or third-party utility) that will do this.

Does something like this exist?

Best Answer

The currently logged in Apple IDs for iCloud are stored in the MobileMeAccounts preferences. You can query the entire store with:

defaults read MobileMeAccounts Accounts

To return just the first account ID (an email address nowadays):

/usr/libexec/PlistBuddy -c "print :Accounts:0:AccountID" ~/Library/Preferences/MobileMeAccounts.plist