import sys
import os

def get_installed_apps():
    apps_dir = "/Applications"
    apps = []
    for item in os.listdir(apps_dir):
        if item.endswith(".app"):
            app_path = os.path.join(apps_dir, item)
            version = "Unknown"
            plist_path = os.path.join(app_path, "Contents", "Info.plist")
            try:
                # plistlib in Python 3 or 2.7+ can open plist
                try:
                    import plistlib
                except ImportError:
                    plistlib = None
                
                if plistlib:
                    if sys.version_info[0] == 3:
                        with open(plist_path, "rb") as f:
                            plist = plistlib.load(f)
                    else:
                        # Python 2 plistlib needs different open mode
                        with open(plist_path, "r") as f:
                            plist = plistlib.readPlist(f)
                    version = plist.get("CFBundleShortVersionString", "Unknown")
                else:
                    # fallback: use /usr/libexec/PlistBuddy via subprocess
                    import subprocess
                    output = subprocess.check_output([
                        "/usr/libexec/PlistBuddy",
                        "-c", "Print :CFBundleShortVersionString",
                        plist_path
                    ])
                    if sys.version_info[0] == 3:
                        output = output.decode()
                    version = output.strip()
            except Exception:
                pass
            apps.append((item[:-4], version))
    return apps

if __name__ == "__main__":
    print("Installed Applications and Versions:")
    for app, ver in get_installed_apps():
        print("{} - Version: {}".format(app, ver))

    print("\nPython interpreter version info:")
    print(sys.version)
