'How to check what the first word in clipboard is?

Lets say my Cliboard variable is currently in this state:

Clipboard:= "-Classes Essay on Hobbes

I want to perform If conditions depending on the first block/word in the clipboard, in this case -Classes . Essentially doing something like below using FileAppend:

if "-diary"{
    ;Create file in /Diary/Entry 27.MD
}

Else if  "-Classes"{
    ;Create file in /Classes/Essay on Hobbes.MD
}

I was able to find this page in the docs If Var in/contains MatchList - Syntax & Usage | AutoHotkey but its not positional, which means if the block -Classes is also present as part of the file name for some reason, my logic breaks.

I have taken the liberty to crosspost this question on other forums as well. Any help would be greatly appreciated!



Solution 1:[1]

Or.. you can just:

Clipboard:= "-Classes Essay on Hobbes"
Gosub % subroutine := SubStr( Clipboard, 1, InStr( Clipboard, " " )-1 )

Clipboard:= "-diary diary example"
Gosub % subroutine := SubStr( Clipboard, 1, InStr( Clipboard, " " )-1 )
Return

-diary:
    MsgBox % "Diary:`n"  StrReplace( clipboard, subroutine " " ) 
    ;Create file in /Diary/Entry 27.MD
Return

-Classes:
    MsgBox % "Classes:`n"  StrReplace( clipboard, subroutine " " )
    ;Create file in /Classes/Essay on Hobbes.MD
Return

Solution 2:[2]

You might try StrSplit()

; Array := StrSplit(String , Delimiters, OmitChars, MaxParts)
clip_array := StrSplit(Clipboard, A_Space, , 1)

if clip_array[1] = "-diary"{
    ;Create file in /Diary/Entry 27.MD
}

Else if clip_array[1] = "-Classes"{
    ;Create file in /Classes/Essay on Hobbes.MD
}

Else {
    MsgBox % "No match for " clip_array[1]
}

Described here https://www.autohotkey.com/docs/commands/StrSplit.htm and note the param for MaxParts which addresses how many "tokens" to look in to (keeping the rest in the last) if the clipboard is sufficiently large to matter (in which case I'm not sure any of the other answers address any better).

Solution 3:[3]

If the amount of text on the clipboard is enormous, that could cause lagging with the script loading all of that text into the script memory, just to check the first value. I'd recommend regexMatch since that can return a position and capture the word as well without parsing everything.

; for reference of regexMatch function
; regexMatch(Haystack, Needle, outputVar, startPos)

Fnd := regexMatch(Clipboard, "U)(\-[\w]+))", capSubStr, inStr(Clipboard, "-"))
if (Fnd==0) {
    Msgbox, Didn't find a word on the Clipboard.
    Return
}
checkVal := capSubStr1
if (checkVal=="-diary") {
    ;Create file in /Diary/Entry 27.MD
} Else if (checkVal=="-Classes") {
    ;Create file in /Classes/Essay on Hobbes.MD
}

Here is the basic reference for using regular expressions with AHK: https://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm

That will give an idea as to what the above expression is actually looking for, if you are not already familiar with regex.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dieisson Silva dos Santos
Solution 2
Solution 3