Deleting all login items with a particular property...

AARP
New Contributor III

I want to delete a login item via an osascript bash script. I know how to do that if I know the name of the login item. But I would like to be able to say "delete all login items that are mounted volumes" and I haven't been able to find the syntax to accomplish that. My goal is to remove any shares that are mounted via login item, as I intend to replace them with a script to handle automounts. (I would prefer to manage automounts that way instead of using configuration profiles.)
So far, the command I've tried is:
osascript -e 'tell application "System Events" to delete login items with properties { kind:volume }'
The error I get is: 71:72: syntax error: Expected “given”, “with”, “without”, other parameter name, etc. but found “{”. (-2741)
Has anyone else encountered this? And is what I am trying to do even possible?

2 REPLIES 2

mm2270
Legendary Contributor III

Something like this may work, but its a bash script, not a pure AppleScript. I'm not certain if there's a simple way directly in AS.

#!/bin/bash

loginItemsList=$(echo $(/usr/bin/osascript -e 'tell application "System Events" to get the name of every login item') | awk -F'"' '{print}' | tr ',' '
' | sed -e 's/^ //g;s/ $//g')

while read loginitem; do
    if [[ $(echo "$loginitem" | grep "Volumes") ]]; then
        echo "Mount volume login item ${loginitem} located"
        ## Do the delete stuff here with the ${loginitem}
    fi
done < <(echo "$loginItemsList")

Basically, generate a neat list of login items separated by new lines, then pipe that back into a while read loop that checks each one looking for the property that would define it as a volume mount login item and then do something to delete it when found. The commented out line above that states "Do the delete stuff here with the ${loginitem}" would be where you put in the command to remove it.
I have not tested this for deleting, but a quick test in changing the grep to something like "Helper" returned a few Helper type login items I have under my account, so, its reading them back correctly at least.

Hope that helps.

davidacland
Honored Contributor II
Honored Contributor II

This command will do it:

osascript -e 'tell application "System Events" to delete every login item whose kind is "Volume"'