I'm relatively new to AutoIT, and have put together a script/gui to map/unmap a network drive. It works well, but there are several things I want to add and I'm not sure how. Would really appreciate someone educating me on the proper way to do these things.
Here's the code (got the base code from another topic, added onto it using Koda, forums, and help files.)
- After the drive is mapped, if the application receives no input for an hour, I want to delete the drive map. I do not want this to prevent users from using a GUI button to disconnect early.
- If drive is being unmapped after the aforementioned inactivity period, I want to display a message that lasts for 1 minute indicating this will happen, with the ability to cancel. (I assume this is similar to what I'm using for the exit confirmation)
- Pie in the sky (seemed complicated when I researched) it would be cool to show a status bar on the disconnect window counting down the 1 hour
Here's the code (got the base code from another topic, added onto it using Koda, forums, and help files.)
[ autoit ]
#AutoIt3Wrapper_icon=your_icon.ico #AutoIt3Wrapper_Run_Obfuscator=y #obfuscator_parameters=/striponly #NoTrayIcon #include <ButtonConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> ;home screen $home = GUICreate("[removed title]", 385, 176, -728, 125) GUISetFont(12, 400, 0, "MS Sans Serif") $map = GUICtrlCreateButton("Map the P Drive", 12, 11, 361, 75) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") $label_calendar = GUICtrlCreateLabel("Available Calendars", 100, 99, 170, 28) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $calendarlist = GUICtrlCreateCombo("Please Choose...", 65, 131, 241, 28, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL)) GUICtrlSetData(-1, "[removed list items]") GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") ;connect window $connectwindow = GUICreate("Connect To The P Drive", 337, 198, -706, 343, -1, -1, $home) $username_id = GUICtrlCreateInput("", 119, 26, 153, 32) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $password_id = GUICtrlCreateInput("", 119, 67, 153, 32, $ES_PASSWORD) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $label_username = GUICtrlCreateLabel("username", 45, 30, 60, 25) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $label_password = GUICtrlCreateLabel("Password", 15, 74, 90, 25) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $connect = GUICtrlCreateButton("Connect", 18, 114, 305, 57, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON)) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") ;disconnect window $disconnectwindow = GUICreate("Successful Map", 337, 199, -706, 344) $disconnect = GUICtrlCreateButton("Disconnect P Drive", 9, 65, 312, 60) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") $label_timeout = GUICtrlCreateLabel("The drive will automatically" & @CR & "disconnect after 1 hour.", 9, 5, 312, 57, $BS_MULTILINE) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") $open = GUICtrlCreateButton("Open P Drive", 9, 133, 313, 60) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW, $home) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE $confirm = MsgBox(1, "Confirm Exit", "Are you sure you want to close this application? The next person who uses this computer may need it. Note: Closing this application will un-map the P Drive.") If $confirm = 1 Then DriveMapDel("P:") Sleep(10) Exit EndIf Case $map DriveMapDel("P:") GUISetState(@SW_show, $connectwindow) Case $connect $username = GUICtrlRead($username_id) $password = GUICtrlRead($password_id) If $username = '' Or $password = '' Then MsgBox(16, 'Error', 'Empty username or password') ContinueLoop EndIf If DriveMapGet("P:") <> '' Then ; very fast MsgBox(16, 'Error', 'The drive letter (P:) is already in use. You must be very talented to be seeing this message.') ContinueLoop EndIf DriveMapAdd("P:", "[removed path]", 0, "ADND\" & $username, $password) ; slow If @error Then Switch @error Case 1 $err_message = 'Undefined / Other error. Windows API return code: ' & @extended Case 2 $err_message = 'Access to the remote share was denied' Case 3 $err_message = 'The device is already assigned' Case 4 $err_message = 'Invalid device name' Case 5 $err_message = 'Invalid remote share' Case 6 $err_message = 'Invalid password' EndSwitch MsgBox(16, 'Error', $err_message) Else ; Drive Mapped Sucessfully ; Show Mapped Drive ShellExecute("explorer.exe", "P:") ; Clear Username/Password Values and set focus to username text box GUICtrlSetData($username_id, "") $username = '' GUICtrlSetData($password_id, "") $password = '' GUICtrlSetState($username_id, $GUI_FOCUS) ; Hide Connect Window GUISetState(@SW_DISABLE, $connectwindow) GUISetState(@SW_HIDE, $connectwindow) GUISetState(@SW_SHOW, $disconnectwindow) EndIf Case $disconnect DriveMapDel("P:") ; Hide disconnect window and open the connect window for next user GUISetState(@SW_HIDE, $disconnectwindow) GUISetState(@SW_ENABLE, $connectwindow) Case $open ShellExecute("explorer.exe", "P:") EndSwitch WEnd