AutoHotkey Community

It is currently May 27th, 2012, 5:20 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: January 25th, 2011, 8:52 pm 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
I'm currently using the ansi version of Autohotkey_L but I'm trying to move to the unicode version by updating or dropping the scripts that don't work with it. Below is a script I use frequently to copy selected code to clipboard and run it as a separate script (i.e. for the convenient testing of small pieces of ahk code). The hotkey is (Win+Tab).

Code:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

/*
#c:: ;copies selected file paths from explorer to clipboard
Send ^c
Clipboard =
ClipWait,2
Clipboard := % Clipboard
SoundPlay *-1
return
*/

#Tab:: ;Run selected AHK code
If hClip := WinExist("ClipTest")
  WinKill, ahk_id %hClip%
Clipboard =
Send ^c
ClipWait,1
;clip := % Clipboard
ClipContent =
(
   %Clipboard%
   Esc::ExitApp
)
RunScript(ClipContent,"ClipTest")
SetTimer,ClosePipe,-30000
Return

ClosePipe:
Critical
While, hClip := WinExist("ClipTest")
{
  WinKill, ahk_id %hClip%
  Sleep, 1000
}
Critical, Off
Return


;Original script by Lexikos: Lexikos as the main source: http://www.autohotkey.com/forum/viewtopic.php?t=25867
;The function version below is from a post by HotkeyIt: http://www.autohotkey.com/forum/viewtopic.php?p=263671#263671
RunScript(TempScript, name="")
{
   pipe_name := name="" ? A_TickCount : name
   ; Before reading the file, AutoHotkey calls GetFileAttributes(). This causes
   ; the pipe to close, so we must create a second pipe for the actual file contents.
   ; Open them both before starting AutoHotkey, or the second attempt to open the
   ; "file" will be very likely to fail. The first created instance of the pipe
   ; seems to reliably be "opened" first. Otherwise, WriteFile would fail.
   pipe_ga := CreateNamedPipe(pipe_name, 2)
   pipe    := CreateNamedPipe(pipe_name, 2)
   if (pipe=-1 or pipe_ga=-1)
   {
      MsgBox CreateNamedPipe failed.
      ExitApp
   }
   Run, %A_AhkPath% "\\.\pipe\%pipe_name%",,,PID
   ; Wait for AutoHotkey to connect to pipe_ga via GetFileAttributes().
   DllCall("ConnectNamedPipe","uint",pipe_ga,"uint",0)
   ; This pipe is not needed, so close it now. (The pipe instance will not be fully
   ; destroyed until AutoHotkey also closes its handle.)
   DllCall("CloseHandle","uint",pipe_ga)
   ; Wait for AutoHotkey to connect to open the "file".
   DllCall("ConnectNamedPipe","uint",pipe,"uint",0)
   ; AutoHotkey reads the first 3 bytes to check for the UTF-8 BOM "?". If it is
   ; NOT present, AutoHotkey then attempts to "rewind", thus breaking the pipe.
   Script:= chr(239) chr(187) chr(191) TempScript
   
   if !DllCall("WriteFile","uint",pipe,"str",Script,"uint",StrLen(Script)+1,"uint*",0,"uint",0)
      MsgBox WriteFile failed: %ErrorLevel%/%A_LastError%
   DllCall("CloseHandle","uint",pipe)
   Return PID
}

; by Lexikos: http://www.autohotkey.com/forum/viewtopic.php?t=25867
CreateNamedPipe(Name, OpenMode=3, PipeMode=0, MaxInstances=255)
{
    return DllCall("CreateNamedPipe","str","\\.\pipe\" Name,"uint",OpenMode
        ,"uint",PipeMode,"uint",MaxInstances,"uint",0,"uint",0,"uint",0,"uint",0)
}


p.s. I tried to replace %clipboard% in ClipContent with the commented out "clip" variable (which is defined in a way that works for the commented out hotkey above it for something else, but when I run it like that I get a different error and the script crashes.

Any ideas how to make this work with AHK_L unicode?

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


Last edited by pajenn on January 25th, 2011, 9:28 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 25th, 2011, 8:58 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
What is the error? And how are you able to use it without the CreateNamedPipe() function?

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 25th, 2011, 9:41 pm 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
sinkfaze wrote:
What is the error? And how are you able to use it without the CreateNamedPipe() function?


I added CreateNamedPipe() function to the script. The functions are normally called from my Lib directory and this hotkey is usually part of my master hotkey script -- I forgot to include CreateNamedPipe() function when putting it into an "independent" script for demonstration purposes.

The error message box is:

Quote:
---------------------------
ClipTest
---------------------------
Error at line 1.

Line Text: �
Error: This line does not contain a recognized action.

The program will exit.
---------------------------
OK
---------------------------


except with a box symbol instead of a mystery question mark diamond symbol. I don't get the second error anymore, at least not right now - I'll post it later if I can recreate it.

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 25th, 2011, 11:04 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
Please use DynaRun
Code:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
DetectHiddenWindows,On

#Tab:: ;Run selected AHK code
If WinExist("ahk_pid " PID){
  WinKill,ahk_pid %PID%,,0.5 ;wait 0.5 seconds
  If WinExist("ahk_pid " PID)
      Process,Close,%PID%
}

Clipboard =
Send ^c
ClipWait,1
PID:=DynaRun(Clipboard "`nEsc::ExitApp","ClipTest")
SetTimer,ClosePipe,-30000
Return

ClosePipe:
While, WinExist("ahk_pid " PID)
{
  WinKill, ahk_pid %PID%
  Sleep, 1000
}
Return

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 25th, 2011, 11:45 pm 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
HotKeyIt wrote:
Please use DynaRun


Thank you. That seems to work.

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], HotkeyStick, XstatyK and 81 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group