Autohot key variable function not global - function

I'm trying to make a function that takes coordinates, copies that field and name a variable, but the variable seems to be local and doesn't transfer out of the function. The coordinates (x,y) and delay seem to be working fine, but the desc variable always comes out blank. Please help!
newest(x, y, delay, variable)
{
sleep, %delay%
click, %x%,%y% ;desc machine field
clipboard = ; empty the clipboard
Loop
{
If GetKeyState("F2","P") ; terminate the loop whenever you want by pressing F2
break
Send {Ctrl Down}c{Ctrl Up}
if clipboard is not space
break ; Terminate the loop
}
variable := clipboard ;
msgbox %variable%
return
}
^k::
newest(654, 199, 200, desc)
msgbox %desc%
return

From the function's point of view, parameters are essentially the same
as local variables unless they are defined as ByRef.
ByRef makes one parameter of the function an alias for a variable, allowing the function to assign a new value to this variable.
newest(x, y, delay, ByRef variable){
sleep, %delay%
click, %x%,%y% ; desc machine field
clipboard = ; empty the clipboard
Loop
{
If GetKeyState("F2","P") ; terminate the loop whenever you want by pressing F2
break
Send {Ctrl Down}c{Ctrl Up}
If clipboard is not space
break ; Terminate the loop
}
variable := clipboard
msgbox %variable%
return
}
^k::
newest(54, 199, 200, desc)
msgbox %desc%
return

Related

Arguments not properly passed to function

I am new to Lua, and I downloaded and wanted to use the class.lua file from this link.
However, when I attempt to call the Board:addSign function from anywhere, no matter what I do, the arguments passed are the value of the place variable and nil instead of the values of the sign and place variables. How would I go around fixing that?
Here's my code, both main and the class itself.
Class = require 'class'
require 'Board'
board = Board()
for place = 1, 9 do
print('Input sign: ')
sign = io.read()
board.addSign(sign, place)
end
Board = Class{}
function Board:init()
array = {}
for n = 1, 9 do
array[n] = ' '
end
--
self.array = array
end
function Board:addSign(sign, place)
print(sign) -- outputs whatever I passed as place
print(place) -- outputs nil no matter what
self.array[place] = sign -- crashes here since place is nil
end
Use board:addSign instead of board.addSign.
function Board:addSign(sign, place) end
is syntactic sugar for
function Board.addSign(self, sign, place) end
This allows you to do things like self.array[place] = sign inside that function.
So you defined a function with 3 parameters but you only provide 2 when calling it.
board.addSign(sign, place)
Inside your function that basically results in
local self = sign
local sign = place
local place = nil
So either call Board:addSign(sign, place) or Board.addSign(Board, sign, place)

AutoHotkey - Building a Clipboardsaving Function

What I want to do is to build a function, that I can use to paste something (in this case URLs). Normally I would just use the send command or the sendinput command, but they are kind of slow which is a little bit annoying. That's why I want to avoid it and use the clipboard instead.
Here my function:
ClipPaster(CustomClip){
ClipSaved := ClipboardAll ;Saving the current clipboard
Clipboard := %CustomClip% ;Overwriting the current clipboard
Send, ^{v}{Enter} ;pasting it into the search bar
Clipboard := Clipsaved ;Recovering the old clipboard
}
Here how I'm using the function:
RAlt & b::
Send, ^{t} ;Open a new tab
ClipPaster("chrome://settings/content/images") ;Activating my clipboard
return
RAlt & g::
Send, ^{t} ;Open a new tab
ClipPaster("https://translate.google.com/#en/es/violin") ;Activating
my clipboard function
return
Then when I'm trying to use the function. I get an error:
Error: The following variable name contains an illegal character: "chrome://settings/content/images"
Line:
-->1934: Clipboard := %CustomClip%
What am I doing wrong here?
You get this error message because
Variable names in an expression are NOT enclosed in percent signs.
https://www.autohotkey.com/docs/Variables.htm#Expressions
ClipPaster(CustomClip){
ClipSaved := ClipboardAll ; Saving the current clipboard
Clipboard := "" ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
; Variable names in an expression are NOT enclosed in percent signs:
Clipboard := CustomClip ; Overwriting the current clipboard
ClipWait 1 ; wait max. 1 second for the clipboard to contain data
if (!ErrorLevel) ; If NOT ErrorLevel clipwait found data on the clipboard
Send, ^v{Enter} ; pasting it into the search bar
Sleep, 300
Clipboard := Clipsaved ; Recovering the old clipboard
ClipSaved := "" ; Free the memory
}
https://www.autohotkey.com/docs/misc/Clipboard.htm#ClipboardAll

Create HotKey inside a function (AutoHotKey)

So, I want my function to create a HotKey and returns the corresponding text of the hotkey when i press
here is my code below
global Object := {a:[1,"a","alexa"],b:[2,"b","battle"]}
global key_var1 :="!a"
global key_var2 := "!b"
create(key)
{
HotKey, %key%, myKey
return
myKey:
MsgBox, % Object.key[3]
return
}
create(key_var1)
create(key_var2)
The issue here is, when i press the hotkey, the message box displays nothing just empty.
When i press the HotKey the message box must display the corresponding text inside my Object array (text is in position 3)
Displays associative array element keyed to current hotkey:
global Object := {"!a":[1,"a","alexa"], "!b":[2,"b","battle"]}
global key_var1 := "!a"
global key_var2 := "!b"
create(key)
{
HotKey, %key%, myKey
return
myKey:
MsgBox, % A_ThisHotkey ":" Object[A_ThisHotkey][3]
return
}
create(key_var1)
create(key_var2)
Output:

Simple Function Errors when Running in Access

I am very new to program and VBA. I am trying to play around with VBA in Access so I wrote this little thing to test my understanding. However, when I git run, it pops up empty Macro dialog/window instead of a Message box says the things in the code ( I thought it would). Could anyone take 5 secs to let me know what did I miss please. Thank you very much
Public Function AddOne(value As Integer) As Integer
AddOne = value + 1
End Function
MsgBox "Adding 1 to 5 gives:" & AddOne(5)
It's not possible to Run a macro that takes any arguments/parameters from the "Run" dialog.
So if you press Run button, or F5, you'll see the Dialog box because that is Excel asking you "Which procedure do you want to run".
It will display any available procedures. Procedures which take any argument(s) will not be visible, since the arguments would not be supplied.
A few other points:
Your MsgBox statement is outside of the function. It should be inside the function
Function AddOne(val As Integer)
Dim ret As Integer
ret = val + 1
'Display msgBox:
MsgBox "Adding 1 to " & val & " gives:" & ret
'return to caller:
AddOne = ret
End Function
Since you can't run from the dialog, you need to invoke this manually from the Immediate window:
Or, you can simply print the result to immediate window using the following in the Immediate pane:
?AddOne(5)
Which will print "6" in the Immmediate.

Can a local function replace\override itself in Lua

In Lua is it possible to replace a local function from within itself? For example:
local function emptyFunction(arg)
return
end
local function exampleFunction(arg)
local result, err = io.open("/tmp/BadFile","w")
if result == nil then
print(err)
exampleFunction = emptyFunction
return
end
io.close(result)
end
To redefine a function within the function itself, you need a reference to the function in a variable that has already been declared before the variable is assigned a reference to the function.
First example -- how not to do it -- does not work because "fx" is not in scope inside fx, as intuition suggests (to me) it would not be.
Frankly, I don't know how to precisely describe what "fx" inside the function represents -- but it seems to be a global variable that is in a scope that is somehow "more global than a 'global local' declaration."
In the global space, I always assumed these two were equivalent:
foo = 'bar';
local foo = 'bar';
Precisely speaking, they apparently are not genuinely equivalent, if you have a way of accessing both. This first example does exactly that, and here's why:
When you do this...
local myfunc = function () ...
... what are you actually doing?
You're first declaring an anonymous function, and second declaring "myfunc" and third setting "myfunc" as a reference to the anonymous function you just created. Until the moment the function begins to exist, "local myfunc" does not yet exist, because the statement that declares it hasn't actually been executed yet. (At least, that's how I perceive it.)
Here's the non-working version that illustrates this:
local fx = function ()
print(fx);
print("inside the original fx")
fx = function() print "I was redefined" end
end
function f2()
print("f2")
f2 = fx
end
f = f2
print(fx);
fx(); fx(); f2(); f2() f();
function: 0x21a6140 -- original "fx" ref is in "global local fx"
nil -- inner "fx" -- NOT set on first iteration, so it
inside the original fx -- can't be the same varliable
function: 0x21a6510 -- now it's set, but to new function,
inside the original fx -- but the "global local fx" does not contain our new function
f2
function: 0x21a6510
inside the original fx
f2
Second example, we declare local fx first, and then set it to a reference to the function. There are no other changes.
local fx; -- declare before assignment
fx = function ()
print(fx);
print("inside the original fx")
fx = function() print "I was redefined" end
end
function f2()
print("f2")
f2 = fx
end
f = f2
print(fx);
fx(); fx(); f2(); f2() f();
function: 0x2188e50 -- original function
function: 0x2188e50 -- same reference, so this one *is* in scope
inside the original fx
I was redefined -- and redefining it works exactly as expected
f2
I was redefined
f2
So, yes, there is a way. You need a global local variable that is declared, and subsequently you need to set that to a reference to the function so that it is in scope in the function that tries to redefine it.
In a similar way, the f2 function can't redefine itself. It only redefines what f2 means, inside f2 while f2() is running.
Nothing prevents you from doing so. Local or not function name in Lua is actually variable name which points to function itself.
So there is no any difference between
local function f1()
...
end
and
local f1 = function()
...
end
In both cases f1 is in scope of function body.
However, such replace won't change no any external references
function f1()
print("f1")
end
function f2()
print("f2")
f2 = f1
end
f = f2
f1(); f2(); f2() f();
will lead to output
f1
f2
f1
f2
Note, that if you add local keyword to declarations - nothing changes