发表回复 
appStarter() - 软件快速启动器
2017-04-17, 18 : 55 (这个帖子最后修改于: 2017-04-18 02 : 11 by bootislands1.)
appStarter() - 软件快速启动器
缘起
Windows的启动器,如ALTRun、Claunch、Rolan、FARR、WOX、Listary等,不能保证两键以内,启动常用程序。而且启动时,顺便做点附带操作,也不便定制。所以我转用AutoHotkey。

但用AutoHotkey,给常用程序绑定快捷键,又会产生记忆负担。一些没到绝对常用地步、但偶尔常用的快捷键,又经常记不住。
如果都用易记的首字母,快捷键也不够用;如ProcessMonitor、Proxifier和PotPlayer的首字母都是P。

这个函数,旨在解决这问题。

作用
给每个软件,绑定一个容易记忆的快捷键(例如首字母)。按下后:
  • 如果该快捷键,只绑定1个软件:直接启动。
  • 如果绑定的软件2个以上:弹出小菜单,供选择

调用格式
引用:appStarter(hotkeys, calledString, itemName)
参数:
  • hotkeys :快捷键
  • calledString :启动的程序,字符串格式,支持软件 路径函数标签。例如
    代码: (全选)
    ;普通路径
    appStarter("#c", "cmd", "cmd")
    appStarter("#f", "d:\ProgramFiles\firefox\firefox.exe")
    appStarter("#h", "shell:::{645FF040-5081-101B-9F08-00AA002F954E}", "回收站")

    ;标签
    appStarter("$RButton up", "录制gif")
    录制gif:
        ;your code
        return

    ;函数
    appStarter("Numpad0 & b", "batchPdg")
    batchPdg() {
         ;your code
    }
  • itemName :菜单中显示的名称。可选 参数
Todo
  1. 解放鼠标:菜单弹出后,按下1 2 3…快捷启动第一、二、三项…或者,像Ditto一样,再多按一下,就往下移动一项
  2. calledString是函数时,支持参数传递
地址
GitHub
代码: (全选)
class MenuClass {
        static wholeMenuList := {}        ;里面存储的是对象,不是字符串
        indexOfWholeMenuList :=    
        
        __New(hotkeyOfMenu) {        
            MenuClass.wholeMenuList[hotkeyOfMenu] := this
            this.indexOfWholeMenuList := hotkeyOfMenu
        }
        
        checkMenuExist(hotkeyAndNameOfMenu) {
            if ( MenuClass.wholeMenuList[hotkeyAndNameOfMenu] != "" )
                return MenuClass.wholeMenuList[hotkeyAndNameOfMenu]
            else
            {
                newMenu := new MenuClass(hotkeyAndNameOfMenu)
                return newMenu
            }
        }
        
        afterPressHotkeyDoWhat() {
            return new this.popOutMenuORRunSoloDirectly(this)
        }
        
        Class ItemClass {
            whatItemNameShows :=
            calledBeforeConvert :=    
            calledFuncORLabelString :=    
            parent :=    
            
            __New(myParent) {
                this.parent := myParent
                return this
            }
            
            convertCalled() {
                if ( IsFunc(this.calledBeforeConvert) )
                {
                    this.calledFuncORLabelString := Func(this.calledBeforeConvert)
                }
                else if ( IsLabel(this.calledBeforeConvert) )
                {
                    this.calledFuncORLabelString := this.calledBeforeConvert
                }
                else
                {
                    this.calledFuncORLabelString := new this.FuncClassConvertFromString(this)  
                }
                return this.calledFuncORLabelString
            }
            
            generateItemNameIfNotGiven() {
                if ( this.whatItemNameShows != "" )
                    return this.whatItemNameShows
                else
                {
                    filename := RegExReplace(this.calledBeforeConvert, ".*?\\?([^\\]+)$", "$1")
                    return filename
                }
            }
            
            class FuncClassConvertFromString {
                __New(myParent) {
                    this.parent := myParent
                    return this
                }
                Call() {
                    Run, % this.parent.calledBeforeConvert
                }
                __Call(method, args*) {
                    if (method = "")
                        return this.Call(args*)
                    if (IsObject(method))
                        return this.Call(method, args*)
                }
    
            }
        }
    
        Class popOutMenuORRunSoloDirectly {
            __New(myParent) {
                this.parent := myParent
            }
            Call() {
                if ( this.parent.Length() = 1 )
                {    if ( IsLabel(this.parent[1].calledBeforeConvert) )
                    {
                        destination := this.parent[1].calledFuncORLabelString
                        gosub %destination%
                    }
                    else
                        this.parent[1].calledFuncORLabelString.call()
                }
                else        ;menu里有多个item
                {
                    menuName := this.parent.indexOfWholeMenuList
                    CoordMode, Menu, Screen
                    this.setMenuPosition()
                    x := this.x
                    y := this.y
                    Menu, %menuName%, Show, %x%, %y%
                }
            }
            __Call(method, args*) {
                if (method = "")  
                    return this.Call(args*)
                if (IsObject(method))      
                    return this.Call(method, args*)
            }
            setMenuPosition() {                                    ;设定menu显示的位置,存储在this.x  this.y中
                this.x := (A_ScreenWidth / 2)
                this.y := (A_ScreenHeight / 2)
            }
            
        }
    }
    
    ;calledString只支持接受字符串,可以是单程序路径、函数名、标签名
    appStarter(hotkeys, calledString, itemName := "") {
        menu := MenuClass.checkMenuExist(hotkeys)            ;检查menu实例是否存在。若无,则创建,如有,则读取存储地址。返回menu instance
        
        itemInstance := new menu.ItemClass(menu)
        itemInstance.whatItemNameShows := itemName
        itemInstance.calledBeforeConvert := calledString
        calledOBJorSTR := itemInstance.convertCalled()
        itemName := itemInstance.generateItemNameIfNotGiven()
        menu.Push(itemInstance)
        
        Menu, %hotkeys%, Add, %itemName%, %calledOBJorSTR%
        
        popOutMenuORRunSoloDirectly := menu.afterPressHotkeyDoWhat()        ;创建热键,调用afterPressHotkeyDoWhat()函数
        Hotkey, %hotkeys%, %popOutMenuORRunSoloDirectly%, On
    }
查找这个用户的全部帖子
表示感谢 引用并回复 移动视图置页面顶端
[+] 2用户表示感谢bootislands1
发表回复 


论坛跳转:


联系我们 | Autohotkey 中文站 | 回到顶部 | 回到正文区 | 精简(归档)模式 | RSS 聚合