Sign in to follow this  
Followers 0
ReitzelTechnology

Run Script all the time

18 posts in this topic

We would like to run a script when a user logs in to automatically connect to a network printer, and then pause (icon stays in notificication area) and in like 30 minutes reexecute the script (kinda on a loop).

IE: Like how when you run a script, the AutoIt icon is the taskbar, we want it to be there all the time and just reexecute the script every so often.

Share this post


Link to post
Share on other sites



While 1
    _MyScript($Params)
    Sleep(1000*60*30)
WEnd
Func _MyScript($args)
    ;code
EndFunc

We figured out how to get it to work, but when it stays in the taskbar, we want to be able to right click it and force it to rerun the script, or click the exit button.

Share this post


Link to post
Share on other sites

The helpfile is your friend. Try the example script for the TrayCreateMenu - it seems to be exactly what you are looking for.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

The helpfile is your friend. Try the example script for the TrayCreateMenu - it seems to be exactly what you are looking for.

We got the menu to come up right, but now the actual script doesn't execute. Our script before the menu change started at where is says $I0 and it worked.

Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.

TrayCreateItem("")
$refreshitem      = TrayCreateItem("Refresh")
TrayCreateItem("")
$exititem       = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $refreshitem
            ShellExecute(@ScriptFullPath)
            ExitLoop
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd
Exit

$I0 = @ComSpec & " /c " & 'cscript.exe "\\rtssrv\netlogon\SET_Default_Printer.vbs"'
useprinter("\\RTSSRV\CMF4370DN")
usefaxprinter("\\RTSSRV\CMF4370DN_FX")

func useprinter($uncpath)
    $serverAr = stringsplit($uncpath,"\")
    $server = $serverAr[3]

    if (ping($server)) then
        run(@comspec &' /c Rundll32 printui.dll,PrintUIEntry /in /n "' & $uncpath &'"',"",@SW_HIDE)
    endif
endfunc

func usefaxprinter($uncpath)
    $serverAr = stringsplit($uncpath,"\")
    $server = $serverAr[3]

    if (ping($server)) then
        run(@comspec &' /c Rundll32 printui.dll,PrintUIEntry /in /n "' & $uncpath &'"',"",@SW_HIDE)
    endif
endfunc

RunWait($I0,"","",@SW_HIDE)
Sleep(18000000)
ShellExecute(@ScriptFullPath)

Share this post


Link to post
Share on other sites

Dont you need TrayCreateMenu() first befor you go creating Items?

I don't know. I just need to know what I need to change to make it work. I don't understand how to make it work. :graduated:

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Seems like you need to at least remove the ExitLoop from your refreshitem code. Since the exitloop exits the main while loop, and there is an Exit right below it, the loop will be exited not only if you press the Exit option but also if you press the Refresh button.

By the way I tried replacing your exitloop code with a simple messagebox and that seems to work fine, even without the TrayCreateMenu that JohnOne suggested. Only the options in the menu have checkboxes, you might want to delve into the helpfile a bit deeper if you deem it important enough to change that.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Seems like you need to at least remove the ExitLoop from your refreshitem code. Since the exitloop exits the main while loop, and there is an Exit right below it, the loop will be exited not only if you press the Exit option but also if you press the Refresh button.

The exitloop is there because when you click refresh, I want it to restart the script from the start. The menu stuff works fine, but insted of it executing the script to map the printers, it just sits there.

Share this post


Link to post
Share on other sites

I want it to have the following menu when you right click on it:

"Refresh Printers"

"Exit PrintMgr"

The exit would quit out of the script and the Refresh should rerun the functions at the bottom to remap the printers.

I'm not sure how to code it.

Share this post


Link to post
Share on other sites

Try this

Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown.

TrayCreateItem("")
$refreshitem = TrayCreateItem("Refresh")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $refreshitem
            _Run()
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd
Exit

Func useprinter($uncpath)
    $serverAr = StringSplit($uncpath, "\")
    $server = $serverAr[3]

    If (Ping($server)) Then
        Run(@ComSpec & ' /c Rundll32 printui.dll,PrintUIEntry /in /n "' & $uncpath & '"', "", @SW_HIDE)
    EndIf
EndFunc   ;==>useprinter

Func usefaxprinter($uncpath)
    $serverAr = StringSplit($uncpath, "\")
    $server = $serverAr[3]

    If (Ping($server)) Then
        Run(@ComSpec & ' /c Rundll32 printui.dll,PrintUIEntry /in /n "' & $uncpath & '"', "", @SW_HIDE)
    EndIf
EndFunc   ;==>usefaxprinter
Func _Run()
    $I0 = @ComSpec & " /c " & 'cscript.exe "\\rtssrv\netlogon\SET_Default_Printer.vbs"'
    useprinter("\\RTSSRV\CMF4370DN")
    usefaxprinter("\\RTSSRV\CMF4370DN_FX")
    RunWait($I0, "", "", @SW_HIDE)
    Sleep(18000000)
    ShellExecute(@ScriptFullPath)
EndFunc   ;==>_Run

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

The exitloop is there because when you click refresh, I want it to restart the script from the start. The menu stuff works fine, but insted of it executing the script to map the printers, it just sits there.

Well in any case, at it is now the exitloop exits the main while loop and then the interpreter encounters an Exit and exits the script. That doesn't make it restart itself, it just makes it, well, exit the script.

... sorry for my perhaps somewhat sarcastic remark above, didn't mean to be mean... JohnOne was more helpful. Good luck with his solution! :graduated:

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Try this

Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown.

TrayCreateItem("")
$refreshitem = TrayCreateItem("Refresh")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $refreshitem
            _Run()
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd
Exit

Func useprinter($uncpath)
    $serverAr = StringSplit($uncpath, "\")
    $server = $serverAr[3]

    If (Ping($server)) Then
        Run(@ComSpec & ' /c Rundll32 printui.dll,PrintUIEntry /in /n "' & $uncpath & '"', "", @SW_HIDE)
    EndIf
EndFunc   ;==>useprinter

Func usefaxprinter($uncpath)
    $serverAr = StringSplit($uncpath, "\")
    $server = $serverAr[3]

    If (Ping($server)) Then
        Run(@ComSpec & ' /c Rundll32 printui.dll,PrintUIEntry /in /n "' & $uncpath & '"', "", @SW_HIDE)
    EndIf
EndFunc   ;==>usefaxprinter
Func _Run()
    $I0 = @ComSpec & " /c " & 'cscript.exe "\\rtssrv\netlogon\SET_Default_Printer.vbs"'
    useprinter("\\RTSSRV\CMF4370DN")
    usefaxprinter("\\RTSSRV\CMF4370DN_FX")
    RunWait($I0, "", "", @SW_HIDE)
    Sleep(18000000)
    ShellExecute(@ScriptFullPath)
EndFunc   ;==>_Run

This script worked, but I had to click the Refresh button to make to go. How do you make it when it runs, it does the same as when you click the refresh button?

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Just add _Run() underneath TraySetState()

I got everything to run as I want it, but when you click the menu commands, nothing happens. Edited by ReitzelTechnology

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Probably because its sleeping

Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown.
AdlibRegister("_Run",1000*60*30)
TrayCreateItem("")
$refreshitem = TrayCreateItem("Refresh")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()
_Run()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $refreshitem
            _Run()
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd
Exit

Func useprinter($uncpath)
    $serverAr = StringSplit($uncpath, "\")
    $server = $serverAr[3]

    If (Ping($server)) Then
        Run(@ComSpec & ' /c Rundll32 printui.dll,PrintUIEntry /in /n "' & $uncpath & '"', "", @SW_HIDE)
    EndIf
EndFunc   ;==>useprinter

Func usefaxprinter($uncpath)
    $serverAr = StringSplit($uncpath, "\")
    $server = $serverAr[3]

    If (Ping($server)) Then
        Run(@ComSpec & ' /c Rundll32 printui.dll,PrintUIEntry /in /n "' & $uncpath & '"', "", @SW_HIDE)
    EndIf
EndFunc   ;==>usefaxprinter
Func _Run()
    $I0 = @ComSpec & " /c " & 'cscript.exe "\\rtssrv\netlogon\SET_Default_Printer.vbs"'
    useprinter("\\RTSSRV\CMF4370DN")
    usefaxprinter("\\RTSSRV\CMF4370DN_FX")
    RunWait($I0, "", "", @SW_HIDE)
EndFunc   ;==>_Run

Notice the Adlbregister funcrtion at the top, that will run it every 30 minutes

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Probably because its sleeping

Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown.
AdlibRegister("_Run",1000*60*30)
TrayCreateItem("")
$refreshitem = TrayCreateItem("Refresh")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()
_Run()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $refreshitem
            _Run()
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd
Exit

Func useprinter($uncpath)
    $serverAr = StringSplit($uncpath, "\")
    $server = $serverAr[3]

    If (Ping($server)) Then
        Run(@ComSpec & ' /c Rundll32 printui.dll,PrintUIEntry /in /n "' & $uncpath & '"', "", @SW_HIDE)
    EndIf
EndFunc   ;==>useprinter

Func usefaxprinter($uncpath)
    $serverAr = StringSplit($uncpath, "\")
    $server = $serverAr[3]

    If (Ping($server)) Then
        Run(@ComSpec & ' /c Rundll32 printui.dll,PrintUIEntry /in /n "' & $uncpath & '"', "", @SW_HIDE)
    EndIf
EndFunc   ;==>usefaxprinter
Func _Run()
    $I0 = @ComSpec & " /c " & 'cscript.exe "\\rtssrv\netlogon\SET_Default_Printer.vbs"'
    useprinter("\\RTSSRV\CMF4370DN")
    usefaxprinter("\\RTSSRV\CMF4370DN_FX")
    RunWait($I0, "", "", @SW_HIDE)
EndFunc   ;==>_Run

Notice the Adlbregister funcrtion at the top, that will run it every 30 minutes

Thanks! That got it to work.

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
Sign in to follow this  
Followers 0