iCloud Status Extension Attribute

sanbornc
New Contributor III

Hello All!

Im trying to create an iCloud Status Extension Attribute that checks for the size of the MobileMeAccounts.plist file. When iCloud is signed in the file balloons to 2K, when its not, the file shrinks to 56bytes. So I created this script to let me know if users have signed into their iCloud account, which they are not allowed to. The problem is Im trying to find out if the file is even there are all. I have this first script, which works perfectly at letting me know if iCloud is signed into based on the size of the file:

#!/bin/sh
loggedInUser=$(stat -f%Su /dev/console) 
file=/Users/$loggedInUser/Library/Preferences/MobileMeAccounts.plist
minimumsize=1000
actualsize=$(wc -c <"$file")
if [ $actualsize -ge $minimumsize ]; then
    echo "<result>iCloud is logged in</result>"
else
    echo "<result>iCloud is logged out</result>"
fi

The problem is Im trying to add an element that looks for the existence of the file first, then moves on. So...if the file MobileMeAccounts.plist doesn't exist at all the script should echo "No File" and and exit. This is what Ive come up with but Its not working. What am I doing wrong?

#!/bin/sh
loggedInUser=$(stat -f%Su /dev/console) 
file=/Users/$loggedInUser/Library/Preferences/MobileMeAccounts.plist
minimumsize=1000
actualsize=$(wc -c <"$file")
if [ -e "$file" ]; then   
if [ $actualsize -ge $minimumsize ]; then
    echo "iCloud is logged in"
elif
    echo "iCloud is logged out"
else
    echo "No File" 
fi
2 REPLIES 2

mm2270
Legendary Contributor III

If I had to guess, it's because you are checking for the size of the file outside of the if/then block, which is probably coming up with some result, even if it's just an error that the file doesn't exist, so it's populating that variable, and when it gets to this line

if [ $actualsize -ge $minimumsize ]; then

It actually has something for $actualsize, so it messes up the checks. Is it printing back a "iCloud is logged out" result? If so, you can fix this a few ways. Here's one way:

#!/bin/sh
loggedInUser=$(stat -f%Su /dev/console) 
file="/Users/$loggedInUser/Library/Preferences/MobileMeAccounts.plist"
minimumsize=1000

if [ -e "$file" ]; then
    actualsize=$(wc -c < "$file")
    if [ $actualsize -ge $minimumsize ]; then
        echo "iCloud is logged in"
    else
        echo "iCloud is logged out"
    fi
else
    echo "No File" 
fi

What this version does is first check for the existence of the file, and if true, then grabs the $actualsize file size, and then moves on to comparing it against the $minimumsize variable. If the first test fails (file doesn't exist) it just skips those steps and moves on to the last section where it echos "No file"

Hopefully that will help get it working for you.

As a side note, in the past there were other more reliable methods of checking to see if someone was logged into iCloud, by checking for info in one of the sqlite databases that contains the info. It was able to see which account was actually logged into iCloud. I'm not sure that that method works anymore under the current OS, especially with all the additional security restrictions. But if I can locate one of the old scripts that does this, I'll post it here so you can try it out.

sanbornc
New Contributor III

@mm2270 Thanks! That seems to work perfectly! I would be interested in the script for the sqlite database you mentioned if you want find it. Thanks!