PageRenderTime 74ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/SRC/Ahk/candy.ahk

http://candy-software.googlecode.com/
AutoHotKey | 377 lines | 291 code | 37 blank | 49 comment | 7 complexity | 5692a00420f6dbd7eaf165853976c9ff MD5 | raw file
  1. ;This is the autohotkey version.
  2. Process Priority,,High
  3. ;#NoTrayIcon
  4. #SingleInstance Ignore
  5. #Include sub/urlencode.ahk
  6. #Include sub/ini.ahk
  7. #Include sub/web_search.ahk
  8. #Include sub/get_processname.ahk
  9. #Include sub/asc_count.ahk
  10. SetWorkingDir,%A_ScriptDir% ;ÉèÖù¤×÷Ŀ¼ΪcandyËùÔÚĿ¼
  11. splitpath,A_ScriptDir,,,,,script_driver ;candyËùÔÚµÄÅÌ·û
  12. ;ÌáÈ¡ÃüÁîÐÐ
  13. If 0>0
  14. {
  15. Loop, %0%
  16. {
  17. tmp := %A_Index%
  18. If InStr( tmp, "/ini=", TRUE ) = 1
  19. StringReplace ,Settings_file,tmp,/ini=
  20. Else
  21. CommandLineInput=%CommandLineInput%%A_Space%%tmp%
  22. }
  23. }
  24. CandyInit:
  25. If Settings_file=
  26. {
  27. StringReplace, Settings_file, A_ScriptName, .exe, .ini
  28. }
  29. IfNotExist,%Settings_file%
  30. {
  31. Msgbox Usage: `n------------------------------`nThe name of your configuration file must be same as the executable file. %Settings_file%`nOr run with parameters "%A_ScriptName% /ini=anyname.ini" `n
  32. ;exitapp
  33. return
  34. }
  35. ini_Load(src,Settings_file)
  36. return
  37. CandyGo:
  38. WinGet,currwin_pid,pid,A ;µ±Ç°´°¿ÚµÄPID
  39. WinGetClass, currwin_class, A ;µ±Ç°´°¿ÚµÄclass
  40. WinGetTitle, currwin_Title, A ;µ±Ç°´°¿ÚµÄTitle
  41. ControlGet, currwin_hwnd,Hwnd,,,A ;µ±Ç°´°¿ÚµÄHandle
  42. currwin_fullpath:=GetModuleFileNameEx(currwin_pid) ;µ±Ç°´°¿ÚµÄfullpath
  43. splitpath,currwin_fullPath,,,,currwin_name, ;Çиǰ´°¿ÚµÄ·¾£¬»ñÈ¡´°¿ÚµÄname
  44. /*
  45. ÌáÈ¡Ä¿±ê
  46. */
  47. If CommandLineInput!= ;Èç¹ûÓÐÃüÁîÐвÎÊý£¬Ìø¹ýCTRL ¡Ä¿±ê
  48. {
  49. f_fileselected=%CommandLineInput%
  50. Goto Lable_CommandLineInput
  51. }
  52. ;**************************************************************************************************************************
  53. ;ûÓÐÃüÁîÐвÎÊý£¬ÔòÒª¿¿CTRL ¡Ä¿±êÁË
  54. IniRead,p_timewait_for_candy,%settings_file%,configuration,TimeWaitCandy,0.4
  55. Saved_ClipBoard := ClipboardAll
  56. clipboard =
  57. Send, ^c
  58. ClipWait,%p_timewait_for_candy%
  59. f_fileselected=%Clipboard%
  60. Clipboard := Saved_ClipBoard
  61. Saved_ClipBoard =
  62. ;**************************************************************************************************************************
  63. is_windowflag:=0
  64. If f_fileselected= ;Èç¹ûÕ³Ìù°åÀïÃæûÓÐÄÚÈÝ£¬Ôò½øÐÐcandywindows½ø³Ì
  65. {
  66. f_FileExt:="window"
  67. f_fileselected=%currwin_name%
  68. is_windowflag:=1
  69. GOTO Lable_WindowSelected
  70. ;;ExitApp
  71. return
  72. }
  73. ;ÌáÈ¡ºó׺
  74. Lable_CommandLineInput:
  75. IniRead,p_ShortText_length,%settings_file%,configuration,ShortText_length,80 ;ÌÎı¾µÄ³¤È
  76. IfExist,%f_fileselected% ;Èç¹ûÊǵçÄÔÀïÃæ´æÔÚµÄÎļþ
  77. {
  78. FileGetAttrib, f_file_Attrib, %f_fileselected% ;ÊÇÎļþµÄÇé¿öÏ£¬Çø·ÖÊÇ·ñÎļþ¼Ð,°Ñ Path Ö¸ÏòµÄÎļþ»òÎļþ¼ÐµÄÊôÐÔ¸³Öµ¸ø Attrib
  79. IfInString, f_file_Attrib, D ;Èç¹ûÔÚ Attrib ÀïÓÐ D ,¾Í±íʾÕâ¸ö·¾´ú±íµÄÊÇÎļþ¼Ð£¬·ñÔò¾ÍÊÇÎļþ
  80. {
  81. If(RegExMatch(f_fileselected,":\\$"))
  82. {
  83. f_FileExt:="Driver" ;ÊÇ·ñÅÌ·û
  84. }
  85. Else
  86. {
  87. f_FileExt:="Folder" ;ÊÇ·ñÎļþ¼Ð
  88. }
  89. SplitPath,f_fileselected,,f_FilePath,,f_filefoldernameonly,f_FileDriver
  90. }
  91. Else ;Èô²»ÊÇÎļþ¼ÐµÄ»°£¬ÔòÖ»ÄÜÊÇÎļþÁË
  92. {
  93. is_fileflag:=1
  94. splitpath,f_fileselected,f_FileName,f_FilePath,f_FileExt,f_FileNamenoext,f_FileDriver
  95. SplitPath,f_FilePath,,,,f_filefoldernameonly,
  96. ;StringReplace,f_FileDriverletter,f_FileDriver,: ;±»Ñ¡ÎļþµÄÅÌ·û£¬²»´øðºÅ
  97. If !f_FileExt ;ûÓкó׺µÄÎļþ
  98. {
  99. f_FileExt:="NoExt"
  100. }
  101. }
  102. }
  103. Else ;Èç¹ûÊÇÎı¾
  104. {
  105. is_textflag:=1
  106. If(RegExMatch(f_fileselected,"i)^(https://|http://|www.)([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"))
  107. {
  108. f_FileExt:="WebUrl" ;ÅÐÏÊÇ·ñÍøÖ·
  109. }
  110. Else If(RegExMatch(f_fileselected, "^[\w-_.]+@(?:\w+(?::\d+)?\.){1,3}(?:\w+\.?){1,2}$"))
  111. {
  112. f_FileExt:="Email" ;ÅÐÏÊÇ·ñemailµØÖ·
  113. }
  114. Else If (strlen(f_fileselected)<p_ShortText_length)
  115. {
  116. f_FileExt:="ShortText" ;ÅÐÏÊÇ·ñÌÎı¾
  117. }
  118. Else
  119. {
  120. f_FileExt:="LongText" ;ÅÐÏÊÇ·ñ³¤Îı¾
  121. }
  122. }
  123. ;ÔÄܹ»Ó®
  124. Lable_WindowSelected: ;Èç¹ûÊÇwindowsÔòÖ±½ÓÌøµ½ÕâÀïÀ´£¬±ÜÃâ±»ÈÏΪÊÇ0³¤ÈµÄshort_text
  125. abc=i)(^|\|)%f_fileext%($|\|) ;ÕýÔò±í´ïʽ°¡£¬ºÃÐÁ¿àµÄ£¬|±ØÐëÒªÓÐ \¿ªµÀ
  126. f_fileext_group:=ini_FindKeysRE(src, "associations", abc)
  127. IniRead,MyAppName, %Settings_file%,associations,%f_fileext_group%, ;ÔÚiniÎļþÀïÃæÕÒÆ¥Åä
  128. If MyAppName=Error ;Èç¹ûûÓÐÏàÓ¦ºó׺µÄ¨Òå
  129. {
  130. If is_fileflag=1 ;Èç¹ûûÓШÒ壬ÈôÊÇÎļþµÄ»°£¬¿´ÊÇ·ñÓÐanyfileµÄ¨Òå
  131. {
  132. IniRead,MyAppName, %Settings_file%,associations,AnyFile
  133. If MyAppName=Error
  134. {
  135. Run,%f_fileselected%, ,useerrorlevel ;UseErrorLevelÈç¹ûrunÃüÁîʧ°Ü£¬ºöÂÔ³ö´íÌáʾ²¢ÖÃerrorlevelΪError£¬Ï̼߳ÌÐøÍùÏ¡£
  136. ;ExitApp
  137. return
  138. }
  139. }
  140. Else if is_textflag=1 ;Èç¹ûûÓШÒ壬ºó׺ÊÇÎı¾£¬¿´ÊÇ·ñÓÐanytextµÄ¨Òå
  141. {
  142. IniRead,MyAppName, %Settings_file%,associations,AnyText
  143. If MyAppName=Error
  144. {
  145. ;ExitApp
  146. return
  147. }
  148. }
  149. Else ;ÆäËüµÄºó׺ÊÇÎļþ¼Ð¡¢ÅÌ·û¡¢´°ÌåµÈÁË£¬ÕâÖÖÇé¿öÏÂÖ±½ÓÍ˳ö³ÌÐò¼´¿É
  150. {
  151. ;ExitApp
  152. return
  153. }
  154. }
  155. ;**************************************************************************************************************************
  156. ;ÅÐÏÊÇQuickCandy£¬»¹ÊÇmenuCandy
  157. If(RegExMatch(MyAppName,"i)^(menu_)")) ;Èç¹ûÊÇÒÔmenu_¿ªÍ·,ÏÈÈ¥»­²Ëµ¥
  158. Goto Lable_DrawMenu
  159. Else ;·ñÔòÖ±½ÓÔËÐÐÓ¦ÓóÌÐò
  160. Goto Lable_RunApp
  161. Lable_DrawMenu:
  162. ;================²Ëµ¥µÚÒ»ÐУ¬ÏÞÖÆ×ÖÊýÔÚ20¸ö×Ö================================
  163. f_length_fileselected:=StrLen(f_fileselected)
  164. IfGreater,f_length_fileselected,20
  165. {
  166. stringleft,f_textmenu_left,f_fileselected,5
  167. StringMid, tmp, f_fileselected, 6, 1
  168. If (! IsChar(tmp) && Mod(AscCount(f_textmenu_left), 2) = 0 ) ;5¸ö×Ö·ûÖÐÓÐżÊý¸öASCII£¬àÈ¡Ò»¸ö×Ö½Ú
  169. {
  170. stringleft,f_textmenu_left,f_fileselected,6
  171. StringRight,f_textmenu_right,f_fileselected,11
  172. If (Mod(AscCount(f_textmenu_right), 2) = 0) ;11¸ö×Ö·ûÖÐÓÐżÊý¸öASCII£¬Ö»ÄÜÉÙÈ¡Ò»¸ö×Ö½Ú
  173. StringRight,f_textmenu_right,f_fileselected,10
  174. }
  175. Else
  176. {
  177. StringRight,f_textmenu_right,f_fileselected,12
  178. If (! IsChar(f_textmenu_right) && Mod(AscCount(f_textmenu_right), 2) = 1) ;12¸ö×Ö·ûÖÐÓÐÆæÊý¸öASCII£¬ÉÙÈ¡Ò»¸ö×Ö½Ú
  179. StringRight,f_textmenu_right,f_fileselected,11
  180. }
  181. menu mymenu,add,%f_textmenu_left% ¡­ %f_textmenu_right%,r_CopyFullPath ;¼ÓµÚÒ»Ðв˵¥£¬ÏÔʾѡÖеÄÄÚÈÝ£¬¸Ã²Ëµ¥ÈÃÄ㿽±´ÆäÄÚÈÝ
  182. }
  183. Else
  184. {
  185. Menu mymenu,add,%f_fileselected%,r_CopyFullPath ;¼ÓµÚÒ»Ðв˵¥£¬ÏÔʾѡÖеÄÄÚÈÝ£¬¸Ã²Ëµ¥ÈÃÄãÖ±½Ó´ò¿ªÅäÖÃÎļþ
  186. }
  187. ;================¸ù¾Ý¸÷ÀàÐÍ»­³ö²Ëµ¥================================
  188. IfInString,MyAppName,@
  189. {
  190. f_Menu_additional:=1
  191. StringReplace,MyAppName,MyAppName,@
  192. }
  193. key:=ini_GetKeys(src,MyAppName) ;µ÷ÓÃʱ£¬Èç¹ûÊDZäÁ¿¾Í²»ÓüÓ%%
  194. if key!=
  195. {
  196. menu mymenu,add
  197. Loop,Parse,key,`n
  198. {
  199. Menu,mymenu,Add,%A_LoopField%,r_Menu_handle
  200. }
  201. }
  202. If f_Menu_additional=1 ;Èç¹ûûÓÐ@£¬Ôò²»ÓÃÏÔʾ¸½¼ÓµÄ²Ëµ¥
  203. {
  204. menu mymenu,add
  205. f_Menu_additional:=0
  206. f_key_Menu_AnyFile:=ini_GetKeys(src,"Menu_AnyFile") ;µ÷ÓÃʱ£¬Èç¹ûÊdz£Á¿£¬Ò»¶¨Òª¼Ó""
  207. Loop,Parse,f_key_Menu_AnyFile,`n
  208. {
  209. Menu,mymenu,Add,%A_LoopField%,r_Menu_handle_AnyFile
  210. }
  211. }
  212. Menu,MyMenu,Show
  213. Menu, MyMenu, DeleteAll
  214. ;ExitApp
  215. return
  216. ;================²Ëµ¥´¦Àí================================
  217. r_Menu_handle:
  218. MyAppName:=ini_Read(src,MyAppName,A_ThisMenuItem)
  219. Goto Lable_RunApp
  220. r_Menu_handle_AnyFile:
  221. MyAppName:=ini_Read(src,"Menu_AnyFile",A_ThisMenuItem)
  222. Goto Lable_RunApp
  223. r_CopyFullPath:
  224. Clipboard:=f_fileselected
  225. ;ExitApp
  226. return
  227. /*
  228. ¨X¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨[
  229. ¨U ÔËÐд¦Àí¢ÚÔËÐд¦Àí ¨U
  230. ¨^¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨a
  231. */
  232. Lable_RunApp:
  233. If(RegExMatch(MyAppName,"i)^(https://|http://|www.)([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?")) ;Èç¹ûÊÇÍøÒ³ËÑË÷£¬ÒÔhttp¿ªÍ·£¬×ª³Éutf8
  234. {
  235. f_fileselected:=url2UTF8(f_fileselected)
  236. f_ÍøÖ·=%MyAppName%%f_fileselected%
  237. mywebrun:=websearch(Settings_file,f_ÍøÖ·,currwin_class,currwin_fullpath)
  238. Run, %mywebrun%,,useerrorlevel
  239. ;ExitApp
  240. return
  241. }
  242. If(RegExMatch(MyAppName,"i)^(@https://|@http://|@www.)([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?")) ;Èç¹ûÊÇÍøÒ³ËÑË÷£¬ÒÔ@http¿ªÍ·£¬×ª³Égbk
  243. {
  244. StringTrimLeft,MyAppName,MyAppName,1
  245. f_fileselected:=url2gbk(f_fileselected)
  246. f_ÍøÖ·=%MyAppName%%f_fileselected%
  247. mywebrun:=websearch(Settings_file,f_ÍøÖ·,currwin_class,currwin_fullpath)
  248. Run, %mywebrun%,,useerrorlevel
  249. ;ExitApp
  250. return
  251. }
  252. ;=========================================
  253. Else ;²»ÊÇÍøÖ·£¬ÔòÊÇÒ»°ãµÄÓ¦ÓóÌÐò
  254. {
  255. ;Ìæ»»µôµ±Ç°´°¿ÚÄÇÒ»²¿·ÖµÄ
  256. StringReplace,MyAppName,MyAppName,<win_class> ,%currwin_class%,All
  257. StringReplace,MyAppName,MyAppName,<win_title> ,%currwin_title%,All
  258. StringReplace,MyAppName,MyAppName,<win_hwnd> ,%currwin_hwnd%
  259. StringReplace,MyAppName,MyAppName,<win_name> ,%currwin_name%,All
  260. StringReplace,MyAppName,MyAppName,<win_fullpath> ,%currwin_fullpath%
  261. ;Ä¿µÄÊÇÏàÔ·¾
  262. StringReplace,MyAppName,MyAppName,<cp>,%A_ScriptDir%,All
  263. StringReplace,MyAppName,MyAppName,<cd>,%script_driver%,All
  264. ;һЩʱ¼ä²ÎÊý
  265. StringReplace,MyAppName,MyAppName,<year> ,%A_YEAR%,All
  266. StringReplace,MyAppName,MyAppName,<mon> ,%A_MM%,All
  267. StringReplace,MyAppName,MyAppName,<date> ,%A_dd%,All
  268. StringReplace,MyAppName,MyAppName,<hour> ,%A_YEAR%,All
  269. StringReplace,MyAppName,MyAppName,<min> ,%A_Min%,All
  270. StringReplace,MyAppName,MyAppName,<sec> ,%A_Sec%,All
  271. StringReplace,MyAppName,MyAppName,<wday> ,%A_WDay%,All
  272. StringReplace,MyAppName,MyAppName,<now> ,%A_Now%,All
  273. ;²Ù×÷ÔÏóÊÇÎļþµÄʱºò£¬½øÐÐÌæ»»µÄÔÏó
  274. StringReplace,MyAppName,MyAppName,<file_driver> ,%f_FileDriver%,All ;
  275. StringReplace,MyAppName,MyAppName,<file_path> ,%f_FilePath%,All ;²»´øÎļþÃûµÄ·¾
  276. StringReplace,MyAppName,MyAppName,<file_name> ,%f_FileNamenoext%,All ;´¿´âµÄÎļþÃû£¬ÎÞºó׺£¬ÎÞ·¾
  277. StringReplace,MyAppName,MyAppName,<file_ext> ,%f_FileExt%,All
  278. StringReplace,MyAppName,MyAppName,<file_foldername> ,%f_filefoldernameonly%,All
  279. StringReplace,MyAppName,MyAppName,<file_fullpath> ,%f_fileselected%,All
  280. ;ÌرðµÄ²ÎÊý
  281. IfInString,MyAppName,<clipon>
  282. {
  283. Clipboard:=f_fileselected
  284. StringReplace,MyAppName,MyAppName,<clipon>,,all
  285. }
  286. IfInString,MyAppName,<input>
  287. {
  288. Gui +LastFound +OwnDialogs +AlwaysOnTop
  289. InputBox, myinput,CandyInput,`n`n Please input your parameter: ,, 285, 175,,,,,
  290. if errorlevel
  291. ;ExitApp
  292. return
  293. Else
  294. StringReplace,MyAppName,MyAppName,<input>,%myinput%,all
  295. }
  296. IfInString,MyAppName,<hinput>
  297. {
  298. Gui +LastFound +OwnDialogs +AlwaysOnTop
  299. InputBox, myinput,CandyInput,`n`n Please input your parameter: ,Hide, 285, 175,,,,,
  300. if errorlevel
  301. ;ExitApp
  302. return
  303. Else
  304. StringReplace,MyAppName,MyAppName,<hinput>,%myinput%,all
  305. }
  306. IfInString,MyAppName,<select>
  307. {
  308. FileSelectFile, f_selectedname , , , Open a file,
  309. If f_selectedname <>
  310. {
  311. StringReplace,MyAppName,MyAppName,<select>,%f_selectedname%,all
  312. }
  313. Else
  314. {
  315. ;MsgBox,Canceled
  316. ;ExitApp
  317. return
  318. }
  319. }
  320. ;clear runn#
  321. clear=|||
  322. StringSplit,runn,clear,|
  323. ;Ìæ»»Íê³É£¬¾ÍÊÇÔËÐÐÁË
  324. StringSplit,runn,MyAppName,|
  325. If runn2=
  326. {
  327. If is_windowflag!=1 ;Èç¹ûÊÇwindows£¬ÄÇô¡°²Ù×÷ÔÏ󡱿ÉÒÔÖÿÕ
  328. runn2="%f_fileselected%"
  329. }
  330. ;MsgBox run %runn1%`, %runn2%`,%runn3%`,%runn4%
  331. if runn1="" and runn2=""
  332. return
  333. run,%runn1% %runn2%,%runn3%,%runn4% useerrorlevel
  334. ;1:³ÌÐò 2:Ä¿±ê 3:¹¤×÷Ŀ¼ 4:״̬
  335. ;ExitApp
  336. return
  337. }
  338. ;**************************************************************************************************************************
  339. ;by wannainshuyao@gmail.com
  340. ;**************************************************************************************************************************