My experience porting AHK scripts to Linux.

Discuss other useful utilities, general computing tips & tricks, Internet resources, etc.
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: My experience porting AHK scripts to Linux.

08 May 2020, 07:55

@mfeemster, you might take a look at the code for sharpAHK by LucidMethod. There may be some code there that makes your work easier.
Regards,
burque505
User avatar
mfeemster
Posts: 104
Joined: 24 Apr 2020, 18:30

Re: My experience porting AHK scripts to Linux.

09 May 2020, 22:25

Thanks, that looks rather interesting, although I'm not exactly sure what the intent is. Am I correct in thinking that it's not doing any type of parsing or running of scripts, but instead, it's taking various functionality that you'd see in scripts (and is implemented in C++ in AHK) and just providing that functionality in C#. Such as send a key, focus a window, etc...

The author, LucidMethod, appears to have worked on this project much more recently, unlike IronAHK which was abandoned years ago. Is LucidMethod on these forums?

If my assessment of his project is as I mentioned above, then perhaps I can use some of his code for executing various scripting functionality. IronAHK already has much of it done, but leaves many parts blank. Perhaps reconciling the two can give me more coverage. Regardless, much work remains.

Once I get to a point where I'm comfortable publishing it, I will make my own thread for it. I'd love to have as many people help out as possible.
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: My experience porting AHK scripts to Linux.

10 May 2020, 08:34

The only place I've seen LucidMethod active is on github, which you already know.

There are two ways to get AHK functionality with sharpAHK, it seems to me. One wraps a version of a wrapper around amazing-andrew's AutoHotkey.Interop, a wrapper of AutoHotkey.dll for interacting with AHK and embedding it in C# code, so you can create an instance of AutoHotkey.Engine - run AHK scripts, get and set variables from an instance, create a pipes handler, etc. The other (see the folder 'AutoHotkey.Interop'\_sharpAHK') can also call an AHK engine (see '_AutoHotkey.cs'), but in addition has (as you rightly suggest) AHK-like functionality in C#. (See e.g. '_Mouse.cs'; '_Clipboard.cs'.)

(I'm pretty fuzzy as to whether any of this C# functionality is in any way dependent on an instance of the Autohotkey engine, but at least some it is apparently not dependent on AHK at all.)

Looking forward to your code, @mfeemster!

Regards,
burque505
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: My experience porting AHK scripts to Linux.

11 May 2020, 02:26

Belated welcome, and good to have you aboard, mfeemster.

I'm a bit confused by the conversation. My understanding is that wrapping the AutoHotkey.dll will not work on Linux, unless the intent is to use WINE. This might not be the path that one wishes to take, as one might want to be independent of WINE.

Also, it appears that IsNull helped out on the original IronAHK, and might still be active. It appears he was involved in the code for Linux too. He made various contributions on GitHub last year. https://github.com/IsNull

The Linux code appears under Rusty, for IronAHK on GitHub
https://github.com/Paris/IronAHK/tree/master/Rusty/Linux
User avatar
mfeemster
Posts: 104
Joined: 24 Apr 2020, 18:30

Re: My experience porting AHK scripts to Linux.

11 May 2020, 15:32

Hi @SOTE , thanks for the welcome.

I have all of the IronAHK code and have mostly made sense of it.

I will check out that IsNull person if I get stuck.

I am indeed trying to make something independent of WINE. My intent is to pick up where IronAHK left off and expand it, while also doing a few things differently. I'll detail the differences in the future after I have something to present.

That said, I am stuck on the concept of "variables" in AHK. Not sure if this is the proper thread to ask it on. If not, let me know where.

Being a senior developer, variables always have a type. However, in AHK, their type seems to be indeterminate. It's sort of a string, but sort of an int, sort of a float, but that one isn't a string, and they might work with COM, but also might not. Ugh!

>>> Although a variable is typically thought of as holding a single value, and that value having a distinct type (string, number or object), AutoHotkey automatically converts between numbers and strings in cases like myString + 1 and MsgBox %myNumber%. As these conversions can happen very frequently, whenever a variable is converted, the result is cached in the variable.

In effect, a variable can contain both a string and a number simultaneously. Usually this just improves the script's performance with no down-sides, but if a variable contains both a number and a string, is it number, or is it a string? This ambiguity causes unexpected behavior in at least two cases:
1.COM objects. In order to pass parameters to a COM object, the program must convert the variable's content to either a number or a string. Some COM objects throw an exception if the wrong type of value is passed. If a variable has both, the number is used. Usually this gets the right result, but sometimes it doesn't.
2.Objects don't have the capability to store both a number and a string as a key or value. Since numbers are more memory-efficient, if a variable has both, the number is used (except for floating-point values used as keys).

SetFormat's slow mode forces the assignment of a pure number to immediately convert that number to a string. For integers, the number is also stored, so this doesn't have adverse effects other than to performance. For floats, the number is not stored, since SetFormat affects the precision of the value, possibly even truncating all decimal places. In other words, SetFormat's slow mode prevents pure floats from being stored in variables.

Taking the address of a variable effectively converts the variable's value to a string, disabling caching until the variable's address changes (this happens when its capacity changes). This is both for backward-compatibility and because the script could change the value indirectly via its address at any time, making the cache inaccurate.

---------------------------------------------------

So let me ask about the following scenarios.

Say you have a function:

Func(x, y)
{
z := x + y
}

How does it know what to do in the following cases:

Func("hello", 1)//Adding 1 to a string makes no sense
Func(1, 2)//Ok
Func(true, false)//Also makes no sense
Func("hello", true)//Ditto


You see my point. Without any way for the script to specify the type, it's very hard for me to know what *should* be done under the covers, when porting this to C#. As you know in C#, object is the base of everything. But that still doesn't solve this problem.

Any help or direction would be much appreciated.

Thanks.
User avatar
mfeemster
Posts: 104
Joined: 24 Apr 2020, 18:30

Re: My experience porting AHK scripts to Linux.

11 May 2020, 15:54

I've further inspected both the AHK and IronAHK code and see how they do it. Horribly inefficient, but it looks like the only way.
User avatar
mfeemster
Posts: 104
Joined: 24 Apr 2020, 18:30

Re: My experience porting AHK scripts to Linux.

12 May 2020, 17:03

A moderator named BoBo just sent me a PM and I can't reply to him.

BoBo: if you see this, can you please enable the reply feature on my account? Thank you.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: My experience porting AHK scripts to Linux.

13 May 2020, 08:34

@mfeemster I've changed your status. Please check if you have access now, and good luck with finding some allies so ironAHK will rise like a Phoenix :thumbup:
User avatar
mfeemster
Posts: 104
Joined: 24 Apr 2020, 18:30

Re: My experience porting AHK scripts to Linux.

13 May 2020, 10:54

@BoBo Thanks, but it still won't let me reply to your message, or compose a new PM to you.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: My experience porting AHK scripts to Linux.

13 May 2020, 11:04

mfeemster wrote:
13 May 2020, 10:54
@BoBo Thanks, but it still won't let me reply to your message, or compose a new PM to you.
Our fellow moderator @gregster (or another of our guardian angels :angel: ) might shed some light on this (restriction). Stay tuned.
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: My experience porting AHK scripts to Linux.

13 May 2020, 15:12

BoBo wrote:
13 May 2020, 11:04
mfeemster wrote:
13 May 2020, 10:54
@BoBo Thanks, but it still won't let me reply to your message, or compose a new PM to you.
Our fellow moderator @gregster (or another of our guardian angels :angel: ) might shed some light on this (restriction). Stay tuned.
Did you check after your last post? You should be pretty close to the threshold.
User avatar
mfeemster
Posts: 104
Joined: 24 Apr 2020, 18:30

Re: My experience porting AHK scripts to Linux.

13 May 2020, 21:33

Yes, I can send PMs now. Thank you.
rbreaves
Posts: 2
Joined: 12 Jun 2020, 16:48

Re: My experience porting AHK scripts to Linux.

12 Jun 2020, 17:21

You guys might be interested in looking at the xkeysnail project as well, it has got to have the simplest and closest autohotkey like implementation I have seen, but written in python. The syntax is python, but for me it has worked equally well for most things I have thrown at it (although I need to submit some new PRs to fix some scenarios I might have broken).

https://github.com/mooz/xkeysnail

My own remapping project that uses both autohotkey and xkeysnail is located here https://github.com/rbreaves/kinto for anyone that wants to check it out. It would definitely be awesome to have autohotkey in linux though.

Wayland will be another ball game though compared to x11. I have done some early work on it though.

https://gist.github.com/rbreaves/257c3edfa301786e66e964d7ac036269

And a mention by this guy who updated an x11 app to work under wayland if it is running Gnome.
https://github.com/ActivityWatch/activitywatch/issues/92#issuecomment-642030180

I believe the same could be done for a python implementation such as xkeysnail or potentially a rewrite of ahk.
biorpg
Posts: 2
Joined: 11 Dec 2014, 20:22

Re: My experience porting AHK scripts to Linux.

11 May 2021, 06:59

mfeemster wrote:
11 May 2020, 15:32

Being a senior developer, variables always have a type. However, in AHK, their type seems to be indeterminate. It's sort of a string, but sort of an int, sort of a float, but that one isn't a string, and they might work with COM, but also might not. Ugh!

---------------------------------------------------

So let me ask about the following scenarios.

Say you have a function:

Func(x, y)
{
z := x + y
}

How does it know what to do in the following cases:

Func("hello", 1)//Adding 1 to a string makes no sense
Func(1, 2)//Ok
Func(true, false)//Also makes no sense
Func("hello", true)//Ditto


You see my point. Without any way for the script to specify the type, it's very hard for me to know what *should* be done under the covers, when porting this to C#. As you know in C#, object is the base of everything. But that still doesn't solve this problem.

Any help or direction would be much appreciated.

Thanks.
AHK is intuitive and easy to learn for the non-programmer, which is the majority of its users. One of the biggest hurdles in learning C++ or other strongly typed languages(without formal education), is not only grasping the concept of of *why* variables need to always have a type is how they are stored in memory, and how various functions interact with them, but moreso the massive struggle it can be to cast one type into another, such as the various convoluted methods of converting a string into a float, or sometimes even converting a string into a different *type* of string in order to be compatible with something.
With the way AHK is set up, one can start with a few simple examples to get started, and find that the need to reference documentation is less frequent, and far less frustrating or confusing.
The way to handle your example case, is to write it in a way that expects mixed parameter types, as such a function would need to do that anyway.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: My experience porting AHK scripts to Linux.

15 Nov 2021, 11:50

biorpg wrote:
11 May 2021, 06:59
AHK is intuitive and easy to learn for the non-programmer...

...but moreso the massive struggle it can be to cast one type into another, such as the various convoluted methods of converting a string into a float, or sometimes even converting a string into a different *type* of string in order to be compatible with something.
Having come further along in programming, looking back, I would say that I liked what AutoHotkey did/does. That is, being a weakly typed language. Of course to some, such is near heresy, and I completely understand that thinking. But, I think it takes time for non-programmers to become accustomed to strongly and statically typed languages and lots of type casting (or compile errors). Non-programmers can already be tentative about getting into the field, so adding to frustration levels is not encouraging. Often an intermediate usage step is to use string as a kind of default type ("no shame in their game"), have few types (hello JavaScript), or to even have an "emergency exit" (hello variants of Pascal).

In fact, various advocates make some strong arguments of the convenience provided by JavaScript guessing the type (of course others went the way of TypeScript). I will say that after playing with AutoHotkey, this include it providing if Var is Type for further assistance, JavaScript ways were quite comfortable to get used to. Another reason I think such is OK, is because JavaScript and other weakly and/or dynamically typed languages would more likely be supplemental languages used on various projects.

A lot is to be said for convenience, particularly where people can be entering the subject of programming from completely different backgrounds. Saw a video a little while back of an architect talking about his usage of AutoHotkey. Clearly, this was a reasonably intelligent man, but programming just wasn't his previous focus or background. So finding something convenient to do various programming tasks, is a great relief. Then, when the person feels ready or as necessary, they can progress into more advanced levels.
drake raider
Posts: 1
Joined: 06 Apr 2022, 00:56

Re: My experience porting AHK scripts to Linux.

06 Apr 2022, 01:00

Hey, new here, joined just for this thread.

I have some experience with AHK on windows, but I'm primarily a linux user for a variety of reasons.
However, I find myself in a bit of a dilemma. I need to map my mouse axis to send keyboard input for a specific program.
There are several ways I could so that, and most of them are a pain in the neck, but one thing I found was a relatively simple ahk script. Which makes sense, alot of them are pretty simple, and I had no issues understanding what it did.
But I have no idea how to recreate it in Autokey or any of the Ubuntu counterparts, and was wondering where I should seek guidance.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: My experience porting AHK scripts to Linux.

06 Apr 2022, 06:37

@drake raider - probably just waiting for this: viewtopic.php?f=80&t=77248
datavectors
Posts: 6
Joined: 25 Apr 2022, 05:56

Re: My experience porting AHK scripts to Linux.

25 Apr 2022, 06:13

@darke raider

I have just joined this forum to see how AHK is applied by cross platform users.

As one Ubuntu user to another.
Actually I do have Windows 10 in dual boot setup but largely I develop these days in Ubuntu.

I have searched for cross platform automation tools and my choice is Actiona.

[Actiona](https://github.com/Jmgr/actiona) is a cross-platform automation tool developed in the era of Qt4. It dates back to days of Actionscript but still a very effective tool.

[SikuliX](http://sikulix.com/quickstart/) started as an M.I.T. research project and is now under different stewardship. Sikulix requires Java installed.

Now I deplore using Wine on Ubuntu. Ubuntu internal tool to look at is xdotool.

But Actiona offers a nice GUI and after developing and testing scripts you can use the binary actexec to run the script - without GUI.

Although Actiona installs directly into Windows (and Linux) I might try building some Actiona scripts which manage underlying AHK scripts in a chain. But this requires me to boot up Windows and I'm busy on Ubuntu right now.

Incidentally Actiona uses Actionscript2 (Javascript) in Code objects.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: My experience porting AHK scripts to Linux.

24 Aug 2022, 05:24

datavectors wrote:
25 Apr 2022, 06:13
I have just joined this forum to see how AHK is applied by cross platform users.
As one Ubuntu user to another.
Actually I do have Windows 10 in dual boot setup but largely I develop these days in Ubuntu.
I have searched for cross platform automation tools...
Eventually, there will be a cross platform offshoot of AHK, which is Keysharp (viewtopic.php?f=80&t=77248) and uses C#/.NET. If you check Mfeemster's Bitbucket, he's making constant progress. This will cover Linux and the macOS, and it should be eventually able to run on Android, either by the user adding more Linux subsystems to Android (as Android is built on top of a Linux kernel) or a variant of Keysharp that runs on Android (via .NET 6 or later). A few examples on the focus on cross platform functionality: Convert an Android Device to Linux (https://www.linux-magazine.com/Online/Features/Convert-an-Android-Device-to-Linux) or Dot Net 6 (.NET 6) and How It Impacts Various Operating Systems? (https://www.ashutec.com/blog/dot-net-6-and-how-it-impacts-various-operating-systems-5b10399e47a3)

There is another AHK user who is in the process of porting an earlier version of AutoHotkey (using the Crystal language). This is AHK_X11, AutoHotkey for Linux (viewtopic.php?f=81&t=106640). By the way, this is possible with not just Crystal, but many other C/C++ alternative and cross-platform languages such as Object Pascal, D, Go, Rust, V, Red, C# (used with Keysharp), etc... Phil294 (AutoHotkey for Linux ) has also made it known that he would like to help/collaborate with Mfeemster (Keysharp), for whatever might be needed for Linux. Something to also be said of this Linux port of an earlier version of AHK, is it could be able to run on an Android too, after some modifications.

Additionally, there is the WINE project (https://www.winehq.org/), which runs on Linux, macOS, and Android. WINE is constantly having new releases and improving, so they are something to keep an eye on too. A version of WINE could be coming, that will allow the existing Windows AHK to have some useful functionality on other OSes, depending on what is being done.

AHK users can look forward to the possibility of using variants of AHK that are much closer to the Windows version (maybe even eventfully the current Windows AHK being somewhat useful on WINE) and are connected to the AutoHotkey website versus much more foreign automation scripting languages that look and do things very differently.

Now I deplore using Wine on Ubuntu. Ubuntu internal tool to look at is xdotool.
The idea of using xdotool with AHK, is quite old. Tinku99, created AutoHotkeyX, and used it with WINE. That version got a Platinum rating with WINE, however, that was quite a while ago and is poorly documented. That old version of AutoHotkey.dll was compiled with GCC, not Visual Studio and used xdotool.dll. It was a much more WINE friendly version of AutoHotkey, and it's interesting that the idea was not pursued more (specifically compiling more WINE friendly versions of AutoHotkey).

Below are related links (updated):

https://appdb.winehq.org/objectManager.php?sClass=version&iId=17738
(WINE AutoHotkeyX)

https://github.com/tinku99/ahkx/tree/master/xdotool
(Tinku99's AutoHotkeyX)

https://www.autohotkey.com/board/topic/28748-compiling-with-gcc/
(Compiling with GCC (old version of AHK))

https://github.com/tinku99/ahkmingw
(Compile autohotkey (old version) using gcc / mingw on windows)

Note: Tinku99 seems to still have some occasional activity on GitHub, so might be contacted over there.

AutoHotkey 1.0.48.05 (AHK v1 "Classic") can be installed and run on Linux using WineTricks (greater convenience). WINE should arguably not be looked down upon, because it is cross platform. Not just for Linux, but macOS and Android. The theory is that you can take a Windows only project, and bring it to multiple other OSes. WINE has been hit and miss on delivering that dream, but they keep coming out with better new releases too. Below is a YouTube video about such.

https://youtu.be/B2kRlouBtpc
(How to Install and Run AutoHotKey on Linux using WineTricks)
Last edited by SOTE on 02 Sep 2022, 05:23, edited 2 times in total.
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: My experience porting AHK scripts to Linux.

27 Aug 2022, 11:21

@SOTE, nice post, thank you!

Return to “Other Utilities & Resources”

Who is online

Users browsing this forum: No registered users and 24 guests