Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to Handle Selected Text and Error Prevention #8

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions AI-Tools.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,20 @@ PromptHandler(promptName, append := false) {
global _running := true
global _startTime := A_TickCount

ShowWaitTooltip()
SetSystemCursor(GetSetting("settings", "cursor_wait_file", "wait"))

prompt := GetSetting(promptName, "prompt")
promptEnd := GetSetting(promptName, "prompt_end")
mode := GetSetting(promptName, "mode", GetSetting("settings", "default_mode"))
input := GetTextFromClip()

try {
input := GetTextFromClip()
} catch {
global _running := false
RestoreCursor()
return
}

ShowWaitTooltip()
SetSystemCursor(GetSetting("settings", "cursor_wait_file", "wait"))
CallAPI(mode, promptName, prompt, input, promptEnd)

} catch as err {
Expand All @@ -93,16 +99,20 @@ PromptHandler(promptName, append := false) {
;###

SelectText() {
if WinActive("ahk_exe WINWORD.EXE") or
WinActive("ahk_exe OUTLOOK.EXE") {
; In Word/Outlook select the current paragraph
Send "^{Up}^+{Down}+{Left}" ; Move to para start, select para, move left to not include para end
} else if WinActive("ahk_exe notepad++.exe") or
WinActive("ahk_exe Code.exe") {
; In Notepad++ select the current line
Send "{End}{End}+{Home}+{Home}"
} else {
Send "^a"
Send "^c"
ClipWait(2)
text := A_Clipboard
if StrLen(text) < 1 {
if WinActive("ahk_exe WINWORD.EXE") or WinActive("ahk_exe OUTLOOK.EXE") {
; In Word/Outlook select the current paragraph
Send "^{Up}^+{Down}+{Left}" ; Move to para start, select para, move left to not include para end
} else if WinActive("ahk_exe notepad++.exe") or WinActive("ahk_exe Code.exe") {
; In Notepad++ select the current line
Send "{End}{End}+{Home}+{Home}"
} else {
; Select all text if no text is selected
Send "^a"
}
}
sleep 50
}
Expand All @@ -118,6 +128,7 @@ GetTextFromClip() {
text := A_Clipboard

if StrLen(text) < 1 {
ShowWarning("No text selected. Please select text and try again.")
throw ValueError("No text selected", -1)
} else if StrLen(text) > 16000 {
throw ValueError("Text is too long", -1)
Expand All @@ -126,6 +137,10 @@ GetTextFromClip() {
return text
}

ShowWarning(message) {
MsgBox message
}

GetSetting(section, key, defaultValue := "") {
global _settingsCache
if (_settingsCache.Has(section . key . defaultValue)) {
Expand Down Expand Up @@ -432,4 +447,4 @@ LogDebug(msg) {
logMsg := "[" . now . "] " . msg . "`n"
FileAppend(logMsg, "./debug.log")
}
}
}