<strong><span>二、运行程序或打开文件</span></strong><strong><span> <p></p></span></strong><span>1</span><span>、运行程序</span><span> <p></p></span><span>Run t</span><span>命令或者函数用来运行外部可执行文件,</span><span>AHK</span><span>还可利用它来直接打开文件。</span><span> <p></p></span><span>AHK</span><span>:</span><span> <p></p></span><span>Run, </span><span>目标文件</span><span> [, </span><span>工作目录</span><span>, Max|Min|Hide|UseErrorLevel, </span><span>输出</span><span>PID</span><span>变量</span><span>] <p></p></span><span>AU3</span><span>:</span><span> <p></p></span><span>Run ( "</span><span>文件名</span><span>" [, "</span><span>工作目录</span><span>" [, </span><span>标志</span><span>]] ) <p></p></span><span>【示例</span> <span>2.1.1</span> <span>】</span><span> <p></p></span><span>AHK</span><span>:</span><span> <p></p></span><span>Run, Notepad.exe <p></p></span><span>AU3</span><span>:</span><span> <p></p></span><span>Run("Notepad.exe") <p></p></span><span>上面的示例中都没有给出程序“</span><span>Notepad.exe</span><span>”的路径,为什么仍能执行?这是因为它们都会自动在脚本所在目录下搜寻目标文件,如有则运行,否则就到系统文件夹(</span><span>%PATH%</span><span>)中搜寻。</span><span> <p></p></span><span>注意:</span><span> <p></p></span><span>A</span><span>)某些程序必须给定“工作目录”才能成功运行!</span><span> <p></p></span><span>B</span><span>)给出完整的文件路径有助于轻微提高程序的可靠性。</span><span> <p></p></span><span>C</span><span>)</span><span>AHK</span><span>的</span><span>Run</span><span>命令可以用来运行程序和直接打开文件,而</span><span>AU3</span><span>的</span><span>Run</span><span>函数则只能用来运行程序(可执行文件)或传递参数让某个程序打开目标文件。</span><span> <p></p></span><span>当然,运行程序的功能还不仅仅是这么简单,我们还可以指定运行程序的初始状态,比如让运行的记事本窗口以最大化状态显示(或者最小化、隐藏):</span><span> <p></p></span><span>【示例</span> <span>2.1.2</span> <span>】</span><span> <p></p></span><span>AHK</span><span>:</span><span> <p></p></span><span>Run, Notepad.exe, , Max <p></p></span><span>AU3</span><span>:</span><span> <p></p></span><span>Run("Notepad.exe", "", @SW_MAXIMIZE) <p></p></span><span><br /> </span><span>2</span><span>、打开文件</span><span> <p></p></span><span>前面已经提到,</span><span>AHK</span><span>的</span><span>Run</span><span>命令可以直接打开文件,而</span><span>AU3</span><span>的</span><span>Run</span><span>函数则只能用来运行程序,因此在打开文件的方式上有点不同:</span><span>AHK</span><span>脚本中可直接给出目标文件,而</span><span>AHK</span><span>将自动运行该文件的关联程序来打开它;而</span><span>AU3</span><span>则必须由用户自己传递参数让某个程序打开目标文件。</span><span><br /></span><span>【示例</span> <span>2.2.1</span> <span>】</span><span> <p></p></span><span>AHK</span><span>:</span><span> <p></p></span><span>Run, MyFile.txt <p></p></span><span>Run, Notepad.exe MyFile.txt <p></p></span><span>AU3</span><span>:</span><span> <p></p></span><span>Run("Notepad.exe MyFile.txt") <p></p></span><span><br /> </span><span>3</span><span>、以命令行形式运行程序</span><span> <p></p></span><span>可以考虑运行系统的命令行解释器(</span><span>cmd.exe/command.com</span><span>),然后指定要执行的命令并传递参数。</span><span> <p></p></span><span>假设我们要执行命令“</span><span>dir C:\WINDOWS\system 32<span><span>”</span></span> </span><span>,用以列出指定目录的所有文件及子目录。</span><span> <p></p></span><span>【示例</span> <span>2.3.1</span> <span>】</span><span> <p></p></span><span>AHK</span><span>:</span><span> <p></p></span><span>Run, %ComSpec% /k dir C:\WINDOWS\system32 <p></p></span><span>AU3</span><span>:</span><span> <p></p></span><span>Run(@ComSpec & " /k dir C:\WINDOWS\system32") <p></p></span><span>注意:</span><span> <p></p></span><span>A</span><span>)</span><span>ComSpec</span><span>是脚本内建的用以指示命令行解释器位置的变量或宏。</span><span> <p></p></span><span>B</span><span>)</span><span>/k </span><span>参数表示“执行字符串指定的命令但保留”,若改为</span><span> /c </span><span>则表示“执行字符串指定的命令然后终断”。对此比较直观的解释是</span><span> /k </span><span>将在执行完命令后保留命令提示窗口,而</span><span> /c </span><span>则将在执行完命令之后关闭命令提示窗口。</span><span> <p></p></span><span>C</span><span>)符号“</span><span>&</span><span>”是</span><span>AU3</span><span>定义的字符串连接符。</span><span> <p></p></span><span><br /> </span><span>4</span><span>、特殊应用</span><span> <p></p></span><span>A</span><span>)打开网页</span><span> <p></p></span><span>【示例</span> <span>2.4.1</span> <span>】</span><span> <p></p></span><span><span> </span>AHK</span><span>:</span><span> <p></p></span><span>Run, <a href="http://www.autohotkey.com/" target="_blank"><span>www.autohotkey.com</span></a> <p></p></span><span>Run, %</span><span>A_ProgramFiles</span><span>%\Internet Explorer\IEXPLORE.EXE <a href="http://www.autohotkey.com/" target="_blank"><span>www.autohotkey.com</span></a> <p></p></span><span>AU3</span><span>:</span><span> <p></p></span><span>Run(@ProgramFilesDir & "\Internet Explorer\IEXPLORE.EXE <a href="http://www.autohotkey.com/" target="_blank"><span>www.autohotkey.com</span></a>") <p></p></span><span>B</span><span>)打开特殊文件夹</span><span> <p></p></span><span>系统的某些特殊文件夹被定义了相应的</span><strong><span>CLSID</span></strong><strong><span>(请查看帮助文档)</span></strong><span>,我们可利用它来打开相应的文件夹,比如打开回收站:</span><span> <p></p></span><span>【示例</span> <span>2.4.2</span> <span>】</span><span> <p></p></span><span><span> </span>AHK</span><span>:</span><span> <p></p></span><span>Run ::{645ff040-5081-101b -9f 08-00aa 002f 954e} <p></p></span><span>AU3</span><span>:</span><span> <p></p></span><span>不适用!</span><span> <p></p></span><span>C</span><span>)运行控制面板工具</span><span> <p></p></span><span>微软已经为我们提供了通过命令行打开控制面板某个工具或项目的方式,比如打开系统属性窗口:</span><span> <p></p></span><span>【示例</span> <span>2.4.3</span> <span>】</span><span> <p></p></span><span><span> </span>AHK</span><span>:</span><span> <p></p></span><span>Run control sysdm.cpl <p></p></span><span>AU3</span><span>:</span><span> <p></p></span><span>Run("control sysdm.cpl") <p></p></span><p><span>关于访问控制面板项目的详细介绍请查看此文:</span><span><a href="http://www.blogcn.com/user48/yonken/blog/38798174.html" target="_blank"><span><span>文章地址</span></span></a></span><span>。</span></p><span>D</span><span>)指定搜索位置并打开搜索窗口</span><span> <p></p></span><span>假设我们要打开一个搜索窗口,而且要指定搜索位置,比如</span><span>C:\</span><span>:</span><span> <p></p></span><span>【示例</span> <span>2.4.4</span> <span>】</span><span> <p></p></span><span><span> </span>AHK</span><span>:</span><span> <p></p></span><span>Run, find C:\ <p></p></span><span>AU3</span><span>:</span><span> <p></p></span><span>不适用!</span><span> <p></p></span><span>E</span><span>)显示指定文件的属性窗口</span><span> <p></p></span><span>假设我们要打开文件“</span><span>MyFile.txt</span><span>”的属性窗口,则使用关键字</span><span>properties </span><span>然后接上目标文件即可:</span><span> <p></p></span><span>【示例</span> <span>2.4.5</span> <span>】</span><span> <p></p></span><span><span> </span>AHK</span><span>:</span><span> <p></p></span><span>Run, properties MyFile.txt <p></p></span><span>AU3</span><span>:</span><span> <p></p></span><span>不适用!</span><span> <p></p></span><span>注意:</span><span>AHK</span><span>在退出前将自动关闭打开的属性窗口!</span><span> <p></p></span><span>F</span><span>)用“资源管理器”打开指定文件夹</span><span> <p></p></span><span>我们知道使用</span><span>Run, explorer C: </span><span>或</span><span>Run("explorer C:") </span><span>即可打开指定的文件夹,可是有时候我们需要在资源管理器中打开它,这时可使用关键字</span><span> <span>explore</span></span><span>:</span><span> <p></p></span><span>【示例</span> <span>2.4.6</span> <span>】</span><span> <p></p></span><span><span> </span>AHK</span><span>:</span><span> <p></p></span><p><span>Run, explore C</span><span>:</span></p><span>AU3</span><span>:</span><span> <p></p></span><span>run("explorer.exe /e,C:\")</span><span> <p></p></span><span>G</span><span>)打印指定文件</span><span> <p></p></span><span>要打印指定文件,可使用关键字</span><span> <span>print</span></span><span>:</span><span> <p></p></span><span>【示例</span> <span>2.4.7</span> <span>】</span><span> <p></p></span><span><span> </span>AHK</span><span>:</span><span> <p></p></span><p><span>Run, print MyFile.txt</span></p><span>AU3</span><span>:</span><span> <p></p></span><div><span>不适用!</span> |