IMG2HotString

Post your working scripts, libraries and tools for AHK v1.1 and older
off
Posts: 176
Joined: 18 Nov 2022, 21:54

IMG2HotString

Post by off » 23 Mar 2023, 09:12

I think this script is useful enough for someone who need it, so ill just post it here.

This script put your image file to .clip and can be accessible with Hotstring.
Press Win+Alt+C after selecting image file in file explorer, a GUI will appear, put your hotstring there (inspirations taken from Hotstring helper) and press Confirm.
This will append your hotstring to the current script.

Current extension supported:
  • PNG
    JPG
    JPEG
    JFIF
    ICO
Script:

Code: Select all

#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%
SetBatchLines -1

#!c:: ; Win+Alt+C
; select file and copy automatically.
SaveClip:=Clipboard
Clipboard:=
Send, ^{c}
:*:.copy::
; you need to copy the file first to use this hotstring.
If !(FileExist("Saved Image Clip"))
{
FileCreateDir, Saved Image Clip
}
;MsgBox, %Clipboard%
;If !(InStr(Clipboard, ".png") or InStr(Clipboard, ".jpg") or InStr(Clipboard, ".jpeg") or InStr(Clipboard, ".jfif") or InStr(Clipboard, ".ico"))
SplitPath, Clipboard,,, FileExt
;MsgBox, %FileExt%
If !(InStr(FileExt, "png") or InStr(FileExt, "jpg") or InStr(FileExt, "jpeg") or InStr(Clipboard, "jfif") or InStr(Clipboard, "ico"))
{
MsgBox, The selected file doesn't contain png/jpg/jpeg/jfif/ico
Return
}
Else
if (InStr(FileExist(Clipboard), "D"))
{
Return
}
Else
Gui, Destroy
Gui, +AlwaysOnTop +OwnDialogs +ToolWindow
Gui Font, s10
Gui Add, Edit, vhsOpt x15 y32 w45 h25 -VScroll, *
Gui Add, Text, x6 y8 w105 h23 +0x200, Hotstring Options
Gui Add, Edit, gupdPreview vhsTrigger x116 y32 w176 h25 -VScroll
Gui Add, Text, x117 y8 w120 h23 +0x200, Trigger String
Gui Add, Edit, gupdPreview vhsPreview x15 y63 w277 h21 +ReadOnly
Gui Add, Button, ghsOptHint x62 y31 w37 h27, ?
Gui Add, Picture, vhsImgPreview x8 y96 w425 h-1, %Clipboard%
Gui Add, Button, ghsConfirm x295 y32 w70 h26, Confirm
Gui Add, Button, ghsHelp x295 y60 w70 h26 Disabled, Help
Gui Add, Button, ghsEdit x366 y32 w70 h26, Edit Script
Gui Add, Button, ghsClose x366 y60 w70 h26, Close
Gui Show, x50 y50, IMG2HS GUI

GoSub, updPreview
TotalFile=1
Loop, Files, %A_ScriptDir%\Saved Image Clip\*.*
{
TotalFile=% A_Index + 1
;MsgBox, %TotalFile% %A_Index%
}
Return

hsConfirm:
FileAppend, 
(

; === Image File %TotalFile% ===
:%hsOpt%:%hsTrigger%::
SaveClip:=ClipboardAll
Clipboard:=
FileRead, Clipboard, *c Saved Image Clip\img_%TotalFile%.clip
ClipWait, 2
Send, ^{v}
Clipboard:=SaveClip
Return

), %A_ScriptFullPath%
FileAppend, %ClipboardAll%, Saved Image Clip\img_%TotalFile%.clip
Sleep, 100
Clipboard:=
Clipboard:=SaveClip
Reload
return

hsEdit:
Run, Notepad.exe %A_ScriptFullPath%
WinWait, %A_ScriptName%
WinActivate, %A_ScriptName%
WinWaitActive, %A_Scriptname%
Send, ^{End}
Clipboard:=
Clipboard:=SaveClip
Return

hsHelp:
Return

updPreview:
Gui, Submit, NoHide
;MsgBox, %hsOpt% %hsTrigger%
GuiControl,, hsPreview, :%hsOpt%:%hsTrigger%::
Return

hsOptHint:
MsgBox, 
(
* (asterisk): An ending character (e.g. Space, ., or Enter) is not required to trigger the hotstring.

? (question mark): The hotstring will be triggered even when it is inside another word; that is,
when the character typed immediately before it is alphanumeric. Use ?0 to turn this option back off.

B0 (B followed by a zero): Automatic backspacing is not done to erase the abbreviation you type.
Use a plain B to turn backspacing back on after it was previously turned off.

C: Case sensitive: When you type an abbreviation, it must exactly match the case defined in the script.
Use C0 to turn case sensitivity back off.

C1: Do not conform to typed case. Use this option to make auto-replace hotstrings case insensitive
and prevent them from conforming to the case of the characters you actually type.
C0 can be used to turn this option back off, which makes hotstrings conform again.

Kn: Key-delay: This rarely-used option sets the delay between keystrokes produced by auto-backspacing or auto-replacement.
Specify the new delay for n; for example, specify k10 to have a 10ms delay and k-1 to have no delay.

•SI (SendInput): Key-delay is ignored because a delay is not possible in this mode.
•SP (SendPlay): A delay of length zero is the default, which for SendPlay is the same as -1 (no delay).
•SE (SendEvent): A delay of length zero is the default.

O: Omit the ending character of auto-replace hotstrings when the replacement is produced.
Use O0 (the letter O followed by a zero) to turn this option back off.

Pn: The priority of the hotstring (e.g. P1). This rarely-used option has no effect on auto-replace hotstrings.

R: Send the replacement text raw; that is, without translating {Enter} to Enter, ^c to Ctrl+C, etc.
Use R0 to turn this option back off.

T [v1.1.27+]: Send the replacement text raw, without translating each character to a keystroke.
Use T0 or R0 to turn this option back off, or override it with R.

X [v1.1.28+]: Execute. Instead of replacement text, the hotstring accepts a command or expression to execute.

Z: This rarely-used option resets the hotstring recognizer after each triggering of the hotstring. 
)
Return

hsClose:
GuiClose:
Gui Destroy
Clipboard:=
Clipboard:=SaveClip
Return

; === ALL THE HOTSTRING ===



GUI Appearance
image.png
image.png (25.24 KiB) Viewed 820 times

Edit : Fixed wrong var in FileAppend
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: IMG2HotString

Post by off » 23 Mar 2023, 20:56

Update 24/3/2023

Code:

Code: Select all

#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%
SetBatchLines -1

#!c:: ; Win+Alt+C
; select file and copy automatically.
SaveClip:=Clipboard
Clipboard:=
Send, ^{c}
:*:.copy::
; you need to copy the file first to use this hotstring.
If !(FileExist("Saved Image Clip"))
{
FileCreateDir, Saved Image Clip
}
;MsgBox, %Clipboard%
;If !(InStr(Clipboard, ".png") or InStr(Clipboard, ".jpg") or InStr(Clipboard, ".jpeg") or InStr(Clipboard, ".jfif") or InStr(Clipboard, ".ico"))
SplitPath, Clipboard,,, FileExt
;MsgBox, %FileExt%
If !(InStr(FileExt, "png") or InStr(FileExt, "jpg") or InStr(FileExt, "jpeg") or InStr(Clipboard, "jfif") or InStr(Clipboard, "ico"))
{
MsgBox, The selected file doesn't contain png/jpg/jpeg/jfif/ico
Return
}
Else
if (InStr(FileExist(Clipboard), "D"))
{
Return
}
Else
Gui, Destroy
Gui, +AlwaysOnTop +OwnDialogs +ToolWindow
Gui Font, s10
Gui Add, Edit, gupdPreview vhsOpt x15 y32 w45 h25 -VScroll, *
Gui Add, Text, x6 y8 w105 h23 +0x200, Hotstring Options
Gui Add, Edit, gupdPreview vhsTrigger x116 y32 w176 h25 -VScroll
Gui Add, Text, x117 y8 w120 h23 +0x200, Trigger String
Gui Add, Edit, vhsPreview x15 y63 w277 h21 +ReadOnly
Gui Add, Button, ghsOptHint x62 y31 w37 h27, ?
Gui Add, Picture, vhsImgPreview x8 y219 w425 h-1, %Clipboard%
Gui Add, Button, ghsConfirm x295 y32 w70 h26, Confirm
Gui Add, Button, ghsHelp x295 y60 w70 h26 Disabled, Help
Gui Add, Button, ghsEdit x366 y32 w70 h26, Edit Script
Gui Add, Button, ghsClose x366 y60 w70 h26, Close
Gui Add, Text, x8 y96 w425 h23 +0x200, Add Hotstring Comment
Gui Add, Edit, gupdPreview vhsImgComment x8 y119 w425 h100
Gui Show, x50 y50, IMG2HS GUI

GoSub, updPreview
TotalFile=1
Loop, Files, %A_ScriptDir%\Saved Image Clip\*.*
{
TotalFile=% A_Index + 1
;MsgBox, %TotalFile% %A_Index%
}
Return

hsConfirm:
hsFixImgComment:=StrReplace(hsImgComment, "!", "`!")
hsFixImgComment:=StrReplace(hsImgComment, "[", "`[")
hsFixImgComment:=StrReplace(hsImgComment, "]", "`]")
hsFixImgComment:=StrReplace(hsImgComment, "\", "`\")
hsFixImgComment:=StrReplace(hsImgComment, "{", "`{")
hsFixImgComment:=StrReplace(hsImgComment, "}", "`}")
hsFixImgComment:=StrReplace(hsImgComment, """, "`"")
hsFixImgComment:=StrReplace(hsImgComment, "'", "`'")
hsFixImgComment:=StrReplace(hsImgComment, ":", "`:")
hsFixImgComment:=StrReplace(hsImgComment, ";", "`;")
hsFixImgComment:=StrReplace(hsImgComment, "<", "`<")
hsFixImgComment:=StrReplace(hsImgComment, ">", "`>")
hsFixImgComment:=StrReplace(hsImgComment, ",", "`,")
hsFixImgComment:=StrReplace(hsImgComment, ".", "`.")
hsFixImgComment:=StrReplace(hsImgComment, "/", "`/")
hsFixImgComment:=StrReplace(hsImgComment, "?", "`?")
hsFixImgComment:=StrReplace(hsImgComment, "(", "`(")
hsFixImgComment:=StrReplace(hsImgComment, ")", "`)")
hsFixImgComment:=StrReplace(hsImgComment, "@", "`@")
hsFixImgComment:=StrReplace(hsImgComment, "#", "`#")
hsFixImgComment:=StrReplace(hsImgComment, "$", "`$")
hsFixImgComment:=StrReplace(hsImgComment, "%", "`%")
hsFixImgComment:=StrReplace(hsImgComment, "^", "`^")
hsFixImgComment:=StrReplace(hsImgComment, "&", "`&")
hsFixImgComment:=StrReplace(hsImgComment, "*", "`*")
hsFixImgComment:=StrReplace(hsImgComment, "=", "`=")
hsFixImgComment:=StrReplace(hsImgComment, "+", "`+")
hsFixImgComment:=StrReplace(hsImgComment, "-", "`-")
hsFixImgComment:=StrReplace(hsImgComment, "`", "``")
hsFixImgComment:=StrReplace(hsImgComment, "~", "`~")
hsFixImgComment:=StrReplace(hsImgComment, "|", "`|")
hsFixImgComment:=StrReplace(StrReplace(hsImgComment, "`r`n", "+`{enter`}"), "`n", "+`{enter`}")
FileAppend, 
(

; === Image File %TotalFile% ===
:%hsOpt%:%hsTrigger%::
SaveClip:=ClipboardAll
Clipboard:=
FileRead, Clipboard, *c Saved Image Clip\img_%TotalFile%.clip
ClipWait, 2
Send, ^{v}
Sleep, 300
SendInput, %hsFixImgComment%
Clipboard:=SaveClip
Return

), %A_ScriptFullPath%
FileAppend, %ClipboardAll%, Saved Image Clip\img_%TotalFile%.clip
Sleep, 100
Clipboard:=
Clipboard:=SaveClip
Reload
return

hsEdit:
Run, Notepad.exe %A_ScriptFullPath%
WinWait, %A_ScriptName%
WinActivate, %A_ScriptName%
WinWaitActive, %A_Scriptname%
Send, ^{End}
Clipboard:=
Clipboard:=SaveClip
Return

hsHelp:
Return

updPreview:
Gui, Submit, NoHide
;MsgBox, %hsOpt% %hsTrigger%
GuiControl,, hsPreview, :%hsOpt%:%hsTrigger%::
Return

hsOptHint:
MsgBox, 
(
* (asterisk): An ending character (e.g. Space, ., or Enter) is not required to trigger the hotstring.

? (question mark): The hotstring will be triggered even when it is inside another word; that is,
when the character typed immediately before it is alphanumeric. Use ?0 to turn this option back off.

B0 (B followed by a zero): Automatic backspacing is not done to erase the abbreviation you type.
Use a plain B to turn backspacing back on after it was previously turned off.

C: Case sensitive: When you type an abbreviation, it must exactly match the case defined in the script.
Use C0 to turn case sensitivity back off.

C1: Do not conform to typed case. Use this option to make auto-replace hotstrings case insensitive
and prevent them from conforming to the case of the characters you actually type.
C0 can be used to turn this option back off, which makes hotstrings conform again.

Kn: Key-delay: This rarely-used option sets the delay between keystrokes produced by auto-backspacing or auto-replacement.
Specify the new delay for n; for example, specify k10 to have a 10ms delay and k-1 to have no delay.

•SI (SendInput): Key-delay is ignored because a delay is not possible in this mode.
•SP (SendPlay): A delay of length zero is the default, which for SendPlay is the same as -1 (no delay).
•SE (SendEvent): A delay of length zero is the default.

O: Omit the ending character of auto-replace hotstrings when the replacement is produced.
Use O0 (the letter O followed by a zero) to turn this option back off.

Pn: The priority of the hotstring (e.g. P1). This rarely-used option has no effect on auto-replace hotstrings.

R: Send the replacement text raw; that is, without translating {Enter} to Enter, ^c to Ctrl+C, etc.
Use R0 to turn this option back off.

T [v1.1.27+]: Send the replacement text raw, without translating each character to a keystroke.
Use T0 or R0 to turn this option back off, or override it with R.

X [v1.1.28+]: Execute. Instead of replacement text, the hotstring accepts a command or expression to execute.

Z: This rarely-used option resets the hotstring recognizer after each triggering of the hotstring. 
)
Return

hsClose:
GuiClose:
Gui Destroy
Clipboard:=
Clipboard:=SaveClip
Return

; === ALL THE HOTSTRING ===


Changes :
+ Added image comment option
image.png
image.png (29.7 KiB) Viewed 775 times
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Re: IMG2HotString

Post by kunkel321 » 24 Mar 2023, 10:44

Hey this is awesome! It seems to work well. Here are my two sample items that got auto-generated by the script:

Code: Select all

; === Image File 1 ===
::steveimg::
SaveClip:=ClipboardAll
Clipboard:=
FileRead, Clipboard, *c Saved Image Clip\img_1.clip
ClipWait, 2
Send, ^{v}
Sleep, 300
SendInput, Steve's image
Clipboard:=SaveClip
Return

; === Image File 2 ===
::Steve's image2::
SaveClip:=ClipboardAll
Clipboard:=
FileRead, Clipboard, *c Saved Image Clip\img_2.clip
ClipWait, 2
Send, ^{v}
Sleep, 300
SendInput, Second one
Clipboard:=SaveClip
Return
I don't understand how the FileRead line works though (??) It seems to be reading from *c Saved Image Clip\img_2.clip, but that doesn't seem to be an actual location... How does it work?

EDIT: Actually I see the subfolder now. It's in the same parent folder as the .ahk script (which makes sense). Still, the syntax you used is new to me -- cool stuff! :ugeek:
ste(phen|ve) kunkel

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: IMG2HotString

Post by off » 26 Mar 2023, 21:38

@kunkel321
Thanks, I hope this useful for you!
also i might need to credit you for the HotString idea, would you like it?
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Re: IMG2HotString

Post by kunkel321 » 27 Mar 2023, 08:40

No need to credit me... Especially since I got the idea from someone else! :)
ste(phen|ve) kunkel

Post Reply

Return to “Scripts and Functions (v1)”