6 votes

Please proofread this AI-generated AutoHotkey script

For my job I need to copy a person's name out of a field and split it into first and last names. I have a AHK script that can do this but my version only works with simple, one-word, non-hyphenated names. I tried prompting Gemini as below.

There is a line of text consisting of one or more words, a comma, then one or more words. write an autohotkey 1.1 script to select everything before the comma, store it to a variable called firstname, then select everything after the comma and store it to a variable called lastname. thanks

(the firstname and lastname variables should be swapped as the last name would come before the comma, that was my error)

Here's the AutoHotkey 1.1 script to achieve what you described:

; This script splits a text line into first and last name based on a comma

#NoEnv  ; Recommended for performance and compatibility.
SendMode Input  ; Simulates keystrokes for reliable input.

; Function to split the text
Func SplitText(_text, firstName, lastName) {
  StringSplit, parts, _text, `,`
  if (parts[1]) {
    firstName := parts[1] ; Assign first name
    lastName := SubStr(_text, 1 + StrLen(parts[1])+1) ; Extract last name after comma (including space)
  } else {
    firstName := _text ; Handle cases without a comma
    lastName := ""
  }
}

; Hotkey to trigger the script (replace F2 with your desired key)
F2::
  Send, ^c ; Copy the selected text to clipboard
  clipText := ClipboardAll  ; Read the clipboard content
  SplitText(clipText, firstName, lastName) ; Call the function to split the text
  MsgBox, First Name: %firstName%`nLast Name: %lastName%

  ; Optional: Clear clipboard to avoid unintended pasting (comment out if not desired)
  ; Clipboard := 
Return

Explanation:

#NoEnv and SendMode Input: These lines are included for performance and reliable input simulation.
Func SplitText(_text, firstName, lastName): This function takes the text (_text) as input and assigns the split parts to variables firstName and lastName.
StringSplit, parts, _text, ,:** Splits the text based on the comma (,`) delimiter.
if (parts[1]):** Checks if there's a second part (i.e., text after the comma).
If true, the first part is assigned to firstName and the text after the comma (including the space) is extracted using SubStr and assigned to lastName.
else:** If there's no comma, the entire text is assigned to firstName and lastName is set to an empty string.
F2::: This line defines a hotkey. When F2 is pressed, the following actions occur:
Send, ^c:** Copies the selected text to the clipboard.
clipText := ClipboardAll:** Reads the copied text from the clipboard and stores it in the variable clipText.
SplitText(clipText, firstName, lastName):** Calls the function to split the copied text into first and last names.
MsgBox:** Displays a message box showing the extracted first name and last name.
Optional: Clear Clipboard: The commented line Clipboard := clears the clipboard content after processing. Uncomment this line if you want to avoid accidentally pasting the copied text elsewhere.

Edit: To answer questions, and provide info I probably should have from the beginning: I'm a medical transcriptionist using AHK with my job's software (Emdat Inscribe) and a word expander (Instant Text). Inscribe seems to be browser-based but functions as a standalone app. I already have dozens of AHK 1.1 macros; my .ahk file has over 2000 lines, although I'm sure it's terribly written code. This is also why I'm disinclined to switch to AHK 2.0 since I have no desire to rewrite everything unless absolutely necessary. The first part of this macro is a bunch of keypresses to jump to the relevant patient info field, then this part would be used to store the names appropriately. I already have hotkeys to use the variables as needed and most macros are limited with #ifwinactive to Inscribe.

7 comments

  1. [3]
    RheingoldRiver
    Link
    I'm not that "fluent" in ahk but it looks more or less sane to me. A few things though: F2 is the "rename" hotkey in Windows. You probably want to restrict this via some #IfWinActive if you are...

    I'm not that "fluent" in ahk but it looks more or less sane to me. A few things though:

    • F2 is the "rename" hotkey in Windows. You probably want to restrict this via some #IfWinActive if you are set on that hotkey, or otherwise change to something that you're comfortable being bound globally
    • FYI ahk 1.1 is deprecated, possibly EOL. You should switch to ahk 2 if you are having an llm write the code for you anyway, no reason to be on an old version.
    • Agree with the other commenter, why ahk? If you are copying something from a browser you can make a much nicer UX using javascript in something like tampermonkey. If it's a text file maybe a python script. AHK is absolutely wonderful when you legitimately need to send a bunch of keypresses but I don't think that's the case here. Can you describe your workflow a little? (what program are you copying from, where is the data going, is this a one-at-a-time thing or are there many to process at once, etc)
    11 votes
    1. [2]
      zptc
      Link Parent
      edited

      edited

      2 votes
      1. RheingoldRiver
        Link Parent
        ok with this edit, sounds like you're in a good place!

        ok with this edit, sounds like you're in a good place!

        3 votes
  2. [2]
    Carrow
    Link
    It reads like it'll do what it says and gave a reasonable explanation. That script will perform the operation on the highlighted text, but will just give you a pop up telling you the first and...

    It reads like it'll do what it says and gave a reasonable explanation. That script will perform the operation on the highlighted text, but will just give you a pop up telling you the first and last name. That is suitable for checking if the script does what you expect it to, but probably isn't what you actually want. You'll probably want to substitute out the MsgBox line for a Send command to write over the highlighted text. Changing the hotkey off of plain F2 is also a good idea (unless your workflow is already amenable to that key).

    This seems like an excellent use case for AHK if that is the intended behavior and you want it "on-demand" instead of bulk/batch operations.

    2 votes
    1. zptc
      Link Parent
      thanks, edited with more info

      thanks, edited with more info

  3. [2]
    ebonGavia
    Link
    I don't mean this in a rude way, but is there a reason we're working in AHK? I'm not being unhelpful, but this is possibly one of the last languages on Earth for which I'd qualify to be helpful. I...

    I don't mean this in a rude way, but is there a reason we're working in AHK? I'm not being unhelpful, but this is possibly one of the last languages on Earth for which I'd qualify to be helpful.

    I mean just looking at it nothing jumps out. But I think Gemini might be able to help you more than I can.

    8 votes