Jump to content

A simple utility script


gsb
 Share

Recommended Posts

Still a noobe, I think I am well on my way to my first application.

It is a simple one, but should do the trick.

It is a backend control, not a visible window, but a simple tray icon.

I've included the code here to see if I am on the right track or if some code should be changed.

It is working fine and most code is patched from member's posts and the help files.

So I thought some more experienced AutoIt scriptors might give me a sanity check at this point.

Here is that script.

#Include <GUIConstants.au3>
#Include <Constants.au3>
#include <File.au3>
#Include "Misc.au3"

#NoTrayIcon

if _Singleton("gsb_fscp",1) = 0 Then; filename is gsb_fscp.exe
    Msgbox(0," fscp","...already running. Aborting.")
    Exit 0
EndIf

$log_file = "gsb_fscp.log"
$fp = FileOpen($log_file,2)
sleep(1)
FileClose($fp)
_FileWriteLog( $log_file, @ScriptName & " started." )

HotKeySet("+!t", "Terminate"); Shift-Alt-t

Opt("TrayMenuMode",1)
TraySetIcon("gsb_fscp.ico")
TraySetToolTip("fsCommand processor" & @CRLF & "by GSB v061014a")
$aboutitem = TrayCreateItem("About")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")
TraySetState()

$gui = GUICreate("gsb_fscp", 1, 1, -1, -1)
GUISetIcon ("SWiSHmax.ico")

$sfObj = ObjCreate("ShockwaveFlash.ShockwaveFlash.1")
$SinkObject = ObjEvent($sfObj,"controller_"); 
$GUIActiveX = GUICtrlCreateObj($sfObj, 0, 0, 1, 1)
$sfObj.Movie = @scriptdir & "\controller.swf?gsbTraceLevel=2"

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $aboutitem
            Msgbox(64,"About: fscp","fsCommand processor." & @CRLF & "by GSB v061014a")
        Case $msg = $exititem
            Msgbox(0," fscp","Exiting gsb_fscp")
            ExitLoop
    EndSelect
WEnd

_FileWriteLog($log_file, @ScriptName & " exited...")
Exit 0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func controller_FSCommand($cmd,$args)
    Select
        Case $cmd = "showmenu"
        Case $cmd = "log"
            _FileWriteLog($log_file, $args )
        Case Else
            _FileWriteLog($log_file, "FSCommand - cmd: " & $cmd & "  Args: " & $args & @CRLF)
            $sfObj.SetVariable("/:returnvalue","Success");
            msgbox(0," fscp","au3 FSCommand" & @crlf & "Cmd: " & $cmd & @crlf & "Args: " & $args)
    EndSelect
EndFunc

Func Terminate()
    HotKeySet("+!t"); Shift-Alt-t
    MsgBox(0," fscp","Treminating gsb_fscp.")
    _FileWriteLog($log_file, @ScriptName & " terminated...")
    Exit 0
EndFunc

Thanks for your time.

gsb

"Did you ever stop to think? ...and forget to restart!"
Link to comment
Share on other sites

Hi,

looks good to me. Only thing is: In func controller_FSCommand, you can use switch instead of select which is a little bit faster.

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Still a noobe, I think I am well on my way to my first application.

It is a simple one, but should do the trick.

It is a backend control, not a visible window, but a simple tray icon.

I've included the code here to see if I am on the right track or if some code should be changed.

It is working fine and most code is patched from member's posts and the help files.

So I thought some more experienced AutoIt scriptors might give me a sanity check at this point.

Here is the way I would code the script with the following goals :

-separate the script in "regions" where all data, vars and functions relative to a item are gathered.(not an object-oriented but with a same goal

This is also to allow further usage of include files

the regions allow to hide/show piece of code in SCITE

-Avoid global variables except in the #region scope with use of functions

-Allow to add in the "event" loop further items like GUI events without rewriting

-All exit points outside the event loop have been modified by setting a "scriptState" which is tested in the event loop. So there the only ways to end the script are shown in the codeScript function

NOTE : I have'nt tested the code as I am unable to create the object. Can you do it for me?

QUESTION : Why do you create in your script the GUI ?

#Include <GUIConstants.au3>
#Include <Constants.au3>
#include <File.au3>
#Include "Misc.au3"

#region Script*********************************************
codeScript()
exit
Func initScript()
    if _Singleton("gsb_fscp",1) = 0 Then; filename is gsb_fscp.exe
        Msgbox(0," fscp","...already running. Aborting.")
        return "exit"
    EndIf
EndFunc

Func scriptState($state="")
    global $running
    if $state<>"" then $running=$state
    Return $running
EndFunc

Func codeScript()
    ScriptState("running")
    if initScript()="exit" then return
    initLog()
    initHotKeys()
    initTray()
    initObject()
    initGUI()
    While ScriptState()="running"
        trackTray()
    WEnd
    writeToLog(@ScriptName & " " & ScriptState() & ".")
EndFunc

#endregion Script*****************************************

#region hotkeys*******************************************
Func initHotKeys()
    HotKeySet("+!t", "Terminate"); Shift-Alt-t
EndFunc

Func Terminate()
    HotKeySet("+!t"); Shift-Alt-t
    MsgBox(0," fscp","Treminating gsb_fscp.")
    ScriptState("Terminated")
EndFunc
#endregion hotkeys*******************************************

#region Log************************************************
Func LogFileName($name="gsb_fscp.log")
    return $name
EndFunc

Func initLog()
    writeToLog(@ScriptName & " started.")
EndFunc

Func writeToLog($text)
    _FileWriteLog( LogFileName(), $text )
EndFunc
#endregion Log**********************************************

#region Tray*************************************************
Func initTray()
#NoTrayIcon
    Opt("TrayMenuMode",1)
    TraySetIcon("gsb_fscp.ico")
    TraySetToolTip("fsCommand processor" & @CRLF & "by GSB v061014a")
    $aboutitem = TrayCreateItem("About")
    TrayCreateItem("")
    $exititem = TrayCreateItem("Exit")
    TraySetState()
EndFunc

Func trackTray()
    $msg = TrayGetMsg()
    Select
         Case $msg = $aboutitem
            Msgbox(64,"about: fscp","fsCommand processor." & @CRLF & "by GSB v061014a")
        Case $msg = $exititem
            Msgbox(0," fscp","Exiting gsb_fscp")
            scriptState("Exited")
    EndSelect
EndFunc

#endregion Tray****************************************

#region GUI**********************************************
Func initGUI()
    $gui = GUICreate("gsb_fscp", 1, 1, -1, -1)
    GUISetIcon ("SWiSHmax.ico") 
EndFunc
#endregion GUI******************************************

#region Object******************************************
Func initObject()
    $sfObj = ObjCreate("ShockwaveFlash.ShockwaveFlash.1")
    $GUIActiveX = GUICtrlCreateObj($sfObj, 0, 0, 1, 1)
    $sfObj.Movie = @scriptdir & "\controller.swf?gsbTraceLevel=2"
    $SinkObject = ObjEvent($sfObj,"controller_");
EndFunc

Func controller_FSCommand($cmd,$args)
    Select
        Case $cmd = "showmenu"
        Case $cmd = "log"
            writeToLog( $args )
        Case Else
            writeToLog( "FSCommand - cmd: " & $cmd & "  Args: " & $args & @CRLF)
            $sfObj.SetVariable("/:returnvalue","Success");
            msgbox(0," fscp","au3 FSCommand" & @crlf & "Cmd: " & $cmd & @crlf & "Args: " & $args)
    EndSelect
EndFunc
#endregion Object******************************************
Link to comment
Share on other sites

Valuater, Thank you.

That open/close code was the first way I found to empty the file at the script's startup. I was using WriteLine but could not find a "i/o flush" command and ended up with the log commands to accomplish that.

tresa, and Thank you too. That is some rewrite.

I will try it as soon as possible and let you know for it is Monday AM and I have a full day to work.

"Why do you create in your script the GUI?" - ignorance I guess.

I do not yet know enough, ...ah have my head around all the concepts of AutoIt and am patching together code snippets from posts and help files. That came from an example. The intent was to make a "container" for the flash object to run within in the background with no visible user content. That seemed to work. Your question is exactly why I posted the code here. I learn best by example and your code is appreciated.

gsb

"Did you ever stop to think? ...and forget to restart!"
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...