2

Right now I've got two Autokey scripts (for modularity), one that opens a file, and one that puts text in it.

The one that opens the file has hotkey F1 (and we'll call this script 1 for simplicity), and the one that puts text in it has hotkey F2. I want a new Autokey script, that when I hit F3, it runs both the 1 script and the 2 script.

I've tried making the 3 script just send the F1 and F2 keys, but the timing is all off. It would be better if I could just call 1 and 2 from 3. Is this possible?

Thanks!

4

3 回答 3

4

https://github.com/autokey/autokey/blob/fc7c6b90f3f28a57ad256829ef2b69e3be5148d2/lib/autokey/scripting.py#L1242

engine.run_script("<description>")

应该做的伎俩

这个上下文中的“Description”一般是AutoKey界面侧边栏中的脚本名称。如果您打开.json脚本的文件,您可以肯定地看到它,但它将是侧栏中显示的名称,除非您在同一文件夹或其他边缘场景中的脚本有重复的名称

于 2020-12-04T20:26:23.210 回答
3

AutoKey 不是递归的。它不会检查 AutoKey 短语或脚本的输出来查找热键或触发缩写,从而调用进一步的操作。这就是为什么您的初始解决方案不起作用的原因。

这取决于你实际上想要做什么。

如果您有多个独立有用的脚本,最好的方法是 @Icallitvera 提供的一个。

如果你只是想模块化共享功能,你可以创建函数的 Python 模块并将它们放在 AutoKey Modules 目录中。然后,您可以将它们导入到任何需要它们的 AutoKey 脚本中。

您可以通过 AutoKey 主菜单找到/设置模块目录Settings->Configure AutoKey->Script Engine

目前,这种方法受到限制,因为以这种方式调用的脚本无法(轻松)访问 AutoKey API,因此它们不能包含任何 API 调用。我们计划在下一个主要版本 AutoKey 0.96 中解决此问题。如果您现在真的需要这样做,请在我们的支持列表Gitter上询问。

于 2020-12-05T05:59:18.500 回答
1

我遇到了同样的问题,我发现解决该限制的唯一方法是使用该exec()函数。由于具有共享功能的脚本已经在我使用的 AutoKey 用户文件夹中。因此,为了将我的用户模块“mygame”的共享功能加载到我使用的自动键脚本中:

exec(open(engine.configManager.userCodeDir + "/" + "mygame.py").read())

为了避免在“导入”多个脚本时出现名称冲突和更像模块的感觉,我将函数放在类中,并使用命名为模块的变量进行实例化。

所以最后它看起来像这样:

我的游戏.py:

import time

class MyGame:
    def GameReload(self):
        self.GameExitNoSave()
        time.sleep(0.3)
        self.GameLoadCurrent()

    def GameExitNoSave(self):
        keyboard.send_key('d')
        time.sleep(0.1)
        keyboard.send_key('<up>')
        time.sleep(0.05)
        keyboard.send_key('<enter>')

    def GameLoadCurrent(self):
        keyboard.send_key('<down>')
        time.sleep(0.1)
        keyboard.send_key('<down>')
        time.sleep(0.1)
        keyboard.send_key('<enter>')
        time.sleep(0.5)
        keyboard.send_key('<enter>')
        
mygame = MyGame()   

AutoKey 中的用户脚本:

exec(open(engine.configManager.userCodeDir + "/" + "mygame.py").read())

mygame.GameReload():
于 2021-10-12T09:28:20.657 回答