Script assistance with logged in user folder

macmanmk
Contributor

I am trying to set up an extension attribute that will show whether an application has been run on individual systems. When launched, the app creates a directory at the user level. I'm trying to find a way to output whether that directory is present, but I don't think it's working. It shows that the app is not in use on every machine. Can anyone tell me what is wrong with the script I set up below?

!/bin/bash
if [ -d "/Users/${loggedInUser}/Library/ndOneClick/Working Documents" ]; then
echo "<result>ndOneClick in use</result>"
else
echo "<result>Not in use</result>"
fi
exit

4 REPLIES 4

mm2270
Legendary Contributor III

Hi @macmanmk The main problem would be that you haven't defined $loggedInUser anywhere in your script, so it doesn't know what path you mean when you use it in "/Users/${loggedInUser}/Library/ndOneClick/Working Documents"

Other than that, I would also look at using mdls, which is part of the Spotlight command line tools. If all you need to know is if the app was run, not how long it was used or anything like that, you can get that by searching the app's spotlight information for it's kMDItemLastUsedDate or even the kMDItemUseCount, like so (I'm making an assumption on the app name, so correct this if the below is wrong)

#!/bin/bash

if [ -d "/Applications/ndOneClick.app" ]; then
    if [[ $(mdls -name kMDItemUseCount "/Applications/ndOneClick.app" | awk '{print $NF}') == "(null)" ]]; then
        echo "<result>Not in use</result>"
    else
        echo "<result>ndOneClick in use</result>"
    fi
else
    echo "<result>Not installed</result>"
fi

The only real problem is if the Spotlight index gets deleted and recreated I'm not sure if those values are retained. I think it ends up starting from scratch, but don't quote me on that. Meaning, even if it was once used, if the Spotlight index is rebuilt it could start saying Not in use.

If you really want to go with the detection of the folder, you can try using find

find /Users -maxdepth 5 -path /Users/*/Library/ndOneClick/Working Documents

therealmacjeezy
New Contributor III

Try using the script below with the changes I made.

The original script was missing the variable for the current logged in user, which was why it was reporting that no one was using it. Now that the variable is set it can be used in the statement you wrote.

Also, I'd recommend using double quotes when calling on variables. This way, if your variable contains a space it won't need to be escaped in order to work. It will treat the variable as one string instead of stopping at the first space.

#!bin/bash
# Pulls the current logged in user
currentUser=$(who | grep "console" | cut -d" " -f1)

if [[ -d "/Users/"$currentUser"/Library/ndOneClick/Working Documents" ]]; 
then
     echo "<result>ndOneClick in use</result>"
else
     echo "<result>Not in use</result>"
fi
"Saying 'uhh..' is the human equivalent to buffering."

macmanmk
Contributor

Thanks very much for these responses. Obviously, I still have some brushing up to do with my syntax. While I am intrigued with leveraging Spotlight to do this, I'm afraid it might break from time to time. We have to do regular rebuilds of the Spotlight index due to searching issues in Mail.

I do see some strange behavior, through. Only about 15 machines (out of 600) are displaying the EA output in inventory even though it has been posted for a few hours. I ran recon on a handful of machines and yet those machines are not displaying the EA output.. If I SSH to a few of those machines and run the script manually, it returns output, but still nothing in inventory. Is there anything that would be delaying the display of the information in inventory?

tlarkin
Honored Contributor

I saw this thread and was a bit bored so I figured I would write a Python version of this for grins.

#!/usr/bin/python

import sys
import os
from SystemConfiguration import SCDynamicStoreCopyConsoleUser


username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]
username = [username,""][username in [u"loginwindow", None, u""]]

file_path = str('/Users/' + '%s' + '/Library/ndOneClick/Working Documents') % username

if os.path.isdir(file_path):
    print '<result>ndOneClick in use</result>'
else:
    print '<result>ndOneClick not in use</result>'


sys.exit(0)

output:

$ python check_app.py 
<result>ndOneClick not in use</result>
$ mkdir -p ~/Library/ndOneClick/Working Documents
$ python check_app.py 
<result>ndOneClick in use</result>