CreateShortcut メソッド

ショートカットまたは URL ショートカットへのオブジェクト参照を作成します。

object.CreateShortcut(strPathname) 

引数

  • object
    WshShell オブジェクトです。
  • strPathname
    作成するショートカットのパスとファイル名です。

解説

CreateShortcut メソッドは、WshShortcut オブジェクトまたは WshURLShortcut オブジェクトのいずれかを返します。CreateShortcut メソッドを呼び出しただけではショートカットは作成されません。ショートカット オブジェクトおよびオブジェクトに対して行った変更はメモリ内に格納されているため、Save メソッドを使ってディスクに保存する必要があります。ショートカットを作成するには、次の手順を実行します。

  1. WshShortcut オブジェクトのインスタンスを作成します。

  2. インスタンスのプロパティを初期化します。

  3. Save メソッドを使ってインスタンスをディスクに保存します。

    メモ   ショートカット オブジェクトの TargetPath プロパティに引数を指定することがよく行われますが、これでは正常に動作しません。ショートカットへの引数はすべて Arguments プロパティに指定する必要があります。

使用例

次の例では、WshShell オブジェクトを作成し、その CreateShortcut メソッドを使って 2 つのショートカットを作成しています。

<package>
   <job id="vbs">
      <script language="VBScript">
         set WshShell = WScript.CreateObject("WScript.Shell")
         strDesktop = WshShell.SpecialFolders("Desktop")
         set oShellLink = WshShell.CreateShortcut(strDesktop & "\ショートカット スクリプト..lnk")
         oShellLink.TargetPath = WScript.ScriptFullName
         oShellLink.WindowStyle = 1
         oShellLink.Hotkey = "CTRL+SHIFT+F"
         oShellLink.IconLocation = "notepad.exe, 0"
         oShellLink.Description = "ショートカット スクリプト."
         oShellLink.WorkingDirectory = strDesktop
         oShellLink.Save
         set oUrlLink = WshShell.CreateShortcut(strDesktop & "\マイクロソフトの Web サイト.url")
         oUrlLink.TargetPath = "https://www.microsoft.com"
         oUrlLink.Save
      </script>
   </job>

   <job id="js">
      <script language="JScript">
         var WshShell = WScript.CreateObject("WScript.Shell");
         strDesktop = WshShell.SpecialFolders("Desktop");
         var oShellLink = WshShell.CreateShortcut(strDesktop + "\\ショートカット スクリプト.lnk");
         oShellLink.TargetPath = WScript.ScriptFullName;
         oShellLink.WindowStyle = 1;
         oShellLink.Hotkey = "CTRL+SHIFT+F";
         oShellLink.IconLocation = "notepad.exe, 0";
         oShellLink.Description = "ショートカット スクリプト";
         oShellLink.WorkingDirectory = strDesktop;
         oShellLink.Save();
         var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\マイクロソフト Web サイト.url");
         oUrlLink.TargetPath = "https://www.microsoft.com";
         oUrlLink.Save();
      </script>
   </job>
</package>

参照

スクリプトを実行する | WshShortcut オブジェクト | WshUrlShortcut オブジェクト | WshShell オブジェクト