@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.
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!