AutoHotkey Community

It is currently May 26th, 2012, 11:59 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Advanced variables
PostPosted: May 7th, 2005, 10:19 pm 
Offline

Joined: July 30th, 2004, 8:50 pm
Posts: 192
8) I don't know when all these functions were introduced, but for those who haven't come across them yet, here's a script that uses some pretty impressive AHK variable functions.

I've put them in an AutoComplete program that reads the users settings from a file, and uses them directly through variables.

The advanced stuff is
Quote:
%line1%=%line2%
and
Quote:
Transform,output,Deref,`%%input%`%


Want an explanation? Ask magician Chris!

Skrommel


Code:
; ShortHand
;
; Automatically expands shorthand
;
; The shorthand is read from ShortHand.ini, with the
; text to look for on the first line, and the text to replace
; it with on the following line like this:
;
; btw
; by the way
; otoh
; on the other hand
;
; !=alt, ^=ctrl, +=shift, #=windows, so use ` to override,
; as in `!=!. Also, you can use {Tab}{Enter}{Left}{PgUp} etc...

Menu,Tray,NoStandard
Menu,Tray,Add,&Edit ShortHand.ini,EDIT
Menu,Tray,Add,&Reload ShortHand.ini,LOAD
Menu,Tray,Add,E&xit,EXIT

SetKeyDelay,-1

LOAD:
counter=0
Loop
{
  counter+=1
  FileReadLine,line1,%A_WorkingDir%\ShortHand.ini,%counter%
  counter+=1
  FileReadLine,line2,%A_WorkingDir%\ShortHand.ini,%counter%
  if ErrorLevel<>0
    break
  %line1%=%line2%                             ;Some clever AHK-stuff
}

START:
Input,input,I V,.:`,;{Space}{Tab}{Esc}{Enter}
Transform,output,Deref,`%%input%`%            ;More clever AHK-stuff
StringLen,length,output
if length>0
{
  StringLen,length,input
  Send,{Left}{Backspace %length%}%output%{Right}
}
Goto,START

EDIT:
run,notepad %A_WorkingDir%\ShortHand.ini
Return

EXIT:
ExitApp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 8th, 2005, 12:18 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
So, you rewrote HotStrings, using a script and an ini file, instead of just a script with all the HotStrings. It is certainly educational for newcomers.

You don’t even need
Code:
Transform,output,Deref,`%%input%`%
regardless of how clever it is. Look at some of the keyboard remappers. They define variable names from the input and assign them the output, too, and you can simply send them.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Help file
PostPosted: May 8th, 2005, 1:48 am 
Offline

Joined: July 30th, 2004, 8:50 pm
Posts: 192
:D Should I reread the help file, you think?

I made the script in response to http://www.autohotkey.com/forum/viewtopic.php?t=3302. If you can show me a way of making an end user definable autoreplacer using hotstrings from a compiled script, I'd be grateful. But autoreplacers wasn't my point.

AHK is the most dynamic language since my HP 48G calculator, which could rewrite it's own running programs. I don't think AHK needs to go that far, but dynamic code can really simplify some chores.

Isn't that what we all want?

Skrommel


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 8th, 2005, 3:24 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
I copied the script below from the forum, but I don’t remember, who posted it. I apologize to the author, but I changed the script name, the comments, etc., to my taste, and I don’t have the original. (This is the version I use.) The basic idea remained: the user is asked for a shorthand for the marked text, which is used to form a HotString command, appended to the script, which is then reloaded. It even has an error check, for malformed HotsStrings your default editor is called with the script already loaded, so you can edit it. It handles special characters and multi-line replacement texts well.
Code:
#h::  ; Win+H hotkey; The marked text as HotString is appended to this script, reloaded

AutoTrim Off  ; Retain any leading and trailing whitespace on the clipboard.
ClipboardOld = %ClipboardAll%
ClipBoard =   ; Clear to be able to see a change

Send ^c
ClipWait 1

; Replace CRLF and/or LF with `n for use in a "send-raw" hotstring
; The same for other characters that might be a problem in raw mode

StringReplace Hotstring, Clipboard, ``, ````, All  ; Do this replacement first to avoid interfering with the others below.
StringReplace Hotstring, Hotstring, `r`n, ``r, All ; Using `r works better than `n in MS Word, etc.
StringReplace Hotstring, Hotstring, `n, ``r, All
StringReplace Hotstring, Hotstring, %A_Tab%, ``t, All
StringReplace Hotstring, Hotstring, `;, ```;, All

Clipboard = %ClipboardOld%       ; Restore previous contents of clipboard.

SetTimer, MoveCaret, 10          ; Move the InputBox's caret to a friendly position

; Show the InputBox, providing the default hotstring
InputBox Hotstring, New Hotstring, Type your abreviation at the insertion point.`nYou can also edit the replacement text.`n`nExample entry: :R:btw`::by the way,,,,,,,, :R:`::%Hotstring%
IfNotEqual ErrorLevel,0,Return   ; The user pressed Esc or clicked Cancel.

IfInString Hotstring, :R`:::
{
   MsgBox You didn't provide an abbreviation. The hotstring has not been added.
   return
}

FileAppend, `n%Hotstring%, %A_ScriptFullPath% ; Add the hotstring, reload the script
Reload

Sleep 1000 ;  Error brings you here, otherwise Reload closes this instance during the Sleep
MsgBox 4,, The hotstring just added is improperly formatted.`nWould you like to open the script for editing?`nThe bad hotstring is at the bottom of the script.
IfMsgBox Yes, Edit
return

MoveCaret:                       ; Another thread to move the caret
IfWinNotActive, New Hotstring
   return
Send {Home}{Right 3}             ; Insertion point -> where to type the abbreviation.
SetTimer, MoveCaret, Off         ; Runs only once
return
It is not a compiled script, though. Your posting did not say it was a requirement. Is your point that a dumb user can handle separate lines better than ::Abbreviation::ReplacementString combinations? Or, you don’t want to install AHK in the user’s computer? It is there hidden in the compiled script, anyway. But your script does provide some HotString functionality from a compiled script.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 8th, 2005, 3:52 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
There is a drawback of your script: the abbreviation text must be a well formed variable name, so "a-z" or "100%" cannot be expanded. You can get around this by applying the Hexify() function from the list manipulation library.

What I meant about the Transform, Deref magic is that
Code:
Transform,output,Deref,`%%input%`%
has the same effect as
Code:
output := %input%
which looks easier to understand.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Compiled script
PostPosted: May 8th, 2005, 12:47 pm 
Offline

Joined: July 30th, 2004, 8:50 pm
Posts: 192
:D So I need to reread the help file after all!

I wasn't aware that := expands a variable reference. Thanks Laszlo!

I find that very few end users wants the hazzle of installing AHK just to try a script, not to mention keeping up with the latest version to make everything work. So I compile.

Hexing the variables is so clever! Thank's again, Laszlo!

So here's the elaborate version.

Skrommel


Code:
; Automatically expands shorthand
;
; The shorthand is read from ShortHand.ini, with the
; text to look for on the first line, and the text to replace
; it with on the following line like this:
;
; btw
; by the way
; otoh
; on the other hand
;
; !=alt, ^=ctrl, +=shift, #=windows, so use ´ to override,
; as in ´!=!. Also, you can use {Tab}{Enter}{Left}{PgUp} etc...

SetKeyDelay,-1
Menu,Tray,NoStandard
Menu,Tray,Add,&Edit ShortHand.ini,EDIT
Menu,Tray,Add,&Reload ShortHand.ini,LOAD
Menu,Tray,Add,E&xit,EXIT

IfNotExist,%A_WorkingDir%\ShortHand.ini
  FileAppend,btw`nby the way,%A_WorkingDir%\ShortHand.ini

counter=0
Loop
{
  counter+=1
  FileReadLine,line1,%A_WorkingDir%\ShortHand.ini,%counter%
  counter+=1
  FileReadLine,line2,%A_WorkingDir%\ShortHand.ini,%counter%
  if ErrorLevel<>0
    break
  ;hexify to allow for more complex shorthand
  hexed:=Hexify(line1)
  %hexed%=%line2%
}

START:
Input,input,I V,.:`,;{Space}{Tab}{Esc}{Enter}
hexed:=Hexify(input)
;variable reference, same as Transform,output,Deref,`%%hexed%`%
output:=%hexed%
StringLen,length,output
if length>0
{
  StringLen,length,input
  Send,{Left}{Backspace %length%}%output%{Right}
}
Goto,START

EDIT:
run,notepad.exe %A_WorkingDir%\ShortHand.ini
Return

LOAD:
Reload

EXIT:
ExitApp


;Function Hexify by Laszlo
Hexify(x)                     ; Convert a string to a huge hex number starting with X
{
   StringLen Len, x
   format = %A_FormatInteger%
   SetFormat Integer, H
   hex = X
   Loop %Len%
   {
      Transform y, ASC, %x%   ; ASCII code of 1st char, 15 < y < 256
      StringTrimLeft y, y, 2  ; Remove leading 0x
      hex = %hex%%y%
      StringTrimLeft x, x, 1  ; Remove 1st char
   }
   SetFormat Integer, %format%
   Return hex
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 8th, 2005, 12:59 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Nice Script,

Isn't it possible to use a endless loop for the checking the input, instead of using "Start: / Goto Start"?

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 8th, 2005, 4:40 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Now it looks great! Thanks for posting it. The abbreviations have practically no restrictions any more, so we have the same flexibility as AHK’s built-in hotstrings.

(...and only losers read documentation.)

Before I integrate it into my wife’s utility script, some little enhancements would be nice. If the ini file gets screwed up (and knowing her it takes only minutes) it is tedious to figure the even-odd line numbers. It could be easier for me to fix it if the file had single line hotstrings like

Abbreviation->Replacement string

Of course, it means loosing possible substrings "->" inside the abbreviation, but it is a little price for my timesaving in fixing problems. Any other weird character or character combination could be used as separator, like ASCI 187: "»".

(Even better: abbreviations should be terminated by Enter, Space, Tab, ', ", ",", ".", ":", ";", "!", "?", "/", "]", ")", "}" – so the first occurrence of any of them in the line could be the separator. ":" or "/" look the most intuitive.)

Also, automatic extension of the ini file would be nice, similar to the other script from my earlier posting. When a piece of text is marked, a hotkey activated function asks for an abbreviation and inserts the corresponding replacement rule into the ini file, and activates the new rule.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Another one
PostPosted: May 8th, 2005, 11:19 pm 
Offline

Joined: July 30th, 2004, 8:50 pm
Posts: 192
Check out http://www.autohotkey.com/forum/viewtopic.php?p=21844#21844.

Skrommel


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], Google Feedfetcher, IsNull and 55 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