Ken Nichols Posted December 17, 2008 Share Posted December 17, 2008 First of all Forgive me if I posted this in the wrong place!I just want to get someone's opinion on how I wrote the following code.It does a search for all "CDROM" drives and adds a key in the registry (My Computer context menu ) allowing you to right click on a drive and select "Close Drive *:" It works fine but just wanted another opinion.Thanks for any help.FileCopy("CDR.exe", @WindowsDir & "\system32")For $i = 1 To 23 Step +1If $i = 1 then $CD = "D"Elseif $i = 2 then $CD = "E"Elseif $i = 3 then $CD = "F"Elseif $i = 4 then $CD = "G"Elseif $i = 5 then $CD = "H"Elseif $i = 6 then $CD = "I"Elseif $i = 7 then $CD = "J"Elseif $i = 8 then $CD = "K"Elseif $i = 9 then $CD = "L"Elseif $i = 10 then $CD = "M"Elseif $i = 11 then $CD = "N"Elseif $i = 12 then $CD = "O"Elseif $i = 13 then $CD = "P"Elseif $i = 14 then $CD = "Q"Elseif $i = 15 then $CD = "R"Elseif $i = 16 then $CD = "S"Elseif $i = 17 then $CD = "T"Elseif $i = 18 then $CD = "U"Elseif $i = 19 then $CD = "V"Elseif $i = 20 then $CD = "W"Elseif $i = 21 then $CD = "X"Elseif $i = 22 then $CD = "Y"Elseif $i = 23 then $CD = "Z"Endif$var = DriveGetType ($CD & ":")$bat = "cmd /c start /min cmd /c CDR.exe close " & $CD & ":"if $var = ("CDROM") then RegWrite("HKEY_CLASSES_ROOT\Drive\shell\Close Drive" & " " & $CD & ":" & "\command", "", "REG_EXPAND_SZ", $bat) NextMsgBox(0, "Close CD Tray Installation", "Close CD Tray Installation has completed successfully!") Link to comment Share on other sites More sharing options...
mara- Posted December 17, 2008 Share Posted December 17, 2008 I would do that in this way, it is much shorter and cleaner:FileCopy("CDR.exe", @WindowsDir & "\system32")$drivelist = DriveGetDrive("CDROM")For $i = 1 To $drivelist[0]$bat = "cmd /c start /min cmd /c CDR.exe close " & $drivelist[$i] & ":"RegWrite("HKEY_CLASSES_ROOT\Drive\shell\Close Drive" & " " & $drivelist[$i] & ":" & "\command", "", "REG_EXPAND_SZ", $bat)NextMsgBox(0, "Close CD Tray Installation", "Close CD Tray Installation has completed successfully!")Cheers Link to comment Share on other sites More sharing options...
MHz Posted December 18, 2008 Share Posted December 18, 2008 (edited) This is a different way to handle closing the CDRom tray. This works without specifying the drive letter literally in registry. It also uses AutoIt to handle the tray functions of opening (eject) or closing.If $CMDLINE[0] Then Switch $CMDLINE[1] Case 'help', '/?', '-?' ; show help MsgBox(0x40000, Default, 'Switches are:' & @CRLF & _ 'open <drive letter:>' & @CRLF & _ 'close <drive letter:>' & @CRLF & _ 'register' & @CRLF & _ 'unregister') Case 'open' CDTray($CMDLINE[2], 'open') Case 'close' CDTray($CMDLINE[2], 'closed') Case 'register' ; copy this compiled executable to system directory If Not FileCopy(@ScriptFullPath, @SystemDir & '\CDTray.exe', 1) Then MsgBox(0x40000, 'Close CD Tray Installation', _ 'Unable to copy "' & _ @ScriptFullPath & '" to "' & @SystemDir & '\CDTray.exe"') Exit 1 EndIf ; register CDTray.exe in registry $data = '"' & @SystemDir & '\CDTray.exe" close "%1"' If RegWrite('HKCR\Drive\shell\close', '', 'REG_SZ', 'Close Tray') _ And RegWrite('HKCR\Drive\shell\close\command', '', 'REG_SZ', $data) Then MsgBox(0x40000, 'Close CD Tray Installation', _ 'Close CD Tray Installation has completed successfully!') Else ; show error MsgBox(0x40000, 'Close CD Tray Installation', _ 'Close CD Tray Installation has not been added to registry') Exit 2 EndIf Case 'unregister' ; unregister CDTray.exe from registry If RegDelete('HKCR\Drive\shell\close') = 1 Then MsgBox(0x40000, 'Close CD Tray UnInstallation', _ 'Close CD Tray UnInstallation has completed successfully!') Else ; show error MsgBox(0x40000, 'Close CD Tray Installation', _ 'Close CD Tray Installation has not been removed from registry') Exit 3 EndIf EndSwitchEndIfCompile it to make it work correct before using.Edit:A note of usage:Use the command CDTray.exe register to install the compiled executable. Use CDTray.exe unregister to uninstall it. Use CDTray.exe close <drive letter:> to close drive tray and CDTray.exe open <drive letter:> to open drive tray. Of course the context menu method is available as well when registered. Edited December 18, 2008 by MHz Link to comment Share on other sites More sharing options...
Ken Nichols Posted December 21, 2008 Author Share Posted December 21, 2008 Thanks to both of you! This is exactly what i was looking for! I KNEW MY WAY WORKED BUT, I ALSO KNEW THERE WHERE MUCH BETTER WAYS OF DOING IT THANKS ALOT!!! Link to comment Share on other sites More sharing options...
Ken Nichols Posted December 22, 2008 Author Share Posted December 22, 2008 I can not get 2 While Statment to run at the same time. How can I achieve this?#include <GuiConstants.au3> Dim $MID, $ColorHotKeySet("{F8}","Get_coord")HotKeySet("{F9}","Get_Color")Opt("MouseCoordMode", 1)Opt("PixelCoordMode", 1)Func Get_coord() $MID = NOT $MID While $MID $pos = MouseGetPos() ToolTip('Mouse coordinates ' & @CRLF & " X = " & $pos[0] & @CRLF & " Y = " & $pos[1],0,0) Sleep(20) WEnd ToolTip("")EndFuncFunc Get_Color()$Color = NOT $Color While $Color$pos = MouseGetPos()$var = PixelGetColor( $pos[0],$pos[1] )ToolTip('The hex color is ' & Hex($var, 6),0,55) Sleep(20) WEndEndFunc Link to comment Share on other sites More sharing options...
MHz Posted December 22, 2008 Share Posted December 22, 2008 I can not get 2 While Statement to run at the same time. How can I achieve this?A function needs to return before you can call another function so that hinders your attempt. AutoIt3 runs code from a script line by line so trying to run 2 blocks of code at the exact same time cannot be done unless timers or other is used. The easiest solution would be to rearrange your code to use 1 loop to handle the conditions of concern at that time.#include <GuiConstants.au3>Dim $MID, $ColorHotKeySet('{F8}', '_HotKeyPressed')HotKeySet('{F9}', '_HotKeyPressed')Opt('MouseCoordMode', 1)Opt('PixelCoordMode', 1)GUICreate('Test', 300, 100)$label_coords = GUICtrlCreateLabel('Mouse coordinates:', 20, 20, 260, 20)$label_color = GUICtrlCreateLabel('Hex color:', 20, 50, 260, 20)GUISetState()While 1 If GUIGetMsg() = -3 Then ExitLoop EndIf $pos = MouseGetPos() $var = PixelGetColor($pos[0], $pos[1]) If $MID Then GUICtrlSetData($label_coords, 'Mouse coordinates: X = ' & $pos[0] & ' Y = ' & $pos[1]) EndIf If $Color Then GUICtrlSetData($label_color, 'Hex color: ' & Hex($var, 6)) EndIfWEndFunc _HotKeyPressed() If @HotKeyPressed = '{F8}' Then $MID = Not $MID ElseIf @HotKeyPressed = '{F9}' Then $Color = Not $Color EndIfEndFunc Link to comment Share on other sites More sharing options...
COOLCOMPUTERGUY Posted December 22, 2008 Share Posted December 22, 2008 (edited) Why not add a nircmd close drive command here:[HKEY_CLASSES_ROOT\Drive\shell\Cmd Here\command] Edited December 22, 2008 by COOLCOMPUTERGUY Link to comment Share on other sites More sharing options...
COOLCOMPUTERGUY Posted December 22, 2008 Share Posted December 22, 2008 (edited) [HKEY_CLASSES_ROOT\Drive\shell\Cmd Here\command]@="closedrive.exe"Where closedrive.exe is placed in windows\system32 and is an an .exe from a .bat file that uses nircmd.exe's close drive command.That's the sinch way to get your right click close drive. Edited December 22, 2008 by COOLCOMPUTERGUY Link to comment Share on other sites More sharing options...
Ken Nichols Posted December 23, 2008 Author Share Posted December 23, 2008 Thanks Mhz I did not realize I could not run 2 Functions at once. I have another question now.Forgive me for my explination! I don't know the proper way to describe it.If I have an application running (Active) that uses it's own F8-F9 commands how do I get my program to bypass that applications use of the F8-F9 keys? Link to comment Share on other sites More sharing options...
MHz Posted December 27, 2008 Share Posted December 27, 2008 ...If I have an application running (Active) that uses it's own F8-F9 commands how do I get my program to bypass that applications use of the F8-F9 keys?I am not sure what application and hotkeys has to do with an installation. Overriding hotkeys seems like acting against the flow so may I suggest asking these general questions at the AutoIt Forums where someone experienced with hotkeys and any other general questions can answer you. Link to comment Share on other sites More sharing options...
Geej Posted December 29, 2008 Share Posted December 29, 2008 (edited) I want to thanks MHz for the great example using Switch...Case...EndSwitch & using Command Line Parameters in CDROM script. It actually inspires me to write my own version of ejecting/closing the DVDtray. My idea is to place the menu only on My Computer -> Context menu -> Open ($Driveletter) & Eject ($driveletter)I'm stuck in getting it to display the helpfile when double click the complied exe (DVDTray.exe) w/o any parameters.(I understand help file is display via cmdline using DVDtray /?)Also I base the drive letter thru 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup', 'Installation Sources'as I think it represent the actual drive letter since it is use for Xp setup installation, not virtual drive letter. (I only have I drive). Or is there a better regkey to read off the drive letter? Advice appreciated.#NoTrayIcon$DVDLetter = StringMid(RegRead ('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup', 'Installation Sources'),1,2)$ValidiateDriveLetter=StringRegExp ($DVDLetter,"([A-Z]:)",0);0 = Returns 1 (matched) or 0 (no match) If $ValidiateDriveLetter=0 then;code 0 means invalid drive letter Msgbox (48,"Invalid Drive letter, Exit !",$DVDLetter) Exit endifIf $CMDLINE[0] then Switch $CMDLINE[1] Case 'help', '/?', '-?', '/help' Call ("_DisplayHELP") Case 'open' ToolTip ( "Ejecting "&$DVDLetter&" tray...", @DesktopWidth/2 , @DesktopHeight/2 , "DVDTray" , 0,1 ) CDTray($DVDLetter, 'open') Case 'close' ToolTip ( "Closing "&$DVDLetter&" tray...", @DesktopWidth/2 , @DesktopHeight/2 , "DVDTray" , 0,1 ) CDTray($DVDLetter, 'closed') EndSwitchEndif Func _DisplayHELP() $GetOSVer=@OSVersion If $GetOSVer =StringCompare ($GetOSVer,"WIN_VISTA",1) then $OSmsgText="Computer" If $GetOSVer =StringCompare ($GetOSVer,"WIN_2000",1) then $OSmsgText="My Computer" If $GetOSVer =StringCompare ($GetOSVer,"WIN_XP",1) then $OSmsgText="My Computer" MsgBox(0x40000, Default, 'DVDTray Cmd Syntax are:' & @CRLF & _ 'Open ('&$DVDLetter &')'& @CRLF & _ 'Close ('&$DVDLetter&')' &@CRLF&@CRLF & _ '*() Drive letter is automatically assigned.'&@CRLF & _ 'It will not take another drive letter'&@CRLF & _ 'such as DVDTray open J:'& @CRLF&@CRLF & _ 'Menu appears in Context Menu of '&$OSmsgText&@CRLF&@CRLF & _ 'This program can be uninstall in Add/Remove Programs -->DVDTray'); show help & info EndFunc==Edit==Sorry. My actual drive is $DVDLetter = StringMid(RegRead ('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup', 'Installation Sources'),1,2)Post the wrong drive as it was just to test my script. I have amended in the codebox to reflect this change but not in the attachment.There are 3 files that I built, 1) Installer, 2)DVDtray & 3)uninstallerI just post the DVDTray.au3 code part as I'm loss how to get it display the help info by doubling clicking DVDtray.exe Edited December 30, 2008 by Geej Link to comment Share on other sites More sharing options...
MHz Posted December 30, 2008 Share Posted December 30, 2008 I'm stuck in getting it to display the helpfile when double click the complied exe (DVDTray.exe) w/o any parameters.(I understand help file is display via cmdline using DVDtray /?)This conditional test is for checking if command line parameters were used and if so will be non zero.If $CMDLINE[0] then Switch $CMDLINE[1] Case 'help', '/?', '-?', '/help' _DisplayHELP() Case 'open' ToolTip ( "Ejecting "&$DVDLetter&" tray...", @DesktopWidth/2 , @DesktopHeight/2 , "DVDTray" , 0,1 ) CDTray($DVDLetter, 'open') Case 'close' ToolTip ( "Closing "&$DVDLetter&" tray...", @DesktopWidth/2 , @DesktopHeight/2 , "DVDTray" , 0,1 ) CDTray($DVDLetter, 'closed') EndSwitchElse _DisplayHELP()EndifAnd you could add a Case Else condition to show help if a unrecognized command line parameter was passed.If $CMDLINE[0] then Switch $CMDLINE[1] Case 'help', '/?', '-?', '/help' _DisplayHELP() Case 'open' ToolTip ( "Ejecting "&$DVDLetter&" tray...", @DesktopWidth/2 , @DesktopHeight/2 , "DVDTray" , 0,1 ) CDTray($DVDLetter, 'open') Case 'close' ToolTip ( "Closing "&$DVDLetter&" tray...", @DesktopWidth/2 , @DesktopHeight/2 , "DVDTray" , 0,1 ) CDTray($DVDLetter, 'closed') Case Else _DisplayHELP() EndSwitchElse _DisplayHELP()Endif Link to comment Share on other sites More sharing options...
Geej Posted December 31, 2008 Share Posted December 31, 2008 Thanks for the explanation, it is useful in understanding Autoit better.Like the code in the last codebox of your post (that solve this problem of mine):Case Else_DisplayHELP()EndSwitchElse_DisplayHELP()Thanks again & have a happy new year. 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