Extract value from a Plist

yarin
New Contributor II

Last week I had help from @mm2270 to extract our Extension Archive name with given Bundle ID
[https://www.jamf.com/jamf-nation/discussions/24701/find-and-save-value-from-a-plist
](link URL)
I tried to use that script to search for other velues but it was too complicated for me.
There are two values I want to extract from the the Extension.Plist:

  • Version = So we could tell which Version the Client use.
  • Declined by User = which will tell us if the extension was installed from gallery or in "Developer Mode"

With an attribute extension the IT team of that enterprise can Deploy the software again!

I'm attaching a screenshot 2f0192eb8753449d85d7fc62e91eb79e Below there's the code we used to find the extension name and delete 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~[upload](0954e65c23fa494a939de5f6d473cc50)
6 REPLIES 6

mm2270
Legendary Contributor III

@yarin I'm not sure if you're looking at the information you really want to get. The Version Number string shown in your image is under the Apple-hosted Updates List array, which is not what's actually installed. I believe that shows what updates Apple's Safari Extension process is aware of, but the actual installed Extensions show up under the Installed Extensions array a few lines up in that image.

As far as I can see when taking a quick look, the installed versions don't show up in that Extensions.plist anywhere. In fact, I have no idea where the installed extension versions are stored. They must be within the .safariextz bundles themselves, but they appear as flat files, so there's no way to drill into them that I can see to get that info.

In short, I don't know if there's an effective way to get that information.

yarin
New Contributor II

Thanks @mm2270 .
The Installed Extension is there. you can see all the details in here:
f86c60905796474b97c74bd8fc34d797

If we cant see the version can we use the Declined by User value ?

mm2270
Legendary Contributor III

@yarin Yes, I know the Installed Extensions array is there and has information on them. It just doesn't seem to show the installed version, which is odd.
I also do not see a "Declined by User" value anywhere in that list, at least not in any of my installed Safari extensions, but maybe that's something unique to that Extension you're working with. I don't know.

IF that value shows up in the Installed Extensions array, then you can test by changing the line in the original script that looks like this

ArchiveName=$(/usr/libexec/PlistBuddy -c "Print :'Installed Extensions':$index:'Archive File Name'" "$ExtensionsPlist")

to

DeniedByUser=$(/usr/libexec/PlistBuddy -c "Print :'Installed Extensions':$index:'Declined By User'" "$ExtensionsPlist")

then remove this line

rm "/Users/$loggedInUser/Library/Safari/Extensions/$ArchiveName"

and replace it with

echo "<result>$DeniedByUser</result>"

I don't have anything installed that has that Declined By User boolean value in it, even under the Apple updates section, so I can't test this out. I have nothing to reference it against.

yarin
New Contributor II

@mm2270 Are you using a mac with Yosmite and safari 9 or newer?
If not it will not show those details since safari gallery is supported starting those versions.
Also the declined by user is shown only when you use an Extension from the developer and when installing it will ask you if
to install from Gallery or developer.
If you choose Developer safari wont update your extension and we want the customers to know that.

yarin
New Contributor II

@mm2270 It's not Working.
I echoed every step to see where it fails
It seems that he cant find the Identifier we are looking for so there is no index to search
We have certain Values that wont change in this dictionary like the "Identifier" which is: com.walkme.SafariExtension.[that long ID]
I tried editing the script in all arguments to find the values but it didn't worked.

bhaessly
New Contributor

I'm trying to do something similar to this and I am getting hung up on the loop.

done < <(printf '%s
' "${BundleIdentifiers[@]}")

I get the following error: syntax error near unexpected token `<'

Does anyone have any insight on why this is happening and how to fix it?