NotAviable Posted May 15, 2008 Share Posted May 15, 2008 Hi, I have written a script to learn AutoIt that shoots down the pc, having configured it on a gui.I added to it the possibility to change the gui language from the menu, but by now it only writes the language chosen in a ini file, and it reads the ini configuration when you start it agai: I wanted to refresh the gui..The problem is on $radioReboot variable at line 245 (file "shutdown_please.au3"), it says "variable used without being declared" (if I comment this line it works, but does not what I wanted to)... someone can help me?The program if made of these files:shutdown_please.au3 - main scriptLABELS_IT.AU3 - translation labels for gui in italianLABELS_EN.AU3 - translation labels for gui in italianLABELS_DEFAULT.AU3 - translation labels for gui (experimental)LABELS.AU3 - labels (experimental)sp_config.ini (configuration file, by now only with language preferences)shutdown_please.au3-------------------------------------; Script for shutting down pc with delay time:; - shutdown could be from logoff to suspend; - time may be delayed, up to 24 days; TODO:; - shutdown at a given time (i.e. 12:00:00 AM); - menu and language change to be improved; The code of mine is under GPL 2; ******************************; AUTOIT SCRIPT OPTIONSOpt("MustDeclareVars", 1) ;variables MUST be declared; ******************************; COSTANTS; Configuration:Enum $ConfigFile = "sp_config.ini"; Constantants for AutoIt shutdown option:Enum $CodeLogoff = 0Enum $CodeShutdown = 1Enum $CodeReboot = 2Enum $CodeForce = 4Enum $CodePowerdown = 8Enum $CodeStandby = 32Enum $CodeHibernate = 64; Constant for results and controlsEnum $CTRL_OK = "OK"Enum $CTRL_NOK = "NOK"; Constants for messagesEnum $OK = 1Enum $CANCEL = 2Enum $ABORT = 3Enum $YES = 6Enum $NO = 7; Constants for languageEnum $LanguageCodeEN = "EN"Enum $LanguageCodeIT = "IT"Enum $LanguageCodeDef = "Default"; ******************************; INCLUDE; Include for text in different languages:#include "LABELS.AU3"Dim $configUserLanguage = IniRead($ConfigFile, "Language", "UserLanguage", $LanguageCodeDef)ChangeLanguage($configUserLanguage); ******************************; VARIABLESDim $shutdownChoice, $chosenActionLabel, $delayTime; ** GUI variables **Dim $eventMsgDim $choiceLanguage, $choiceAction, $choiceTimeLength, $choiceTime; * menu variablesDim $menuSettings, $menuSettingsLanguages, $menuitemSettingsLanguageSeparator1Dim $menuitemSettingsLanguageDefault, $menuitemSettingsLanguageIT, $menuitemSettingsLanguageEN; * group with radio - choice of shutdown action type variablesDim $radioLogoff, $radioShutdown, $radioReboot, $radioForce, $radioPowerdown, $radioStandby, $radioHibernate; * edit - choice of time variablesDim $editTime; * group with radio - choice of timer type variablesDim $radioSeconds, $radioMinutes, $radioHours; * buttons - ok and cancel variablesDim $okbutton, $cancelbutton; ******************************; PROGRAM; ** Generate GUI **ShutdownChoiceGui(); ** Manage GUI events **ManageGuiEvents(); ******************************; FUNCTIONS; ******************************; ******************************; *** ShutdownChoiceGui ***; Function that creates and shows the program GUI.Func ShutdownChoiceGui() ; INCLUDE #include <GUIConstants.au3>Global $radioLogoff, $radioShutdown, $radioReboot, $radioForce, $radioPowerdown, $radioStandby, $radioHibernate ; ** GUI creation ** GUICreate($TitleGUI) ;, 200, 100) ;GUISetIcon ("shutdown_please.ico") ; ** GUI composition ** ; * menu Settings $menuSettings = GUICtrlCreateMenu ($LabelSettingsMenu) $menuSettingsLanguages = GUICtrlCreateMenu ($LabelLanguagesMenu, $menuSettings, 1) $menuitemSettingsLanguageIT = GUICtrlCreateMenuitem ($LabelLanguageItalian, $menuSettingsLanguages) GUICtrlSetState(-1, $GUI_CHECKED) ;DA MODIFICARE!!!!!!!!!!!!!!! $menuitemSettingsLanguageEN = GUICtrlCreateMenuitem ($LabelLanguageEnglish, $menuSettingsLanguages) $menuitemSettingsLanguageSeparator1 = GUICtrlCreateMenuitem ("", $menuSettingsLanguages, 2) $menuitemSettingsLanguageDefault = GUICtrlCreateMenuitem ($LabelLanguageDefault, $menuSettingsLanguages) ;GUICtrlSetState(-1, $GUI_DEFBUTTON) ; * group with radio: choice of shutdown action type GUICtrlCreateGroup ($LabelGroupAction, 15, 10, 200, 250) GUIStartGroup() $radioLogoff = GUICtrlCreateRadio ($LabelRadioActionLogoff, 30, 30, 180, 30) $radioShutdown = GUICtrlCreateRadio ($LabelRadioActionShutdown, 30, 60, 180, 30) $radioReboot = GUICtrlCreateRadio ($LabelRadioActionReboot, 30, 90, 180, 30) $radioForce = GUICtrlCreateRadio ($LabelRadioActionForce, 30, 120, 180, 30) $radioPowerdown = GUICtrlCreateRadio ($LabelRadioActionPowerdown, 30, 150, 180, 30) $radioStandby = GUICtrlCreateRadio ($LabelRadioActionStandby, 30, 180, 180, 30) $radioHibernate = GUICtrlCreateRadio ($LabelRadioActionHibernate, 30, 210, 180, 30) GUICtrlCreateGroup ("", -99, -99, 1, 1) ;close group GUICtrlSetState ($radioHibernate, $GUI_CHECKED) ; * edit: choice of time (for timer) $editTime = GUICtrlCreateInput ( "1", 240, 30, 70, 20) ; * group with radio: choice of timer type GUICtrlCreateGroup ($LabelGroupTime, 225, 10, 120, 160) GUIStartGroup() $radioSeconds = GUICtrlCreateRadio ($LabelRadioTimeSeconds, 240, 60, 100, 30) $radioMinutes = GUICtrlCreateRadio ($LabelRadioTimeMinutes, 240, 90, 100, 30) $radioHours = GUICtrlCreateRadio ($LabelRadioTimeHours, 240, 120, 100, 30) GUICtrlCreateGroup ("", -99, -99, 1, 1) ;close group GUICtrlSetState ($radioHours, $GUI_CHECKED) ; * labels ;GUICtrlCreateLabel ; * buttons: ok and cancel $okbutton = GUICtrlCreateButton($LabelButtonOk, 100, 280, 60) $cancelbutton = GUICtrlCreateButton($LabelButtonCancel, 170, 280, 60) GUICtrlSetState(-1, $GUI_FOCUS) ;GUICtrlCreateButton ; ** Set state for GUI ** GUISetState(@SW_SHOW) ; * Default values $choiceLanguage = $configUserLanguage $choiceAction = $radioHibernate $choiceTimeLength = 1 $choiceTime = $LabelChoiceHoursEndFunc ; ShutdownChoiceGui; ******************************; *** ManageGuiEvents ***; Function that manages GUI events.Func ManageGuiEvents() While 1 $eventMsg = GUIGetMsg() Select ; * Laguage choice Case $eventMsg = $menuitemSettingsLanguageEN ChangeLanguage($LanguageCodeEN) Case $eventMsg = $menuitemSettingsLanguageIT ChangeLanguage($LanguageCodeIT) ;GUICtrlSetData($radioSeconds, $LabelRadioTimeSeconds) Case $eventMsg = $menuitemSettingsLanguageDefault ChangeLanguage($LanguageCodeDef) ; * Program base commands Case $eventMsg = $okbutton ; Time lenght choice $choiceTimeLength = GUICtrlRead($editTime);MsgBox(0, "GUI Event", ControlInput($choiceTimeLength)) If ControlInput($choiceTimeLength) = $CTRL_OK Then ; Action choice Switch $GUI_CHECKED Case BitAnd(GUICtrlRead($radioLogoff),$GUI_CHECKED) $choiceAction = $CodeLogoff $chosenActionLabel = StringReplace($LabelRadioActionLogoff, "&", "") Case BitAnd(GUICtrlRead($radioShutdown),$GUI_CHECKED) $choiceAction = $CodeShutdown $chosenActionLabel = StringReplace($LabelRadioActionShutdown, "&", "") Case BitAnd(GUICtrlRead($radioReboot),$GUI_CHECKED) $choiceAction = $CodeReboot $chosenActionLabel = StringReplace($LabelRadioActionReboot, "&", "") Case BitAnd(GUICtrlRead($radioForce),$GUI_CHECKED) $choiceAction = $CodeForce $chosenActionLabel = StringReplace($LabelRadioActionForce, "&", "") Case BitAnd(GUICtrlRead($radioPowerdown),$GUI_CHECKED) $choiceAction = $CodePowerdown $chosenActionLabel = StringReplace($LabelRadioActionPowerdown, "&", "") Case BitAnd(GUICtrlRead($radioStandby),$GUI_CHECKED) $choiceAction = $CodeStandby $chosenActionLabel = StringReplace($LabelRadioActionStandby, "&", "") Case BitAnd(GUICtrlRead($radioHibernate),$GUI_CHECKED) $choiceAction = $CodeHibernate $chosenActionLabel = StringReplace($LabelRadioActionHibernate, "&", "") EndSwitch ; Time type choice Switch $GUI_CHECKED Case BitAnd(GUICtrlRead($radioSeconds),$GUI_CHECKED) $choiceTime = $LabelChoiceSeconds Case BitAnd(GUICtrlRead($radioMinutes),$GUI_CHECKED) $choiceTime = $LabelChoiceMinutes Case BitAnd(GUICtrlRead($radioHours),$GUI_CHECKED) $choiceTime = $LabelChoiceHours EndSwitch;MsgBox(0, "GUI Event", "Lang: " & $choiceLanguage & " - Action: " & $choiceAction & " - Timer time: " & $choiceTimeLength & " - Time type: " & $choiceTime) ;, 4) $delayTime = SetDelayTime($choiceTimeLength, $choiceTime) $ConfirmMsg = StringReplace($ConfirmMsg, "$chosenActionLabel", $chosenActionLabel) $ConfirmMsg = StringReplace($ConfirmMsg, "$choiceTimeLength", $choiceTimeLength) $ConfirmMsg = StringReplace($ConfirmMsg, "$choiceTime", $choiceTime) If MsgBox(4, $TitleConfirmMsg, $ConfirmMsg) = $YES Then DoShutdown($choiceAction, $delayTime) ExitLoop EndIf Else ; time lenght input not correct MsgBox(0, "GUI Event", $ErrorMsgChoiceTimeLength) EndIf Case $eventMsg = $cancelbutton ExitLoop Case $eventMsg = $GUI_EVENT_CLOSE Exit ;ExitLoop EndSelect WEnd EndFunc ; ManageGuiEvents; ******************************; *** ChangeLanguage ***; Function that changes settings and GUI language (messages included)Func ChangeLanguage($configUserLanguageIN) ; Memorize user choice: Dim $ctrlIniWrite = IniWrite($ConfigFile, "Language", "UserLanguage", $configUserLanguageIN) If $ctrlIniWrite == 1 Then ; Change GUI language according to user choice: Switch $configUserLanguageIN Case $LanguageCodeDef #include "LABELS_DEFAULT.AU3" Case $LanguageCodeEN #include "LABELS_EN.AU3" GUICtrlSetData($radioReboot, "");$LabelRadioActionReboot) Case $LanguageCodeIT #include "LABELS_IT.AU3" Case Else #include "LABELS_DEFAULT.AU3" EndSwitch Else #include "LABELS_DEFAULT.AU3" MsgBox(48, $TitleChangeLanguageNOK, $MsgChangeLanguageNOK) EndIfEndFunc ; ChangeLanguage; ******************************; *** ControlInput ***; Function that verify (and eventually change) data input received from GUI.Func ControlInput($choiceTimeLengthIN) Dim $control $control = StringIsInt($choiceTimeLengthIN) If $control == 1 Then Return $CTRL_OK Else Return $CTRL_NOK EndIf ; If $choiceTimeLengthIN > 0 AND $choiceTimeLengthIN < 1 Then ; Return $CTRL_OK ; Else ; Return $CTRL_NOK ; EndIfEndFunc ; ControlInput; ******************************; *** SetDelayTime ***; Function that prepares delay time in the correct format.Func SetDelayTime($choiceTimeLengthIN, $choiceTimeIN) Dim $timeMultiplier ; Note: delay time must be in milliseconds Switch $choiceTimeIN Case $LabelChoiceSeconds $timeMultiplier = 1000 Case $LabelChoiceMinutes $timeMultiplier = 60 * 1000 Case $LabelChoiceHours $timeMultiplier = 60 * 60 * 1000 EndSwitch Return $choiceTimeLengthIN * $timeMultiplierEndFunc ; ControlInput; ******************************; *** DoShutdown ***; Function that executes the shutdown, accordingly to user choices.Func DoShutdown($shutdownChoiceIN, $timeIN) Sleep ($timeIN) ;in milliseconds (max 24 days) Shutdown($shutdownChoiceIN)EndFunc ; DoShutdown-------------------------------------LABELS_IT.AU3-------------------------------------; Variables$LabelChoiceSeconds = "secondi"$LabelChoiceMinutes = "minuti"$LabelChoiceHours = "ore"; Testo per la GUI:$TitleGUI = "Spegnimento pc"$LabelSettingsMenu = "&Impostazioni"$LabelLanguagesMenu = "Lingua"$LabelLanguageItalian = "italiano"$LabelLanguageEnglish = "english"$LabelLanguageDefault = "Default"$LabelGroupAction = "Scegli l'azione"$LabelRadioActionLogoff = "&disconnetti"$LabelRadioActionShutdown = "spe&gni"$LabelRadioActionReboot = "&riavvia"$LabelRadioActionForce = "&forza lo spegnimento"$LabelRadioActionPowerdown = "&powerdown"$LabelRadioActionStandby = "stand&by"$LabelRadioActionHibernate = "sospe&ndi"$LabelGroupTime = "Esegui tra"$LabelRadioTimeSeconds = "&secondi"$LabelRadioTimeMinutes = "&minuti"$LabelRadioTimeHours = "&ore"$LabelButtonOk = "Ok"$LabelButtonCancel = "Annulla"; Messaggi di errore:$TitleChangeLanguageNOK = "Errore nelle impostazioni lingua"$MsgChangeLanguageNOK = "Si è verificato un errore nelle impostazioni della lingua. Provare a verificare se il file di configurazione è di sola lettura."$ErrorMsgChoiceTimeLength = "Il formato del tempo di esecuzione digitato non è corretto."$TitleConfirmMsg = "Conferma"$ConfirmMsg = "Hai scelto '$chosenActionLabel' tra $choiceTimeLength $choiceTime. Confermi?"-------------------------------------LABELS_EN.AU3-------------------------------------; Variables$LabelChoiceSeconds = "seconds"$LabelChoiceMinutes = "minutes"$LabelChoiceHours = "hours"; Testo per la GUI:$TitleGUI = "Shutdown pc"$LabelSettingsMenu = "&Settings"$LabelLanguagesMenu = "Language"$LabelLanguageItalian = "italiano"$LabelLanguageEnglish = "english"$LabelLanguageDefault = "Default"$LabelGroupAction = "Choose an action"$LabelRadioActionLogoff = "&logoff"$LabelRadioActionShutdown = "&shudown"$LabelRadioActionReboot = "&reboot"$LabelRadioActionForce = "&force shutdown"$LabelRadioActionPowerdown = "&powerdown"$LabelRadioActionStandby = "stand&by"$LabelRadioActionHibernate = "&hibernate"$LabelGroupTime = "Execution delay"$LabelRadioTimeSeconds = "&seconds"$LabelRadioTimeMinutes = "&minutes"$LabelRadioTimeHours = "&hours"$LabelButtonOk = "Ok"$LabelButtonCancel = "Cancel"; Messaggi di errore:$TitleChangeLanguageNOK = "Error in language settings"$MsgChangeLanguageNOK = "An error has occurred in language settings. Try and check if configuration file is read-only."$ErrorMsgChoiceTimeLength = "Delay time format is not correct."$TitleConfirmMsg = "Confirm"$ConfirmMsg = "You have chose '$chosenActionLabel' at $choiceTimeLength $choiceTime from now. Do you confirm?"-------------------------------------LABELS_DEFAULT.AU3-------------------------------------; Can be used to include other languages not foreseen at time of developing, so you can quickly add one; by:; 1) commenting the below instruction, and; 2) copying below the declarations in a language file containing the labels, i.e. LABELS_EN.AU3, ; and translate the values in your preferred language; 3) when the user opens the GUI, he should choose the default language as the GUI language (normally it is english).#include "LABELS_EN.AU3"-------------------------------------LABELS.AU3-------------------------------------; VariablesGlobal $LabelChoiceSecondsGlobal $LabelChoiceMinutesGlobal $LabelChoiceHours; GUI:Global $TitleGUIGlobal $LabelSettingsMenuGlobal $LabelLanguagesMenuGlobal $LabelLanguageItalianGlobal $LabelLanguageEnglishGlobal $LabelLanguageDefaultGlobal $LabelGroupActionGlobal $LabelRadioActionLogoffGlobal $LabelRadioActionShutdownGlobal $LabelRadioActionRebootGlobal $LabelRadioActionForceGlobal $LabelRadioActionPowerdownGlobal $LabelRadioActionStandbyGlobal $LabelRadioActionHibernateGlobal $LabelGroupTimeGlobal $LabelRadioTimeSecondsGlobal $LabelRadioTimeMinutesGlobal $LabelRadioTimeHoursGlobal $LabelButtonOkGlobal $LabelButtonCancel; Error messages:Global $TitleChangeLanguageNOKGlobal $MsgChangeLanguageNOKGlobal $ErrorMsgChoiceTimeLengthGlobal $TitleConfirmMsgGlobal $ConfirmMsg-------------------------------------sp_config.ini-------------------------------------[Language]UserLanguage=EN------------------------------------- Link to comment Share on other sites More sharing options...
martin Posted May 16, 2008 Share Posted May 16, 2008 If you use a variable for the first time in a function then that variable will be local to that function unless it was already declared as Global. That means it is only known about in that function and once you leave the function it is unknown. If you want to have a variable that is available anywhere in your script it has to be a Global variable and must be declared as such. Either declare it at the beginning of your script Global $RadioReboot, $Anothervar,.... the when you have $RadioReboot = GuiCtrlCreate.. it will be the global variable that is used. OR Declare it as Global in the function- Global $RadioReboot = GuiCtrlCreate..... Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
Info Posted May 18, 2008 Share Posted May 18, 2008 What he said ^^^^ And next time go to "Inseart special item" >>> "AutoIt", and there post your script Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now