Jump to content

Self-restarting script? :)


dv8
 Share

Recommended Posts

Thanks.

I suppose one could use just

Run(@ScriptFullPath)

in the example without using Beta, but prevents Au3 execution as when UnCompiled. :)

@dv8

It is If conditional on whether a 2nd instance starts or not. Using the switch restart will allow a 2nd instance and close the first. You cannot have it both ways of executing a 2nd instance and not allowing it.

Edit:

If you want, have a look at FAQ 14 and apply it within the restart part of the If block.

Sorry :">

I see it now.

This suits me perfect!!!

Thank you very much !

Link to comment
Share on other sites

  • Moderators

Your Welcome.

And thanks SmOke_N for creating the good example to show the idea working.

No thank you for taking the time... This is Awesome... (Don't know if I'll ever need it), but a hell of a lot better than my offering!! (Even though I believe you wrote the _SelfDelete() too?! :) ) Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Here is the fruit of all this:

This a small program (HotKey.au3) to define and use (up to 64) HotKeys for any file on your system. :)

;HotKey.au3
#include <GuiConstants.au3>

If Not $CMDLINE[0] Then
    If UBound(ProcessList(@ScriptName)) > 2 Then Exit
ElseIf $CMDLINE[1] <> '/restart' Then
    Exit
EndIf

Global $HK=CheckINI()

Opt("TrayMenuMode",1)
Opt("TrayOnEventMode",1)

$Settings_tray = TrayCreateItem("Settings")
TrayItemSetOnEvent($Settings_tray,"_Settings")

$Restart_tray = TrayCreateItem("Restart")
TrayItemSetOnEvent($Restart_tray,"_Restart")

$exit_tray = TrayCreateItem("Exit (Ctrl+Alt+X)")
TrayItemSetOnEvent($exit_tray,"_Exit")

TraySetState()

HotKeySet ( "^!x","_Exit")

For $i=1 to $HK[0][0]
    HotKeySet ( "^"&$HK[$i][0],"StartEvent")
Next

While 1
    sleep (100)
WEnd

Func StartEvent()
     $Pressed=StringTrimLeft(@HotKeyPressed,1)
     For $i=1 to $HK[0][0]
           If $Pressed=$HK[$i][0] Then
            DllCall("shell32.dll", "long", "ShellExecute", "hwnd", 0, "string", "", "string",$HK[$i][1], "string", "", "string", "", "long", @SW_SHOWNORMAL)
            exitloop
           EndIf
     Next
EndFunc

Func CheckINI()
     $var = IniReadSection(@ScriptDir&"\HotKey.ini", "HOTKEYS")
    If @error Then
        MsgBox(16,"HotKey","Error! Probably missing INI file.")
        exit
    ElseIf $var[0][0]>64 Then
        MsgBox(16,"HotKey","You have defined more than 64 (max)hot keys!")
        exit
    Else
        For $i=1 to $var[0][0]
              If StringLen($var[$i][0])>2 Then
                 MsgBox(16,"HotKey","Error! The KEY """&$var[$i][0]&""" in the INI file consists of more than 2 sybmols!")
                 exit
          ElseIf $var[$i][0]="!x" Then
             MsgBox(16,"HotKey","Error! The combination ""!x"" is for exiting the HotKey program!")
                 exit
              ElseIf $var[$i][0]="" Then
             MsgBox(16,"HotKey","Error! Missing key for the value """&$var[$i][1]&""" in the INI file!")
             exit
          ElseIf $var[$i][1]="" Then
             MsgBox(16,"HotKey","Error! The key """&$var[$i][0]&""" in the INI file has no value!")
             exit
          EndIf
        Next
        return($var)
    EndIf

EndFunc

Func _Settings()
     Run("notepad.exe "&@ScriptDir&"\HotKey.ini")
EndFunc

Func _Exit()
     exit
EndFunc

Func _Restart()
     Run(@ScriptFullPath& " /restart")
     exit
EndFunc

And here is the sample INI file:

;HotKey.ini
[HOTKEYS]
+c="C:\Program Files\ConTEXT\ConTEXT.exe"
+f="C:\Program Files\Mozilla Firefox\firefox.exe"
+a=C:\Program Files\AutoIt3\beta\AutoIt3.chm
+p=mspaint
!n=c:\New text document.txt

The fist key of the combination is always the CTRL key (the program puts it automaticaly)

"+" is for SHIFT

"!" is for ALT

I hope this will be as usefull to anybody as it is to me. :mellow:

Edited by dv8
Link to comment
Share on other sites

Not happen that way, because there can be only one instance of it.

it has the line:

If UBound(ProcessList(@ScriptName)) > 2 Then Exit

That's because it has many HOTKEYS and if there is another instance the hotkeys mess up. :)

Then, unregister the hot keys before starting another instance
Link to comment
Share on other sites

Here is the fruit of all this:

This a small program (HotKey.au3) to define and use (up to 64) HotKeys for any file on your system. :)

The fist key of the combination is always the CTRL key (the program puts it automaticaly)

"+" is for SHIFT

"!" is for ALT

I hope this will be as usefull to anybody as it is to me. :)

Hmm, sounds interesting. May take me alittle time to set a test with configuring it. If it is proven good to share by you, then you could post it in the Script'n'Scraps forum to share with the AutoIt community. :mellow:
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...