Autohotkey: how to determine if a text file is opened in emac or notepad3 - autohotkey

I need to determine the path and file name of an open text file, whether it is opened in Notepad3 or in Emacs.
For both editors the path and file name are displayed in the window title of the opened file. Hence, I can use the AHK function WinGetTitle to extract the window title, and then the function SubStr to extract the path and file name from the window title.
However, the two editors differ in how the path and filename are displayed in the window title. In Notepad3 the title format is [filename][path], while in Emacs (my configuration) the format is [path][filename].
I would like my code to be usable regardless which of the two editors the text file is opened in. I guess I have to use some sort of an if-statement.
My pseudo code looks like this:
::uuu::
; find the window title of the opened txt file
WinGetTitle, windowTitle, A
; determine if text file is opened in Notepad3 or in Emacs
appName = <some function>
; construct file name and file path from window title
if appName = notepad3
fileName := SubStr(windowTitle,3,24)
filePath := SubStr(windowTitle,29,-12)
elseif appName = emacs
fileName := SubStr(windowTitle,-23)
filePath := SubStr(windowTitle,1,-24)
end
; send input to file
SendInput, %windowTitle% {enter}
SendInput, %fileName% {enter}
SendInput, %filePath% {enter}
return
1) what AHK function can be used to determine appName?
2) if appName can be determined, how should the if-statement in my code be made in correct AHK syntax?

I think I figured out one way to solve my own question:
::uuu::
; find the window title of the opened txt file
WinGetTitle, windowTitle, A
; find the process or app used to open the text file
WinGet, process, ProcessName, A
; construct file name and file path from window title
if (process = "Notepad3.exe")
{
fileName := SubStr(windowTitle,3,24)
filePath := SubStr(windowTitle,29,-12)
}
else if (process = "emacs.exe")
{
fileName := SubStr(windowTitle,-23)
filePath := SubStr(windowTitle,1,-24)
}
; send input to file
SendInput, %windowTitle% {enter}
SendInput, %fileName% {enter}
SendInput, %filePath% {enter}
SendInput, %process% {enter}
return

Related

How can I use Autokey to grab highlighted text + url and then insert said highlighted text + url in the placeholders of a phrase?

I'm trying to get Autokey to work like Autohotkey worked for me in Windows.
One very useful function that was possible to set up in Autohotkey was assigning a keyboard key to grab highlighted text, then go to url, grab that url, and then insert the highlighted text and URL in specific places within a predetermined phrase.
It was extremely useful to create text with links in different formats.
The Autohotkey script for what I'm describing looked something like this:
insert::
clipboard =
Send, ^c
ClipWait
myText = %clipboard%
Send, !d
clipboard =
Send, ^c
ClipWait
myURL = %clipboard%
myLink = %myText%
clipboard = %myLink%
return
Is there a way to translate that into a working Autokey script?
# assigning a keyboard key to
# `hotkey - a key or combination of keys that, when pressed, will trigger AutoKey to do something; run a script, insert a phrase or display a menu. A hotkey can be created using any key on the keyboard (within reason), with or without one or more modifier keys. The modifier keys are Shift, Control, Alt and Super (a.k.a. Windows).`
# [Guide](https://github.com/autokey/autokey/wiki/Beginners-Guide)
import os, time
# grab highlighted text
myText = clipboard.get_selection()
# then go to url
myCmd = "/usr/bin/firefox --new-window http://www. your site .biz/"
os.system(myCmd)
time.sleep(0.25)
# grab that url, and then
keyboard.send_keys('<F6>')
time.sleep(0.25)
myURL = clipboard.get_selection()
# insert the highlighted text and URL in specific places within a predetermined phrase.
# run a window wait, then paste the texts there. Following example opens a msgbox2sec.ahk (create it first):
myCmd = 'wine ~/.wine/drive_c/Program\ Files/AutoHotkey/AutoHotkey.exe /home/administrator/Desktop/msgbox2sec.ahk'
os.system(myCmd)
time.sleep(0.25)
active_class = window.get_active_class()
if not active_class == "your class name": # for example: "autokey-gtk.Autokey-gtk":
time.sleep(0.25) # sleep longer
myLink = "" + myText + ""
# paste your link, then copy it:
keyboard.send_keys(myLink)
# select select a line:
keyboard.send_keys('<home>')
keyboard.send_keys("<shift>+<end>")
# copy line to clipboard:
keyboard.send_keys('<ctrl>+c')
# or copy line to variable:
c = clipboard.get_selection()

Paste the text from clipboard char by char

I'm trying to create a paste function that simulate a write "character by character" inside an input field, but my code doesn't work.
Here is my code:
^+V::
Loop, parse, clipboard, `n, `r
{
SendRaw, %clipboard%
}
return
You're trying too hard. Send command will already send key by key of whatever content you may pass into it.
; Use this in case of delaying each key press.
; SetKeyDelay, Delay
^+V::
SendRaw, %clipboard%
Return
^v::
ClipBucket5 := Clipboard
ClipBucket5 := RegExReplace(ClipBucket5, "\r\n?|\n\r?", "`n")
Loop, parse, ClipBucket5
{
SendRaw, %A_Loopfield%
Sleep, 100
}
return

How to send line by line data from a .txt file using autohotkey?

Dear friends I am trying to send data from a .txt file to some another file by using Loop (read file contents) command in autohotkey. But it is not sending it line by line i.e. it is sending it continuously. As I made a script which is as follows-
F1::
Loop, read, C:\Data.txt
{
Loop, parse, A_LoopReadLine, %A_Tab%
{
Send %A_LoopField%
}
}
In the above example I made F1 a hotkey.
There is a data.txt file in my D: drive. Now I want that when I press F1 key it should send only one line at a time from data.txt file. When I again press F1 key, it should send next line from that file and so on. But it is not doing so. It is sending the data from data.txt file on the trot (continuously) till the end of the file.
Friends kindly suggest me any solution of this problem.
You can load the file-data outside the hotkey, then loop through it inside the hotkey and get the appropriate line. I'm not sure how fast this would be with huge files though.
; Uncomment this to load data from a file
;FileRead, fileData, C:\data.txt
; This is only for testing
fileData =
(LTrim
Line one
Line two
Line three
)
; Init lineIndex to 1
lineIndex := 1
F1::
Loop, Parse, fileData, `n
{
; A_Index holds the current loop-itteration
if (A_Index == lineIndex) {
SendInput, %A_LoopField%
; Increment lineIndex
lineIndex++
break
}
}
return
Esc::ExitApp

How to pass window title to user function in AutoHotKey

I want to pass the window title into a function I wrote in AutoHotKey, is window title WinTitle a string? I have 4 window titles, and I need to pass them to the same function.
Extract(my_window_title) {
; Wake and select the correct window to be in focus
WinWait, my_window_title,
IfWinNotActive, my_window_title, , WinActivate, my_window_title,
WinWaitActive, my_window_title,
; ... do a bunch of things
}
I call the function like this
title1 = "Some title"
Extract(title1)
and I also tried putting % in all the variables
Yes WinTitle is basically a string.
Check out your Autohotkey-folder, there should be a file called "AU3_Spy.exe". Use it to find the window titles.
And as Elliot DeNolf already mentioned, you made some mistakes with variables. You should also take another look at the syntax of IfWInNotActive.
This should work:
Extract(my_window_title) {
; Wake and select the correct window to be in focus
WinWait, %my_window_title%
IfWinNotActive, %my_window_title%
{
WinActivate, %my_window_title%
WinWaitActive, %my_window_title%
}
msgbox, %my_window_title%
; ... do a bunch of things
}
title1 = MyWindowTitle
Extract(title1) ;functions always expect variables, no percent-signs here
There are a few things that look like they are causing an issue in your script.
When assigning a string value and using =, quotes are not needed. If you assign the value using :=, then you need the quotes. These 2 lines are equivalent:
title1 := "Some Title"
title1 = Some Title
Once these values are called via a function ie. Extract(title1), % symbols must be used (as you mentioned at the end of your question). This can be called in 2 ways:
WinActivate, %my_window_title%
WinActivate, % my_window_title
If the title is invalid, your script will wait indefinitely on WinWait and WinWaitActive. I would recommend using a timeout value and then checking ErrorLevel to see if it was successful or not.

Autohotkey to put the selected text in smart quotes

I need an Autohotkey script that puts the selected text in these opening and closing quotation marks:
http://www.fileformat.info/info/unicode/char/201e/index.htm - for the opening quote
and
http://www.fileformat.info/info/unicode/char/201c/index.htm - for the closing quote
So, I can select a text, press the hotkey and the text is quotes like this:
„test text“
Thank you in advance!
f1::
saved := clipboardall ; save clipboard contents
Send, ^x ; cut
Send, „%clipboard%“ ; send clipboard content with your characters around it
clipboard := saved ; restore clipboard
saved := "" ; clear saved
Return
If that doesn't work you can put the code in brackets:
f2::
saved := clipboardall
Send, ^x
Send, {U+201E}%clipboard%{U+201C}
clipboard := saved
saved := ""
Return