can i determine who's signed in to the native mail app?

kashaguilfoyle
New Contributor

Is there a way to create a group or report that will let us know who is signed in to the mail.app? 

We are in the process of implementing something new that we need to get this number for. I saw something about creating an EA but as a new JAMF admin, I'm still hesitant on if this is correct.

 

Any help is appreciated.

5 REPLIES 5

kgam
Contributor

I made the following extension attribute to display the default mail client but stopped using it again as I didn't find it 100% reliable. But maybe you can find some inspiration in it or have more luck using it.

user=$(/usr/bin/stat -f%Su /dev/console)
plist="/Users/$user/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"

if [ -e $plist ]; then
    client=$(defaults read $plist |grep -B 1 mailto |awk -F"\"" '/LSHandlerRoleAll/ {print $2}')
    if [ "$client" = "com.microsoft.outlook" ]; then
        echo "<result>Microsoft Outlook</result>"
    elif [ "$client" = "com.apple.mail" ]; then
        echo "<result>Apple Mail</result>"
    else
        echo "<result>N/A</result>"
    fi
else
    echo "<result>N/A</result>"
fi

exit 0

el2493
Contributor III

@kgam Can you share what wasn't working reliably with your EA? I was looking to achieve the same thing (moving away from a python EA) and inadvertently ended up writing an almost identical EA. It seems to work for me, though I've done very limited testing.

It's been a while now but as I remember I just started to see computers in the inventory, that I knew where using e.g. Apple Mail, report an empty EA back. My script seemed to work okay so not sure if it was the file I was checking against that was different on some computers. But in the end it turned out that very few users where using Apple Mail so I didn't really need the EA.

el2493
Contributor III

Thanks. My EA looks like this:

 

#!/bin/zsh

loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
loggedInUserHome=$(/usr/bin/dscl . -read "/Users/$loggedInUser" NFSHomeDirectory | /usr/bin/awk '{print $NF}')
launchServicesPlist="$loggedInUserHome/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"

if [[ -e "$launchServicesPlist" ]]; then
	mailtoClient=$(defaults read "$launchServicesPlist" | grep -B1 mailto)
	if [[ "$mailtoClient" == *"com.microsoft.outlook"* ]]; then
        echo "<result>com.microsoft.outlook</result>"
    elif [[ "$mailtoClient" == *"com.apple.mail"* ]]; then
        echo "<result>com.apple.mail</result>"
    elif [[ -z "$mailtoClient" ]]; then
    	echo "<result>No default value</result>"
    else
        echo "<result>Unknown</result>"
    fi
else
	# If there's no plist, user hasn't set a default app so it's Mail
	echo "<result>Apple Mail Default</result>"
fi

 

In testing it worked, but when I put it in Jamf and started getting results they didn't seem to be accurate. I already had a Python-based EA that seems to work fine, but since it's Python and Python 2 is being remove from Macs soon, I was trying to find or build an alternative:

 

#!/usr/bin/python 

from LaunchServices import *
master_string=LSCopyDefaultHandlerForURLScheme("mailto")
print("<result>" + master_string + "</result>")

 

el2493
Contributor III

Just wanted to update in case someone came across this: I did get a working version of the EA in Python3, though it requires macadmins Python3 to be installed (it contains modules that the script needs to run, and other distributions of Python3 may or may not include them). I also wrapped it in zsh so that if macadmins Python3 isn't installed, the EA can report on that.

#!/bin/zsh

if [[ -f /Library/ManagedFrameworks/Python/Python3.framework/Versions/Current/bin/python3 ]]; then
	/Library/ManagedFrameworks/Python/Python3.framework/Versions/Current/bin/python3 << END
 
try:
	import CoreServices
	from CoreServices import *
	master_string=LSCopyDefaultHandlerForURLScheme("mailto")
	print ("<result>" + master_string + "</result>")
except ModuleNotFoundError:
	print("<result>CoreServices Not Installed</result>")
        
END

else
	echo "<result>Managed Python3 not installed</result>"
    exit 0
fi

It still may not be 100% reliable (the setting may be user-based so I'm not 100% sure if the value it's reading is accurate for the logged-in user) but it gets me the same results as I was getting with my old python2 EA.