获取打开的文档的路径

66 篇文章 2 订阅
38 篇文章 0 订阅
; File encoding:  UTF-8 
/*
AutoHotkey 版本: 1.1.9.0
操作系统:    Windows XP/Vista/7 
作者:        sunwind <1576157 qq="" com=""> 
博客:        http://blog.csdn.net/liuyukuan/article/details/7411291
脚本说明:此为脚本用于打开Office、Scite等软件中 打开的文档路径。
脚本版本:   2012-12-23版
Timestamp:2012年12月23日14:40:22
*/
 

;2012-12-23 更新Scite路径获取方式为com调用,即使标题栏没有显示全部路径也能工作。
;2012-03-31 改热键为win+del,因为我多数是为了清理没用文档,删除当前打开的文件。  
;2012-03-31 增加:对非office软件 则打开程序所在目录功能。
;2012-03-31 增加 :可打开SciTE 、EmEditor文档所在目录(前提是其标题栏显示文档路径)

;对于notepad等程序需要增加也个case标签单独处理,可以通过先设置各个程序支持在标题栏中显示路径,然后解析路径的方式.本想弄个通用的用WMI方式查询cmd_line内容,但是如果运行了记事本,再open,打开文本文件的话,文件路径是不在cmd_line中的,残念.  


#del:: ;打开office文档所在目录。非office软件打开程序所在目录。
  WinGet, _ProcessPath, ProcessPath, A
  _DocPath:=getDocumentPath(_ProcessPath)
  TrayTip,,%_DocPath%,10
  Run,% "Explorer.exe /select, " _DocPath 
Return


getDocumentPath(_ProcessPath)
  {
	SplitPath,_ProcessPath,Process_Name,Process_Dir,,Process_NameNoExt
	value:=Process_NameNoExt
	
    If IsLabel( "Case_" . value)
        Goto Case_%value%
    Else
        Goto Case_Default
Case_WINWORD:  ;Word OpusApp
  Application:= ComObjActive("Word.Application") ;word
  ActiveDocument:= Application.ActiveDocument ;ActiveDocument
Return  % ActiveDocument.FullName
Case_EXCEL:  ;Excel XLMAIN
  Application := ComObjActive("Excel.Application") ;excel
  ActiveWorkbook := Application.ActiveWorkbook ;ActiveWorkbook
Return % ActiveWorkbook.FullName
Case_POWERPNT:  ;Powerpoint PPTFrameClass
  Application:= ComObjActive("PowerPoint.Application") ;Powerpoint
  ActivePresentation := Application.ActivePresentation ;ActivePresentation
Return % ActivePresentation.FullName

Case_SciTE:       ;;打开SciTE当前文件所在目录
Return % GetCurrentFilePath(GetSciTEInstance())
Case_EmEditor:  ;;打开EmEditor当前文件所在目录 
	WinGetActiveTitle _Title
	SplitPath,_Title,name,dir
	Needle:=" - "
	StringSplit,_name,name,%Needle%
	FullName:=dir . "\" . _name1
Return % FullName
Case_AkelPad:
    WinGetActiveTitle _Title
	SplitPath,_Title,name,dir
    MsgBox %dir%
	Needle:=" - "
	StringSplit,_name,name,%Needle%
	FullName:=dir . "\" . _name1
Return % FullName

Case_Notepad:
 WinGet pid, PID, A
  wmi := ComObjGet("winmgmts:")
  queryEnum := wmi.ExecQuery(""
  . "Select * from Win32_Process where ProcessId=" . pid)
  ._NewEnum()
  If queryEnum[Process] {
       CMDLine := Process.CommandLine ; 获取进程命令行
       RegExMatch(CMDLine, "i)\s+(.*)", ff_)   ; 正则匹配命令行参数
        ;~ RegExMatch(process.CommandLine,"i).*exe.*?\s+(.*)",CMDLine_1)
       StringReplace,ff,ff_1,`",,All
       if ff<>
         Return % ff
       else
        Return % _ProcessPath
       wmi := queryEnum := Process := ""
    }
    
Case_Default:
Return % _ProcessPath ; 其他程序打开程序所在目录
}


GetCurrentFilePath(scite)
{
	if !scite
	{
		MsgBox, 16, Error, Can't find SciTE!
		ExitApp
	}
	return scite.CurrentFile
}
GetSciTEInstance()
{
	olderr := ComObjError()
	ComObjError(false)
	scite := ComObjActive("SciTE4AHK.Application")
	ComObjError(olderr)
	return IsObject(scite) ? scite : ""
}
;~ 增加打开当前程序的路径,热键设置为win+end。 

#end:: ;仅是打开当前激活窗口的程序所在目录。不常用
  WinGet, _ProcessPath, ProcessPath, A
  Run,% "Explorer.exe /select, " _ProcessPath 
Return


  
  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值