Find and save value from a Plist

yarin
New Contributor II

Sorry for bugging you guys lately but I'm stuck, cause Apple just doesn't like IT and enterprises.

I need to create a script that will delete our Safari extension product.
The problem is that sometimes the File name is changing and I cant just delete all .extz files from the Extension Folder.
So I thought maybe to look inside the Extension.Plist and see in the "installed extension key my extension sub key with the installed file name (see screenshot) Then Delete the file With the exact name in the extension folder. I Couldn't figure out how to do it Can Someone help ?
If you guys a better idea how to remove the extension that will be great!
c1ff0fd3a01542f9a98c281a10b12425

2 REPLIES 2

mm2270
Legendary Contributor III

@yarin You can use either the defaults command or the legacy PlistBuddy application to read the data out of the plist.
I think in this case I might use a combination of both, since defaults is good at getting a list of the bundle identifiers, but doesn't do as well with grabbing the specific Archive Name string. PlistBuddy can do that though.

I added comments throughout the below so you can understand what's happening in it.

#!/bin/bash

## Get logged in user
loggedInUser=$(stat -f%Su /dev/console)

## Build path to the user's Extensions.plist
ExtensionsPlist="/Users/$loggedInUser/Library/Safari/Extensions/Extensions.plist"

## The vendor bundle id for the product
bundleID="com.WalkMe.SafariExtension"

## Get a list of all installed Bundled Identifiers (created as an array)
BundleIdentifiers=($(defaults read "$ExtensionsPlist" | awk -F'"' '/Bundle Identifier/{print $4}'))

## Loop over bundle IDs until we find the one we're looking for, capture the index number
x=0
while read BI; do
    if [ "$BI" == "$bundleID" ]; then
        index=$x
    fi
    let x=$((x+1))
done < <(printf '%s
' "${BundleIdentifiers[@]}")

## If we got an index value from the loop...
if [ ! -z "$index" ]; then
    ## Then get the bundle archive name using PlistBuddy (uses the above captured index number to get the correct dictionary index value)
    ArchiveName=$(/usr/libexec/PlistBuddy -c "Print :'Installed Extensions':$index:'Archive File Name'" "$ExtensionsPlist")

    ## Delete the .safariExtz file using the Archive Name found
    rm "/Users/$loggedInUser/Library/Safari/Extensions/$ArchiveName"
else
    ## If the index was blank, there is nothing installed to delete
    echo "No installed product found. Nothing to delete"
fi

One thing to watch for with this, I don't know if deleting Safari Extensions in this way is approved by Apple, and it's possible Safari may not recognize the change properly until it's restarted. Just something to test as you work on this.

yarin
New Contributor II

Yeah it doesn't recognize the changes and the we must reload safari (by script or by user )
Personally I don't think that's that we should do But customers insist We'll give them a mass deployment solution for Mac and that's what we work hard to give them
(With a warning that it may not be optimal with the Mac OS)
I'll test the script you wrote and see if it works!
thanks!