Jump to content
Atoxis

Autoit Perform Variable

Recommended Posts

Atoxis

Howdy, this is my first post, massive fan of autoit. 
I've searched and tried and I would just like people who are better at this than me to let me know if this is even a thing.

I'd like to perform just a variable. For example, it would be. *see inserted code*
So what i'm wanting is, create the constant $test, and that variable would be what is followed after the = . Then perform the _FileCreate. Then perform the variable.  Logically or in my head rather.. That variable is declared and is equal to what it is set to above, therefore just placing the variable plainly in the script, it should be equal to what it was declared as.  So what am I doing wrong, and or how can I have autoit just perform the variable.  


 

#include <File.au3>
Const $test = FileWriteLine(@DesktopDir & "\Log.txt", @CRLF )


_FileCreate(@DesktopDir & "\Log.txt")



$test

 

Share this post


Link to post
Share on other sites
Floops

The variable stores what is assigned to it. If you assign a function to a variable it only stores the return value of that function.

For what you want to do it would be best to make your own function and call it using its name. If you really want to use a variable you could assign the name of the function to the variable and call it using the Call() function.
Here is an example:

#include <File.au3>

_FileCreate(@DesktopDir & "\Log.txt")
$test = "name_of_func"

Call($test)

Func name_of_func()
    FileWriteLine(@DesktopDir & "\Log.txt", @CRLF )
EndFunc

Now you can call the function as often as you want using Call($test).

Hope it helps!

  • Like 1

Share this post


Link to post
Share on other sites
Atoxis

That'll work, appreciate it very much!

Now I'm working on pauses. If you open the tray icon it'll pause the script universally (wherever the script is currently at in its run).  << Which is utterly amazing. Looking for a way to tap into that because it's exactly that, a universal pause/sleep that i'd like to bind to a hotkey.

Again, thank you!

Share this post


Link to post
Share on other sites
Floops

Interesting task, maybe something like this would be what you're looking for?

Global $bPause = False
AdlibRegister("_pause")
HotKeySet("{ESC}", "_toggle_pause")


While 1
    ConsoleWrite("Not paused" & @CRLF)
    Sleep(50)
WEnd


Func _pause()
    While $bPause = True
        ConsoleWrite("Paused..." & @CRLF)
        Sleep(50)
    WEnd
EndFunc

Func _toggle_pause()
    $bPause = Not $bPause
EndFunc

 

Edit: I should really learn to read the helpfiles before posting. There is already an example for a pause toggle in the HotKeySet() help:

; Press Esc to terminate script, Pause/Break to "pause"

Global $g_bPaused = False

HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

While 1
    Sleep(100)
    ConsoleWrite("Script is running..." & @CRLF)
WEnd

Func TogglePause()
    $g_bPaused = Not $g_bPaused
    While $g_bPaused
        Sleep(100)
        ConsoleWrite("Script is paused..." & @CRLF)
    WEnd
EndFunc   ;==>TogglePause

Func Terminate()
    Exit
EndFunc   ;==>Terminate

 

Edited by Floops

Share this post


Link to post
Share on other sites
Atoxis

Let's apply it here.
A text file would be open, and the script would start by writing/sending the number 1, and then incrementing up by 1.
1,2,3,4,5,etc

while the script is doing this, i'd like to be able to press a hotkey that would work not by sleeping, and then ultimately needing me to restart the function again, resulting in starting back at 1.

I'd like to be able to run the above script, and then pause it in place as if I had clicked on the traymenu pause.
so when i'd unpause it via tray menu, the script would continue right where it had left off as if nothing else had happened.


I have school and work tomorrow starting at 8am my time (currently 2:03AM). But i'll keep trying to come up with something or else i'll just sleep and try to code it in my head lol.

Share this post


Link to post
Share on other sites
Atoxis

Keep In mind what i'm trying to make this for is something more complicated than sending numbers that can be stored as a var.

Ultimately I want to remote into the computer running the script, and be able to press a hotkey that globally pauses the script in its currently running line.   And be able to unpause it. Same as traymenu pause. And the script carries on as if it had never skipped a line.  I can't find a way to access the traymenu pause outside of actually using the menu.

Edited by Atoxis

Share this post


Link to post
Share on other sites
TurionAltec

Calling a MsgBox will pause the script until it's closed.

  • Like 1

Share this post


Link to post
Share on other sites
Atoxis
HotKeySet ("{F1}", "Sleeper")
HotKeySet ("{F2}", "start")

start()
Func start()
WinActivate("New Text Document - Notepad", "")

for $start = 0 to 100 Step +1
    Send($start)
    Sleep(500)
Next
EndFunc


Sleeper()
Func Sleeper()
While 1
Sleep(999999)
WEnd
EndFunc

Here I actually test it.
Open up a new note pad, and press F2.  The counter starts.
Pause it by pressing F1.

Test it by either pressing F2, resulting in counting at 0 again; or by pressing F1 to sleep, and pressing F2. The same resulted is achieved for both.

However_____
starting over.
Start the counter, then go and pause the script from the tray icon.
Wait 3 seconds for testing purposes.
now on the tray icon, unpause the script and quickly click in the text box field.
The counter resumes it's number as if it hadn't stopped.

This would be helpful to hotkey toggle that pause in the traymenu.
 

Share this post


Link to post
Share on other sites
Atoxis
1 hour ago, TurionAltec said:

Calling a MsgBox will pause the script until it's closed.

That's neat, i'll play with that some and see if I can make that work.

>
I tested it, resulted in starting back at 0 in the counter.  This is starting to make my head hurt.
I just want to use something that's already built in lol.

I also know if I store the number 0 , in the line
for $start = 0 to 100 step +1  as a variable above the function, and in within the for/next, write a +1 to that variable, it would continue on.  But this still wouldn't work in my real scenario that I need.

So final thing is, does anyone know a way to access the global pause that is built into the traymenuPause. And write it as a hotkey.

Edited by Atoxis
Tested

Share this post


Link to post
Share on other sites
Floops

How about this?

Global $bSleep = False

HotKeySet ("{F1}", "Sleeper")
HotKeySet ("{F2}", "Resume")

start()
Func start()
    WinActivate("New Text Document - Notepad", "")

    for $start = 0 to 100 Step +1
        Send($start)
        Sleep(500)
    Next
EndFunc

Func Resume()
    $bSleep = False
EndFunc

Func Sleeper()
    $bSleep = True
    While $bSleep
        Sleep(10)
    WEnd
EndFunc

 

Share this post


Link to post
Share on other sites
Atoxis

Ultimately I don't think it can be done using Sleeps in script, because you're calling out of the function to another function. Unless there's a way to call to a sleep func, and then return back to the exact line where you were at in the script before you called to sleep.  I gave up around 3am. Maybe in future releases they'll let you tap into it.

Share this post


Link to post
Share on other sites
Floops

Excuse me if I'm missing something here but isn't the code that I posted exactly doing what you are describing?

Once you press F1 it goes into the function Sleeper and stays there until you press F2, then the function Resume is called which allows Sleeper to return exactly where you were before you initially called Sleeper.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • California
      By California
      Hello,
      I wrote a benchmark script to measure variable declarations
      to find out whether you should focus more on static or global variables
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.5 #ce ---------------------------------------------------------------------------- #Region Pre-Setting Local $iTally1 = 0 Local $iTally2 = 0 Local $iTally3 = 0 Local $iTally4 = 0 Local $iTally5 = 0 Local $iTally6 = 0 Local $iTally7 = 0 Global $GLOBALCONST1 = 1 Global $GLOBALCONST2 = 1 Global $GLOBALCONST3 = 1 Global $GLOBALCONST4 = 1 Global $GLOBALCONST5 = 1 #EndRegion Pre-Setting #Region Test Functions Func s1() Static $i = $GLOBALCONST1 Return $i EndFunc Func g2() Return $GLOBALCONST2 EndFunc Func g3() Static $i7 = "gsdgdfegbgbrwefw" Return $GLOBALCONST3 EndFunc Func g4() Static $i1 = 1 Static $i2 = "asd" Static $i3 = 234 Static $i4 = True Static $i5 = [0] Static $i6 = "hgsdg" Static $i7 = 1 Static $i8 = 1 Static $i9 = 1 Static $i0 = 1 Return $GLOBALCONST4 EndFunc Func g5() Local $i = $GLOBALCONST5 Return $i EndFunc Func g6() Local $i = 1 Return $i EndFunc Func g7() Return 1 EndFunc #EndRegion Test Functions #Region Benchmark Loop For $i = 0 To 15 Local $tDelta = TimerInit() Do $iTally1 += s1() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally2 += g2() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally3 += g3() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally4 += g4() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally5 += g5() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally6 += g6() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally7 += g7() Until TimerDiff($tDelta) >= 1000 Next #EndRegion Benchmark Loop ConsoleWrite(@CRLF&"Static1: "&$iTally1&" pkt"&@CRLF&"Global2: "&$iTally2&" pkt"&@CRLF&"Global3: "&$iTally3&" pkt"&@CRLF&"Global4: "&$iTally4&" pkt"&@CRLF&"Local5: "&$iTally5&" pkt"&@CRLF&"Local6: "&$iTally6&" pkt"&@CRLF&"Hardcode7:"&$iTally7&" pkt"&@CRLF) #cs Result Static1: 10291881 pkt global to static Global2: 13977324 pkt only global Global3: 9886169 pkt global and static Global4: 2933051 pkt global and many statics Local5: 9937314 pkt global to local Local6: 10306484 pkt only local Hardcode7: 14835319 pkt no variable #ce Result:
      100% no variable, hardcore value
      94% only global variable use
      69% only local variable use with hardcore value set
      69% only static variable use with global variable value set
      67% declaration of local variable with global variable value set
      66% only global variable use with one static variable beside
      20% only global variable use with ten static variables beside
      My thesis of the result:
      Be careful with declarations, whether local, global or static Note: in my test the global variable performance was better than the local one, but in practice the global one would lose performance due to multiple operations
       
      What is your best practice sharing data between multiple functions?
    • TheAutomator
      By TheAutomator
      Is this better to check a variable before you assign it to a value that could be the same?
      for example:
      local $EmptyLog = false func WriteLog($text) _guictrledit_appendtext($log, ($EmptyLog ? @CRLF : $empty) & $text) If $EmptyLog Then $EmptyLog = False endfunc or does AutoIt behind the scenes already check this?
      i guess overwriting memory with the same value over and over again is not good if you can prevent this with a check?
    • JNutt
      By JNutt
      I see $hWnd used as a local variable in a lot of script examples.  I know the 'h' is used for handles, but what is the 'Wnd' short for?
      Thanks!
    • Rskm
      By Rskm
      i have seen many pointing out issues related to this, i tried reading through them - still unable to solve mine.. i have a script which reads certain lines based on a search item, this is an iterative process - the ldflst_file gets updated for each iteration and the script finds the line number which has the search item. this works perfectly for few iterations, but stops in some cases abruptly with the error = 
      Subscript used on non-accessible variable $aRead^ ERROR I have verified that the search item is present in the file. what could be the reason for this code to stop in certain cases. thanks for help
      my code below (partial).. full code is in attachment
       
      local $aRead=FileReadToArray($ldflst_file)
      global $ldflst_var1=" JOINT  LOAD       FORCE(X)        FORCE(Y)        FORCE(Z)       MOMENT(X)       MOMENT(Y)       MOMENT(Z)"
       
      for $i= 0 to $ldflst_totlines    ; ldflst_totlines is total number of  lines in the file 'ldflst_file'

          if $aRead[$i]=$ldflst_var1 Then
              $ldflst_LFound=$i
              ExitLoop
              else
              EndIf
      Next
       
       
      nomo_new1.au3
    • kartune
      By kartune
      Hello, i'm trying to rename a variable to another variable name (not the value attached to the variable).  This would save me TONS of coding.
      Here is an example:
       
      <autoit>
      Local $clear1="$SATJ"  ;sets the $clear1 variable to become $SATJ
      GUICtrlSetState($clear1 & "3", $GUI_UNCHECKED)  ;i want this to come out to be $SATJ3, it should look something like - GUICtrlSetState($SATJ3, $GUI_UNCHECKED)
      </autoit>
      Is this possible?  I've been searching all the forums and help files for hours.  My javascript friend referenced me to the solution on JS, which lead me to searching through the "assign" help on autoit, but not much info on it.
      I've already tried
      <autoit>
      Assign($clear1, $SATJ)
      </autoit>
       
      any help is greatly appreciated!!! 
×