今回もVBScriptネタです。
今回は「プログラムと機能」から、インストールされているアプリケーションの一覧を取得するスクリプトを書いてみました。
1 2 3 4 5 6 7 8 9 | Option Explicit Dim itm With CreateObject( "Shell.Application" ). Namespace ( "shell:::{7b81be6a-ce2b-4676-a29e-eb907a5126c5}" ) For Each itm In .Items WScript.Echo itm.Name Next End With |
仕組みは「AppUserModelId(AUMID)を列挙するVBScript」と同じで、「プログラムと機能」をフォルダとして扱い、中にあるアイテム(FolderItem2)の名前を列挙しているだけです。
このスクリプトを利用すると、指定したアプリケーションがインストールされているかどうかを判別するスクリプトも簡単に書くことができます。
(名前だけで判断しているので、正確性に欠ける場合があるかもしれませんが)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Option Explicit MsgBox IsInstalledApplication( "ABCDEFG" ) 'インストールされていない場合は空の文字列が返る MsgBox IsInstalledApplication( "7-Zip" ) 'インストールされている場合はプログラム名が返る Private Function IsInstalledApplication( ByVal AppName) '指定したアプリケーションがインストールされているか判別 Dim ret Dim itm ret = "" '初期化 With CreateObject( "Shell.Application" ). Namespace ( "shell:::{7b81be6a-ce2b-4676-a29e-eb907a5126c5}" ) For Each itm In .Items If InStr(LCase(itm.Name), LCase(AppName)) Then ret = itm.Name Exit For End If Next End With IsInstalledApplication = ret End Function |
レジストリにあるアンインストール情報から取得することもできます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | Option Explicit MsgBox IsInstalledApplication( "ABCDEFG" ) 'インストールされていない場合は空の文字列が返る MsgBox IsInstalledApplication( "7-Zip" ) 'インストールされている場合はプログラム名が返る Private Function IsInstalledApplication( ByVal AppName) '指定したアプリケーションがインストールされているか判別 Dim reg Dim keys Dim key Dim ret Dim res Dim display_name Const HKEY_CURRENT_USER = &H80000001 Const HKEY_LOCAL_MACHINE = &H80000002 Const SubKeyName = "Software\Microsoft\Windows\CurrentVersion\Uninstall\" Const SubKeyNameX86 = "Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" res = "" '初期化 Set reg = CreateObject( "WbemScripting.SWbemLocator" ) _ .ConnectServer(, "root\default" ). Get ( "StdRegProv" ) On Error Resume Next reg.EnumKey HKEY_LOCAL_MACHINE, SubKeyName, keys For Each key In keys display_name = "" ret = reg.GetStringValue(HKEY_LOCAL_MACHINE, SubKeyName & key, "DisplayName" , display_name) If ret <> 0 Then ret = reg.GetStringValue(HKEY_LOCAL_MACHINE, SubKeyName & key, "QuietDisplayName" , display_name) If (ret = 0) And (Len(Trim(display_name)) > 0) Then If InStr(LCase(display_name), LCase(AppName)) Then res = display_name Exit For End If End If Next On Error GoTo 0 '64ビットアプリケーションに無い場合は32ビットアプリケーションを検索 If (Isx64() = True ) And (Len(Trim(res)) < 1) Then On Error Resume Next reg.EnumKey HKEY_LOCAL_MACHINE, SubKeyNameX86, keys For Each key In keys display_name = "" ret = reg.GetStringValue(HKEY_LOCAL_MACHINE, SubKeyNameX86 & key, "DisplayName" , display_name) If ret <> 0 Then ret = reg.GetStringValue(HKEY_LOCAL_MACHINE, SubKeyNameX86 & key, "QuietDisplayName" , display_name) If (ret = 0) And (Len(Trim(display_name)) > 0) Then If InStr(LCase(display_name), LCase(AppName)) Then res = display_name Exit For End If End If Next On Error GoTo 0 End If 'HKLMに無い場合はHKCUを検索 If Len(Trim(res)) < 1 Then On Error Resume Next reg.EnumKey HKEY_CURRENT_USER, SubKeyName, keys For Each key In keys display_name = "" ret = reg.GetStringValue(HKEY_CURRENT_USER, SubKeyName & key, "DisplayName" , display_name) If ret <> 0 Then ret = reg.GetStringValue(HKEY_CURRENT_USER, SubKeyName & key, "QuietDisplayName" , display_name) If (ret = 0) And (Len(Trim(display_name)) > 0) Then If InStr(LCase(display_name), LCase(AppName)) Then res = display_name Exit For End If End If Next On Error GoTo 0 End If IsInstalledApplication = res End Function Private Function Isx64() '64ビット環境かどうかを判別 Dim colItems Dim itm Dim ret ret = False '初期化 Set colItems = CreateObject( "WbemScripting.SWbemLocator" ).ConnectServer.ExecQuery( "Select * From Win32_OperatingSystem" ) For Each itm In colItems If InStr(itm.OSArchitecture, "64" ) Then ret = True Exit For End If Next Isx64 = ret End Function |
ただし、こちらの方法で取得できるのはUninstallキー以下に情報があるアプリケーションのみになります。
この記事へのコメントはありません。