1、环境准备
- windows 7,8,10,11操作系统
- ahk 2.x_64位
2、使用方式
get 表达式可以获取配置文件getconfig.txt中配置的值,get可以计算“[ ]”中的表达式
也可以获取用户,系统的环境变量,或者是path
只获取 环境变量或是path也可以用表达式 getpath
在当前脚本下面配置文件 getconfig.txt
##############################################################################
#如果是1开头则后面表达式中的”[ ]”的值需要计算 ,[ ]中支持+-*/^等表达式,[]中数据会整体乘倍数
#大小写敏感
###ahk映射
ctrl=^
alt=!
win=#
shift=+
####时间换算
1天=[24][h] [24*60][min] [24*60*60][s] [864]w[ms]
1h=[1/24][天] [60][min] [60*60][s] [36]w[ms]
1min=[1/24/60][天] [1/60][h] [60][s] [0.6]w[ms]
1s=[1/24/60/60][天] [1/60/60][h] [1/60][min] [100][ms]
1ms=[1/24/60/60/100][天] [1/60/60/100][h] [1/60/100][min] [1/100][s]
###计算存储
1GB=[1024][MB] [1024*1024][KB] [1024*1024*1024][字节] [1024*1024*1024*8][byte]
1MB=[1/1024][GB] [1024][KB] [1024*1024][字节] [1024*1024*8][byte]
1KB=[1/1024/1024][GB] [1/1024]MB [1024][字节] [1024*8][byte]
1Byte=[1/1024/1024/1024][GB] [1/1024/1024][MB] [1/1024][KB] [8][byte]
1byte=[1/1024/1024/1024/8][GB] [1/1024/1024/8][MB] [1/1024/8][KB] [1/8][字节]
##############################################################################
- 输入win+R打开windows运行框
- 在运行框中输入:get ctrl ,输出:“^” ,获取固定值
- 在运行框中输入:get 24h ,输出:“1[天] =1440[min] =86400[s] =864w[ms]” ,获取动态值
- 在运行框中输入:get java_home 输出:“D:\jdk1.8_x64(用户)” ,获取环境变量
- 表达式按== 触发结果
3、演示
4、代码
#HotIf winActive("运行") and winActive("ahk_class #32770")
#MaxThreadsPerHotkey 10
;在运行框中执行强大的计算功能,包括数学运算等
:*?:==::{
try{
rawText:=ControlGetText("Edit1","A") ;
fullResult:=runbox.calculateExpression(rawText)
if fullResult{
ControlsetText(fullResult,"Edit1","A")
}
ControlSend("{END}","Edit1","A")
}
}
#HotIf
;==========================================================================================================系统快捷键映射
;----------------------------------------------------------------------------------------------------------运行框rubox 类 class
;执行各种运算取值
class runbox
{
static getconfigPath:=A_ScriptDir "\getconfig.txt"
;执行比表达式计算,"==" 触发,callflag是其他函数调用该方法
static calculateExpression(rawstr,callflag:=0)
{
;从配置文件getconfig.txt中获取值
if inStr(rawStr,"get ")==1 and (not (str:=Trim(Ltrim(rawStr,"get")))==Trim(rawStr)){
result:=this.getExpression(str,&prefix)
fulltxt:= rawStr . (prefix?"":"=") . result
return fulltxt
}
}
;计算get表达
static getExpression(str,&prefix)
{
getconfig:=ak.readFileToMap(this.getconfigPath)
if RegExMatch(str,"^([\d.]+)",&outn)==1{ ;倍数取值
mn:=outn[1] ;倍数
if not (value:=ak.mapget(getconfig,"1" . strReplace(str,mn)))
return
regmod:="\[([\+\-\*/\d.^%\(\)]+)\]"
while RegExMatch(value,regmod, &OutputVar){
prefix:=1
num :=ak.polish_notation( mn . "*" . "(" . OutputVar[1] . ")" )
value :=strReplace(value,"[" . OutputVar[1] . "]", ak.get_bignumber(num,3,0),,,1)
}
return value
}
prefix:=0
return ak.mapget(getconfig,str,1) or this.getEnvExpression(str) ;直接取值或者是取环境变量
}
;计算getEnv表达式
static getEnvExpression(key)
{
return ((s:=reg.getEnv(key,0))?s . "(系统)":"" ) . "`n" . ((u:=reg.getEnv(key))?u . "(用户)":"")
}
}
;----------------------------------------------------------------------------------------------------------运行框rubox 类 class
;----------------------------------------------------------------------------------------------------------Reg工具类
;注册表操作工具
class reg
{
;用户环境变量位置
static HCU:="HKEY_CURRENT_USER\Environment"
;系统环境变量位置
static HLM:="HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment"
;获取当前某个环境变量的值 ,默认是当前用户
static getEnv(key,user:=1)
{
return ak.mapget(this.getEnvMap(user),key,1)
}
;获取当前所有环境变量 ,默认是当前用户
static getEnvMap(user:=1){
regpath:=user?this.HCU:this.HLM
retMap:=Map()
Loop Reg, regpath ,"KV"{
retMap.set(A_LoopRegName,RegRead())
}
return retMap
}
;删除环境变量,默认当前用户
static delEnv(key,user:=1){
regpath:=user?this.HCU:this.HLM
RegDelete regpath ,key
}
;设置环境变量,立即生效,默认是当前用户
static setEnv(key,value,type:="REG_SZ",user:=1)
{
regpath:=user?this.HCU:this.HLM
RegWrite value, type, regpath, key
}
;添加一个键值对到path ,默认是当前用户
static pathPush(key,user:=1){
arr:=this.pathArr(user)
if(arr and ak.arrHas(arr,key))
return
else
arr.push(key)
pathStr:=ak.joinArr(arr,";","","")
this.setEnv("Path",pathStr,"REG_EXPAND_SZ",user)
}
;在path中删除一个键值,默认是当前用户
static pathPop(key,user:=1){
paths:=ak.arrDelete(this.pathArr(user),key)
pathStr:=ak.joinArr(paths,";","","")
this.setEnv("Path",pathStr,"REG_EXPAND_SZ",user)
}
;返回path集合array ,默认是当前用户
static pathArr(user:=1){
retarr:=[]
pathstr:=Rtrim(this.path(user),";")
loop parse ,pathstr,";"
retarr.push(A_loopField)
return retarr
}
;返回一个path字符串,默认是当前用户
static path(user:=1){
return this.getEnv("Path",user)
}
}
;----------------------------------------------------------------------------------------------------------Reg工具类
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ak工具类class
class ak{
;Fucn 逆波兰表达式计算 + - x ÷ 幂(**/^) 模(%) expression:数学表达式可以带括号
;参考:https://blog.csdn.net/assiduous_me/article/details/101981332
static polish_notation(expression)
{
operator_list:=Map("+",0,"-",0,"*",0,"`/",0,"%",0,"^",0) ;注意list的haskey操作只是检测索引
operatorlevel_map:=Map("(",0,"+","1","-",1,"*",2,"/","2","%",2,"^",3,")",4)
operator_map:=Map("+","add","-","sub" ,"*","multi","/","divi","%","mod2","^","pow")
expression:=strReplace(strReplace(RegExReplace(trim(expression),"\s+",""),"**","^") ,"(-","(0-")
expression:=inStr(expression,"-(")==1?strReplace(this.insertStrAt(expression,this.mirrorSymbolIndex(expression,"(",")"),")"),"-(","(0-("):expression
;①.获取一个中缀表达式集合类似 100+2 -> ["100","+","2"]
middlefix_list:=[],fix:=""
Loop parse,expression{
current_value:=A_LoopField
if(operatorlevel_map.has(current_value))
{
tmp:=""!=fix?middlefix_list.push(fix):""
middlefix_list.push(current_value)
fix:=""
}else fix:=fix . current_value
}
tmp2:=fix!=""?middlefix_list.push(fix):""
if(middlefix_list[1]="-"){ ;处理开头为负数
middlefix_list.insertAt(1,"(")
middlefix_list.insertAt(2,"0")
middlefix_list.insertAt(5,")")
}
;②.转换为后缀表达式(逆波兰表达式)
operator_stack:=[] ,suffix_list:=[],number_stack:=[]
for index ,currentElmt in middlefix_list
{
if(operator_list.has(currentElmt))
{
while(operator_stack.length>0 && operatorlevel_map.get(operator_stack.get(operator_stack.Length))>=operatorlevel_map.get(currentElmt))
suffix_list.push(operator_stack.pop())
operator_stack.push(currentElmt)
}else if(currentElmt=="(")
operator_stack.push("(")
else if(currentElmt==")"){
while(operator_stack.length>0 && operatorlevel_map.get(operator_stack.get(operator_stack.length))>operatorlevel_map.get("("))
suffix_list.push(operator_stack.pop())
if(operator_stack.length>0)
operator_stack.pop()
}else
suffix_list.push(currentElmt)
}
while(operator_stack.length>0)
suffix_list.push(operator_stack.pop())
;③.计算表达式最终的值,规则数字入栈,操作符就出栈两个元素计算值并把结果入栈
for key,opertor_or_number in suffix_list{
if(operator_list.has(opertor_or_number)){
number2:=number_stack.pop(),number1:=number_stack.pop()
tmpObj:={add:number1+number2,sub:number1-number2,multi:number1*number2,pow:number1**number2}
T1:=opertor_or_number=="/"?(tmpObj.divi:=number1/number2):"" ;除法容易引发除0异常
T2:=opertor_or_number=="%"?(tmpObj.mod2:=mod(number1,number2)):"" ;取模容易引发除0异常
number_stack.push(tmpObj.%operator_map.get(opertor_or_number)%)
}else
number_stack.push(opertor_or_number)
}
return number_stack.pop()
}
;Func 计算对称符号所在位置str:原字符串,firstIndex:左边符号所在位置,symbol:右边符号 返回右边符号在原字符串中索引 "-((10000+500)-500)/2" 返回18
static mirrorSymbolIndex(str,Lsymbol,Rsymbol)
{
flag:=false ,list:=[]
Loop Parse ,str {
if(Lsymbol==(sub:=subStr(Str,A_index,1))){
list.push(sub)
flag:=true
}
R:=Rsymbol==subStr(Str,A_index,1)?list.pop():""
if(list.length==0 and flag)
return A_index
}
return 0
}
;func 作用:处理大的数字,
;参数:bigNumber数字类型的大数字,char_flag:0,1(是否带k,w,y), scale 数字类型保留几位小数
;返回:返回字符串
;msgBox % Round(100,2)
static get_bignumber(bigNumber,scale:=0,char_flag:=1)
{
;判断有几位小数
index:=InStr(bigNumber,".")
left :=index=0?strLen(bigNumber):InStr(bigNumber,".")-1
unit:="",prefix:="="
if char_flag{
if(left==4) ;单位K
{
result:=Round(bigNumber/1000,scale)
prefix:=(result==bigNumber/1000)?"=":"≈"
unit:="k"
}else if(left>4 && left <9) ;单位w
{
result:=Round(bigNumber/10000,scale)
prefix:=(result==bigNumber/10000)?"=":"≈"
unit:="w"
}else if(left>=9) ;单位亿
{
result:=Round(bigNumber/100000000,scale)
prefix:=(result==bigNumber/100000000)?"=":"≈"
unit:="亿"
}else{ ;小于1k
result:=Round(bigNumber,scale)
prefix:=(result==bigNumber)?"=":"≈"
}
}else{ ;正常表示方式
result:=Round(bigNumber,scale)
prefix:=(result==bigNumber)?"=":"≈"
}
result:=RegExReplace(result,"\.0+$","") ;去掉 2.000这样式的
if(InStr(result,".")>0)
result:=RegExReplace(result,"0+$","")
return prefix . result . unit
}
;Func 帮助map获取值,优化原生map报错问题,ignoreCase是否忽略大小写
static mapget(map1,key,ignoreCase:=0)
{
try{
if ignoreCase{
for k ,v in map1 {
if k=key
return v
}
}
return map1.get(key)
}catch as e{
return ""
}
}
;Fucn读取配置文件到list返回一个Array,filePath:文件所在路径 ,注释符号"#"
static readFileToList(filePath)
{
lines:=[]
Loop read, filePath
tmp:=(line:=trim(A_LoopReadLine)) and (not (inStr(line,"#")==1))?lines.push(line):unset
return lines
}
;Fucn读取配置文件到map返回一个Map,filePath:文件所在路径 ,注释符号"#"
static readFileToMap(filePath,separator:="=")
{
configs:=Map()
for line in this.readFileToList(filePath){
configs[trim(subStr(line,1,inStr(line,separator)-1))]:=trim(subStr(line,inStr(line,separator)+1))
}
return configs
}
}
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ak工具类class
5、总结
由于很多代码可以复用,单个功能看上去代码很多,
完整功能参考windows超级运行框-表达式计算(12)汇总
各位大佬觉得还可以怎么get? 欢迎评论区留言讨论!