Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Scripting on the fly (from any textual source)


  • Please log in to reply
10 replies to this topic
berban
  • Members
  • 202 posts
  • Last active: Apr 12 2019 01:08 AM
  • Joined: 30 Dec 2009
I'm sure most of you will already have incorporated this functionality into your autohotkey routines. (But if not, then you should!!)

The idea is basically to create a hotkey that will execute the selected text as code, with the addition of a few desirable features like a function library and hotkeys to pause/suspend/exit.

Here's a superior version with HotKeyIt's DynaRun()

DynaRun(TempScript, pipename="") 
{ 
   static _:="uint" 
   @:=A_PtrSize ? "Ptr" : _ 
   If pipename = 
      name := "AHK" A_TickCount 
   Else 
      name := pipename 
   __PIPE_GA_ := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0) 
   __PIPE_    := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0) 
   if (__PIPE_=-1 or __PIPE_GA_=-1) 
      Return 0 
   Run, %A_AhkPath% "\\.\pipe\%name%",,UseErrorLevel HIDE, PID 
   If ErrorLevel 
      MsgBox, 262144, ERROR,% "Could not open file:`n" __AHK_EXE_ """\\.\pipe\" name """" 
   DllCall("ConnectNamedPipe",@,__PIPE_GA_,@,0) 
   DllCall("CloseHandle",@,__PIPE_GA_) 
   DllCall("ConnectNamedPipe",@,__PIPE_,@,0) 
   script := (A_IsUnicode ? chr(0xfeff) : (chr(239) . chr(187) . chr(191))) . TempScript 
   if !DllCall("WriteFile",@,__PIPE_,"str",script,_,(StrLen(script)+1)*(A_IsUnicode ? 2 : 1),_ "*",0,@,0) 
      Return A_LastError 
   DllCall("CloseHandle",@,__PIPE_) 
   Return PID 
}

+^n::
BackupClip := ClipBoardAll 
Clipboard = 
Send ^c 
ClipWait, .5 
If ErrorLevel { 
   SoundPlay %A_Windir%\Media\Windows Ding.wav 
   Return 
} 
ScriptSource := Clipboard 
Clipboard := BackupClip 
;The commented-out lines are shortcuts that I like - for example, I can type "m var" instead of "MsgBox, %var%" to save time.
;ScriptSource := RegExReplace(ScriptSource, "m)^\s+")
;ScriptSource := RegExReplace(ScriptSource, "m)^s ", "Send % ")
;ScriptSource := RegExReplace(ScriptSource, "m)^m ", "MsgBox % ")
;ScriptSource := RegExReplace(ScriptSource, "m)^si ", "SendInput % ")
;ScriptSource := RegExReplace(ScriptSource, "m)^l(\d+)", "Sleep $1000")
;ScriptSource := RegExReplace(ScriptSource, "\bcb\b", "ClipBoard")
ScriptName := SubStr(RegExReplace(ScriptSource, "[/\\*?<>]+", " "), 1, 200)
DynaRun("#NoEnv`r`n`r`nMenu, Tray, Icon, Shell32.dll, 13`r`nMenu, Tray, Tip, % """ . ScriptName . """`r`nOnExit, OnExit`r`n`r`n" . ScriptSource . "`r`n`r`nExitApp`r`n`r`n^;::Pause`r`n`r`n^Del::`r`nExitApp`r`nReturn`r`n`r`nOnExit:`r`nMenu, Tray, NoIcon`r`nProcess, Exist`r`nGroupAdd, This, ahk_pid %ErrorLevel%`r`nWinHide, ahk_group This`r`nSoundPlay, %A_WinDir%\Media\Speech On.wav, Wait`r`nExitApp`r`nReturn", ScriptName)
return

The script that's actually executed will look like this (with my preferences):

#NoEnv
#Include C:\Documents\AutoHotkey\Lib.ahk ;location of my functions library

Menu, Tray, Icon, Shell32.dll, 13 ;custom icon - computer chip
Meny, Tray, Tip, <automatically generated name based on parameters>
OnExit, OnExit

;commands go here

ExitApp

^;::Pause

^Del::
ExitApp
Return

OnExit:
Menu, Tray, NoIcon
SoundPlay, %A_WinDir%\Media\Speech On.wav, Wait ;plays a sound when done
ExitApp
Return

For example, if I type the text "MsgBox, Test", and then select it and press ^+n, a msgbox will pop up. When I press ok, or when I press ^Delete, the script will exit and leave no trace. It's really handy for executing test code in the forums, or for quickly bringing autohotkey functionality into another program. For example, if I need to type a list of 100 numbers in notepad, I can just type "loop 100 / send %A_Index%. `n", select it, and execute it. The send command will overwrite the code I just typed with the numbers, leaving a clean list. It's also handy for testing out snippets of code - for example, testing the appearance of a gui without running the whole script.

Find me on the new AutoHotkey forums and send me a message if you have a question about any of the scrips I've posted to this forum!


DarkVamprism
  • Members
  • 125 posts
  • Last active: Apr 25 2017 10:28 AM
  • Joined: 03 Sep 2009
First off I would like to say that this is awesome, I love the idea BUT because my autohotkey isnt set as the default for .ahk files this fails
I guess a way to fix this would be to read the Autohotkey from registry or make it so that the first time you run it then it makes you show it where Autohotkey is and saves it in an ini and then runs the script by using command line?

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
You could use DynaRun() to avoid creating a file on disk.
Also you would not need to have AutoHotkey installed ;)
+^n::
BackupClip := ClipBoardAll
Clipboard =
Send ^c
ClipWait, .5
If ErrorLevel {
   SoundPlay %A_Windir%\Media\Windows Ding.wav
   Return
}
ScriptSource := Clipboard
Clipboard := BackupClip
ScriptSource := RegExReplace(ScriptSource, "m)^\s+")
ScriptSource := RegExReplace(ScriptSource, "m)^s ", "Send % ") ;these just allow me to take shortcuts when writing quick snippets of code
ScriptSource := RegExReplace(ScriptSource, "m)^m ", "MsgBox % ")
ScriptSource := RegExReplace(ScriptSource, "m)^si ", "SendInput % ")
ScriptSource := RegExReplace(ScriptSource, "m)^l(\d+)", "Sleep $1000")
ScriptSource := RegExReplace(ScriptSource, "\bcb\b", "ClipBoard")
DynaRun("#NoEnv`r`nMenu`, Tray`, Icon`, Shell32.dll`, 13`r`nOnExit`, OnExit`r`n" ScriptSource "`r`nExitApp`r`n^;::Pause`r`n^Del::`r`nOnExit:`r`nMenu`, Tray`, NoIcon`r`nSoundPlay`, `%A_WinDir`%\Media\Speech On.wav`, Wait`r`nExitApp`r`nReturn","QuickScript.ahk")
return


DarkVamprism
  • Members
  • 125 posts
  • Last active: Apr 25 2017 10:28 AM
  • Joined: 03 Sep 2009
It doesnt work after I have compiled it =S it says

Could not open file:
"\\.\pipe\AHK20178765"

Just to clarify I was trying to run this code

Msgbox, hmm

so its not the coding =\ Also because of the error it stops the rest of my script working

Edit: Ok I sussed it out and it was a problem finding Autohotkey.exe
I forgot I hadnt installed it on this computer (Although it is on my external harddrive)

Here is a modified version of DynaRun() that lets you specify the path of Autohotkey the first time you run it (and anytime it runs and cant find it)
IniRead, D_AhkPath, %A_ScriptDir%\DynaRun.ini, Settings, AhkPath, Error

if Not ( FileExist(D_AhkPath) )
{
   FileSelectFile, D_AhkPath, 1, %A_AhkPath%, Find your Autohotkey.exe, Autohotkey.exe (Autohotkey.exe; *.exe)
   IniWrite, %D_AhkPath%, %A_ScriptDir%\DynaRun.ini, Settings, AhkPath
}

DynaRun(TempScript, pipename="") 
{ 
   Global D_AhkPath
   static _:="uint" 
   @:=A_PtrSize ? "Ptr" : _ 
   If pipename = 
      name := "AHK" A_TickCount 
   Else 
      name := pipename 
   __PIPE_GA_ := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0) 
   __PIPE_    := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0) 
   if (__PIPE_=-1 or __PIPE_GA_=-1) 
      Return 0 
   Run, %D_AhkPath% "\\.\pipe\%name%",,UseErrorLevel HIDE, PID 
   If ErrorLevel 
   {
      MsgBox, 262144, ERROR,% "Could not open file:`n" __AHK_EXE_ """\\.\pipe\" name """ " D_AhkPath
      return
   }
   DllCall("ConnectNamedPipe",@,__PIPE_GA_,@,0) 
   DllCall("CloseHandle",@,__PIPE_GA_) 
   DllCall("ConnectNamedPipe",@,__PIPE_,@,0) 
   script := (A_IsUnicode ? chr(0xfeff) : (chr(239) . chr(187) . chr(191))) . TempScript 
   if !DllCall("WriteFile",@,__PIPE_,"str",script,_,(StrLen(script)+1)*(A_IsUnicode ? 2 : 1),_ "*",0,@,0) 
      Return A_LastError 
   DllCall("CloseHandle",@,__PIPE_) 
   Return PID 
}


HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
A compiled script cannot run a script from file, only internal Script.

For compiled you can use Autohotkey.dll and AutoHotkey.exe from same package
Download and Docs
If 0
	FileInstall, AutoHotkey.dll,AutoHotkey.dll
	
ahk:=AhkDllThread("AutoHotkey.dll")
Return

+^n::
BackupClip := ClipBoardAll
Clipboard =
Send ^c
ClipWait, .5
If ErrorLevel {
   SoundPlay %A_Windir%\Media\Windows Ding.wav
   Return
}
ScriptSource := Clipboard
Clipboard := BackupClip
ahk.ahktextdll("#NoEnv`r`nMenu`, Tray`, Icon`, Shell32.dll`, 13`r`nOnExit`, OnExit`r`n" ScriptSource "`r`nExitApp`r`n^;::Pause`r`n^Del::`r`nOnExit:`r`nMenu`, Tray`, NoIcon`r`nSoundPlay`, `%A_WinDir`%\Media\Speech On.wav`, Wait`r`nExitApp`r`nReturn")
return


  • Guests
  • Last active:
  • Joined: --

You could use DynaRun() to avoid creating a file on disk.
Also you would not need to have AutoHotkey installed ;)

The op's script is win9x compatible, whereas (if I am not mistaken, ) DynaRun() is not. It does matter to some users, so that should be made clear.

berban
  • Members
  • 202 posts
  • Last active: Apr 12 2019 01:08 AM
  • Joined: 30 Dec 2009

Also you would not need to have AutoHotkey installed :)


Often I use this on a thumb drive, in which case you can just say
Run, AutoHotkey.exe %A_Desktop%\QuickScript.ahk
which would just open the ahk with the exe.

Do you guys all use DynaRun() on a frequent basis? I use this functionality maybe 5-10 times daily.

Find me on the new AutoHotkey forums and send me a message if you have a question about any of the scrips I've posted to this forum!


swampica
  • Guests
  • Last active:
  • Joined: --
is there a functioning version of this script?? or some alternative?

berban
  • Members
  • 202 posts
  • Last active: Apr 12 2019 01:08 AM
  • Joined: 30 Dec 2009
oh, sorry, I messed up a few things

It should work now. I took out my own function library, which is pretty useless to other people :roll: but I do recommend you include a function library with this kind of thing, since the whole point is convenience!

Find me on the new AutoHotkey forums and send me a message if you have a question about any of the scrips I've posted to this forum!


RaptorX
  • Members
  • 751 posts
  • Last active: Feb 19 2015 02:47 AM
  • Joined: 19 Feb 2010
I have a pretty similar concept in my Autohotkey Toolkit script.

What i did is that i check the registry. If autohotkey is not installed then i pass the script to a copy of ahk.exe that comes with my script, that way it runs everywhere with no exceptions. :D

Besides of simply running scripts with a hotkey, my toolkit has a little editor in which you can type in your code and just hit a "Run" button so you can test code very quickly without having to create a file and save it, so again is a fairly similar concept. 8)

  • Guests
  • Last active:
  • Joined: --
Great thanks for script and function, if i understanding enough (cause i am newbie), may somebody answer me script could be reloaded when it's return ErrorLevel looks like function(library) that cannot be opened :?:

I think about replacing string that containing #Include from clipboard with new one that should be a special dir path with functions library or add new one if not and run from clipboard again, but don't know how to use ErrorStdOut fo this...
It would be great if somebody helps, sorry for my English 8)