My experience porting AHK scripts to Linux.

Discuss other useful utilities, general computing tips & tricks, Internet resources, etc.
A_Perry_1984
Posts: 52
Joined: 07 Dec 2018, 12:08

My experience porting AHK scripts to Linux.

19 Jun 2019, 13:11

I recently switched my OS to Linux and wanted to share my thoughts on trying to adapt AHK scripts for general use. For the sake of simplification when I am using Linux to describe the many distributions (aka distros). I am also going to preface this by saying I am not that technical. I don't really program. I just tinker.

Now I've read a lot of people on this forum in the past have wanted to make the switch to Linux but have been dissuaded from doing so out of fear of not being able to port scripts. I completely understand this. I am a long time Windows user and hardcore AHK user. I also understand that most long time Linux users just don't get it. Many of them are fan-boys/fan-girls who think that Linux is superior at just about everything. And for the most part, I agree. However, these Linux fans have likely have limited experience with Windows, let alone AHK with Windows. They often suggest different packages (programs) for various tasks, and many of these tools are great, but what they don't get is what makes AHK special. It's simple, self contained and does everything like the one ring.

The good news is, I have found it's quite possible to port AHK to Linux and do a lot with it using Wine Tools. There are limitations so I want share in detail how I have worked around them instead of parroting the Linux crowd. Maybe some of you who are on the fence, not wanting to give up AHK like I was for so long. I also want to make it clear that anything you can't do with AHK on Linux can be done in other ways quickly and easily.

Hotkeys

Probably the biggest limitation I have experienced so far in using AHK with Linux is hotkeys themselves. Hotkeys are my life and the thought of not being able to use them exactly the way I want to, (that and my ignorance), held me back from switching to Linux a long time ago. The good news is, just about any Linux desktop environment is already equipped with an extremely intuitive keyboard manager by default. On most Linux desktops it's called the, "Keyboard Manager", and/or Windows Manager". Any common task that you could run with AHK using hotkeys can easily be done with the OS, and you won't even have to go near the console. You can run files, launch programs, open, close, arrange windows, etc... You just have to tell it what you want from the Keyboard and/or Window Manager. In some cases, you might have to use specific commands such as XDG-Open, or EXO-Open. You'll see a lot of these commands in the default hotkeys, but if you need to find out more, just look it up on Duckduckgo.com.

Hotstrings

Bash Scripts
In my limited time using AHK with Linux, I haven't found hotstrings to work, but there are very simple work around's for this. I am almost certain you can program the Linux Keyboard manager with hotstrings but if not, I think they can be done using bash scripts, (Shell type scripts) to do things like that.

Autokey
If you don't want to go that route, there are also some very simple programs I've played with made just for hotstrings. One program is called Autokey. This is a very basic Python editor which comes with simple hotstring, and hotkey scripts to get you on your way. If you know Python 2.7, you can take it a lot further but it's not really necessary if you're just wanting to fill out forms.

Xdotools
If you want something easier, there is, Xdotools. You can use it directly from the console, but it also acts as a sort of plugin to other programs, one being the Keyboard Manager. I have already used it to simulate key presses. I read somewhere that you can adapt it for AHK, but I haven't tried it yet. There is even a program written called AutoHotKeyX which I haven't tried yet.

There are several other programs in addition to these that I haven't even tried.

GUI's

I have managed to port all my GUI scripts, even complicated ones to AHK and they seem to work flawlessly. The few problems I have had with scripts that had GUI's were not a result of the GUI itself but certain functions that are only specific to Windows.

File/Folder Handling

A bit about Wine first...
In simple terms Linux seems to work differently then Windows. In order to make Windows programs like AHK play nice in Linux, Wine creates a pseudo Windows sandbox for your programs to stay. This is just like an emulator except it's not. In other words it tricks Windows programs into thinking they are running on Windows.

One major aspect of making Windows programs work is resolving connecting drives. Windows uses drive letters to designate file paths, such as C:\, or C:\Programs\. Linux doesn't assign drive letters. In Linux your drive might look something like this /Home/Username/. When you install Wine, it will auto detect your Linux drives and assign them their own Windows drive letters. So your Linux home folder such as /Home/Username/ might be assigned to I:\ in the Wine environment. You don't have to use the auto settings in Wine. You can also tell it which drive letters to use. At home I use drive I have assigned a USB drive as A:\ in Wine

The good news about this is you can assign file paths with ease in AHK. Lets say for example I want to copy a file on my USB drive to my hard drive. With AHK on Linux I can still use Windows paths,

Code: Select all

Filecopy, A:\My File Name, C:\New File Name
.

But I can also use Linux Paths,

Code: Select all

Filecopy, /...Media/My File Name, /Home.../New File Name
Even better AHK is still able to use it's built in variables,A_ScriptDir, A_WorkingDir etc.
Since file/folder reading doesn't seem to be a problem, I can just as easily work with loops.

Window Handling

From my limited understanding, AHK works with Windows within Wine only. It has nothing to do with much outside that environment. It won't have any affect on Linux windows that I'm aware of. Using AHK, you can open and interact with a Wine GUI File manager, but this is very limited and doesn't offer much versatility.

I haven't tried image searching but, I am wanting to say no, as I think these are specific to Windows OS. However there are other programs which can handle image searches.

Mouse Movements

I don't think this is possible directly with AHK but I have considered using bash scripts or other programs, and launching them with AHK.

Running Programs

Some programs work and others don't from what I can tell. I have been able to run website links and have it start in my native Browser. I can open files like spreadsheets, docs, and text using Libre Office which is default in most Linux distros. As long as it's a file it should open. I haven't played around much when it comes to launching programs specific to Linux. I think it depends on the context.

Variables, Functions, Objects

I have no problem storing and using variables. I haven't played around with the clipboard, just yet. Objects work as long as their not OS related. I can write and use functions just fine as long as they don't affect the OS itself.

Context Sensitive

I haven't had much need for this personally but from what I have read it isn't possible. Again you can't really interact directly with the OS which handles a lot of this s

Com

I am pretty sure Com is specific to Windows only. However I think you can install Selenium in Linux for Dom (Document Object Model) management, that can be used with Java Script. I think you can then use AHK with programs like Libre Office (Spreadsheet program), and most web browsers.

Processes

I don't know how processes work in Linux, but I haven't played around with them yet.

Editing Scripts

I have tried but failed to install Scite4Autohotkey, and AHK studio with Wine. I am sure it's a simple fix but I haven't figured it out. You can also install the Linux version of Notepad++ but I haven't figured out how to install the AHK language files into it. For now I am just using a basic text editor without autocomplete and syntax highlighting.

I have managed to do a lot in AHK or am comfortable enough to figure out other ways to do what I need. Who knows, maybe I can do more a lot more in AHK than I am aware of.

I know for some of you, the idea of setting all this up and to find all those work arounds while working inside a new OS seems quite daunting, but let me assure you, it's really not that difficult. You just have to take one step at a time and adjust a few things and you'll be up and running before you know it. This is coming from a not so technical person.

And all those people who say you need learn Python, or get good at the console to automate, no, you don't. I would add this however, bash scripts allow you to do some pretty amazing things. I wrote a five line script to clean and organize my hard drive and files, which I can launch from AHK. In my very short time with Linux as in less than a week, I am beginning to see why people say you don't really need AHK. In getting a feel for the OS, I find it's very intuitive. It's practically made for automation. So combine that with the fact that you can use a lot of AHK functionality, your not losing a tool but gaining a great tool..

I would say to anyone here, play around in a virtual box and see what you can and can't do with AHK. But also play around with other tools. Once you start getting the hang of it, it will be a lot easier to leave Windows. And if you still need AHK to work 100%, you can always install a virtual box in Linux and run AHK on Windows.

Now I am well aware of the dead IronAHK project, but I suspect it was dropped due to the advantages of using AHK for Windows on Wine. I would also add the fact that so many people were saying AHK isn't necessary or that Python is just as good. But here is the thing, if enough people who use AHK switch to Linux and want AHK, I don't see why it can't be done by the community. Autohotkey is an open source dream and doesn't have to live only on Windows.
User avatar
davebrny
Posts: 83
Joined: 05 Dec 2016, 06:26

Re: My experience porting AHK scripts to Linux.

27 Jun 2019, 17:20

great post. i been messing around with linux every now and again for the last year but it was only the last week that i sat down and started seeing how much of autohotkey works with wine. im actually quite surprised by how many things work... fileread, iniRead/Write, menu, mouseMove, msgBox, toolTip, trayTip. the clipboard is working like normal as far as i can tell, and functions can be called from all 3 of the library folders

some of those dont work completely though. menu's dont respond to key presses so you cant do letter shortcuts to select a certain item, which is a bummer because i use them a lot. msgBoxs dont come to the front so you have to go looking for them. iniWrite ends up removing any of the empty lines in the file, which is a deal breaker if you like using ini sections inside of your scripts. mouseMove seems dependant on where the wine tray window is but maybe if it works in window mode inside a program it might be of more use.

theres a lot of other things i havnt got around to testing out yet though so im still trying to figure out whether ill have any use for autohotkey on linux. but maybe it would be a good thing in the end as it will force me to learn how to use things like bash scripts and some of the other tools. ive already made a small script in AutoKey that will run things in wine and im actually liking the way python does certain things like substrings and replace. its definitely a lot more elegant. not needing brackets around everything is a plus too.

another handy thing you can do with AutoKey is have it focus on the wine tray and send a hotkey that will trigger Autohotkey. so basically you can run something even when wine isnt focused which makes it feel a little less disconnected from linux

Code: Select all

window.activate("Wine System Tray")
keyboard.send_keys("super>+<shift>+a")

have you ever tried sublime text? i copied over the portable version i was using on windows and apart from some weird looking UI text in some parts it seems to be mostly working. all my snippets/autocomplete works, you can still add project folders, the autohotkey plugin is able to run and compile ok but the piped buffer option doesnt work. the only real let down is that winMenuSelect doesnt work so that messes up my workflow a bit. i will have to manually remap a lot of hotkeys in sublimes's settings, or maybe ill just install the linux version if i can just swap in the windows folder with all the settings.
A_Perry_1984 wrote:
19 Jun 2019, 13:11
but what they don't get is what makes AHK special. It's simple, self contained and does everything like the one ring.
yea that self contained part is really such an underrated feature you dont hear many people talking about. when i upgrade a computer or reset windows, all i have to do is copy back over my autohotkey folder and then re-associate the .ahk files with the portable .exe (thats in the same folder) and im up and running again! i havnt found anything else that comes close to that kind of ease of use. looking at all the different tools that im going to have to install and learn in linux to get the same functionality, im starting to see how spoilt i am with autohotkey
tomjuggler
Posts: 1
Joined: 04 Dec 2014, 16:19

Re: My experience porting AHK scripts to Linux.

02 Oct 2019, 14:45

Thank you so much for the post here. I am a long time Linux only user but my first programming was done in Autohotkey many many years ago. I really missed it on Linux, and you are quite right, there is nothing as easy as ahk available. Thanks to your post I just ran my first msgbox, hello in probably 6 years!! (ok some ancient scripts are still plugging away on my wife's windows work laptop, automating the office forever more..)

I think the ahkx project by Naveen Garg (shared 10 years ago!) has some great potential, if I can only get it to compile and run for me.. It seems he has written a compatibility layer, so ahk mousemove will call the equivalent xdotool command. Also there is a program called xkeyhook which passes keyboard events over to the running ahk script, so that would suggest that hotkeys and strings will also work. This approach is probably the easiest - other functionality could be added on (I like wmctrl in linux - window manager control, can move, resize, focus etc windows)

Well I am excited now. Thanks again. I am definitely going to be testing out AHK on Linux some more now.
burque505
Posts: 1061
Joined: 22 Jan 2017, 19:37

Re: My experience porting AHK scripts to Linux.

02 Oct 2019, 17:20

@davebrny, @tomjuggler, I'd be really interested to know more about your wine experiences. I also tried the ahkx project, and ran into stumbling block after stumbling block when compiling it on Mint 17. When I finally got it to compile, the executable threw errors. On the WineDB page one user reported that "everything works", but I certainly couldn't duplicate those results.
User avatar
Drugwash
Posts: 737
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

Re: My experience porting AHK scripts to Linux.

14 Oct 2019, 11:17

I've been running some of my AHK scripts under Linux Mint+Wine too for the last few months and stumbled into issues. Don't care much about hotkeys or hotstrings - I'm mostly a GUI person. There are bugs in Wine, some very old, that may never get fixed, such as text in themed buttons/checkboxes/radio-buttons being top-aligned instead of vertically centered. Worst bug of all is the alpha channel in images being completely disregarded, leading to ugly image rendering.

However there are others, such as the inability to "skin" a Toolbar/Rebar control (or a corrupt appearance of them in other applications), black artefacts in Tab controls, various issues with RichEdit controls and so on.

Some of these issues can sometimes be fixed by using native Windows libraries such as comctl32.dll, gdiplus.dll, msls32.dll, riched20.dll, riched32.dll, usp10.dll but it has to be done individually on an application by application basis. WineTricks can help installing such native libraries but things can also get messed up and I haven't found a way to uninstall those components.

Also Wine does not accurately follow the behavior of the original libraries according to the OS version set for a given application, so any script/application relying on certain OS-dependent features (or bugs) will fail.

All in all, Wine is still very far from a complete and accurate Windows environment under Linux, so there will almost always be only partial success at running AHK scripts - or any other kind of native Windows applications - under Linux.
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.
User avatar
SL5
Posts: 862
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: My experience porting AHK scripts to Linux.

14 Nov 2019, 09:59

i just installed it. first time :) WinGetActiveTitle and clipboard works. thanks for your postings.
may this helps a bit: https://github.com/baskerville/sxhkd

Return to “Other Utilities & Resources”

Who is online

Users browsing this forum: No registered users and 15 guests