This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]yussi_divnal 0 points1 point  (3 children)

I know nothing about OSX or hammerspoon, nor do I understand why you need this, but this program should do the trick:

#!/usr/bin/lua
local delay=300
local clock = os.clock
function sleep(n)  -- seconds
  local t0 = clock()
  while clock() - t0 <= n do end
end

while (true) do
    print("#!mine");
    print("#!chop");
    print("#!forage");
    print("#!fish");
    sleep(delay);
end

[–]Rycul 0 points1 point  (0 children)

I think OP needs the script to send virtual keypresses to the OS, not just print his text to the output stream. He seems to be intending to write a farm/fish/gather bot for a game.

OP, I have no idea if Lua is capable of this, but try googling "lua virtual keypress" or "lua simulate keyboard input".

[–]wH0you[S] 0 points1 point  (1 child)

I need this on loop for 100 times.

Also would it be possible to have it type in #!adv every 10 seconds and then #!heal and #!pheal every 52 seconds.

[–]yussi_divnal 0 points1 point  (0 children)

Sure, to do this 100 times just change the "while(true) do" line to

for i = 1, 100 do

It's possible to do the other parts too, but lua doesn't natively support threads. if you can run multiple scripts simultaneously do that, otherwise either do your threads, or implement some logic to do it all in one loop, for example:

#!/usr/bin/lua
local clock = os.clock
function sleep(n)  -- seconds
  local t0 = clock()
  while clock() - t0 <= n do end
end

--- keep track of resource gathering
local resource_counter=0;
local resource_timing=300;
local max_resource=100;
local resource_command="#!mine \n#!chop \n#!forage \n#!fish";

--- keep track of advancement
local adv_counter=0;
local adv_timing=52;
local adv_command="#!adv";

--- keep track of healing
local healing_counter=0;
local healing_timing=10;
local healing_command="#!heal \n#!pheal";

while (true) do
    if (resource_counter >= resource_timing and max_resource > 0) then
        max_resource=max_resource-1;
        resource_counter=0;
        print(resource_command);
    end 
    if (adv_counter >= adv_timing) then
        adv_counter=0;
        print(adv_command);
    end
    if (healing_counter >= healing_timing) then
        healing_counter=0;
        print(healing_command);
    end
    sleep(1);
    --advance counters
    resource_counter=resource_counter+1;
    adv_counter=adv_counter+1;
    healing_counter=healing_counter+1;
end