MSFN Forum: AutoIT Help needed - MSFN Forum

Jump to content



Unattended CD/DVD Guide Homepage · MSFN Forum Rules

Welcome to the Applications Installs forum. Make sure you read the forum rules before you start posting.

Links/Requests to warez and/or any illegal material (porn, cracks, serials, etc..) will not be tolerated. Discussion of circumventing WGA/activation/timebombs/keygens or any other illegal activity will also not be tolerated.

We try our best to keep this forum clean of illegal content. If you see any illegal activity use the "report" button you find in every post to report the specific post to the moderators. If you ignore any of the rules you will be banned without notice.

Read Forum Rules
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

AutoIT Help needed I could use some help with AutoIT Rate Topic: -----

#1 User is offline   Ken Nichols 

  • Group: Members
  • Posts: 6
  • Joined: 24-November 08

Posted 17 December 2008 - 11:32 AM

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 +1

If $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)	

Next

MsgBox(0, "Close CD Tray Installation", "Close CD Tray Installation has completed successfully!") 



#2 User is offline   mara- 

  • Office Integrator Developer
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,159
  • Joined: 19-February 07

Posted 17 December 2008 - 05:09 PM

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)
Next
MsgBox(0, "Close CD Tray Installation", "Close CD Tray Installation has completed successfully!")


Cheers ;)

#3 User is offline   MHz 

  • SendToA3X v1.7
  • PipPipPipPipPipPipPip
  • Group: Members
  • Posts: 1,634
  • Joined: 02-August 04

Posted 18 December 2008 - 04:48 AM

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					
	EndSwitch
EndIf
 

Compile 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.

This post has been edited by MHz: 18 December 2008 - 08:05 AM


#4 User is offline   Ken Nichols 

  • Group: Members
  • Posts: 6
  • Joined: 24-November 08

Posted 21 December 2008 - 05:07 PM

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!!!

#5 User is offline   Ken Nichols 

  • Group: Members
  • Posts: 6
  • Joined: 24-November 08

Posted 21 December 2008 - 06:32 PM

I can not get 2 While Statment to run at the same time. How can I achieve this?
 
#include <GuiConstants.au3>	
Dim $MID, $Color
HotKeySet("{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("")
EndFunc

Func 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)
	WEnd
EndFunc 


#6 User is offline   MHz 

  • SendToA3X v1.7
  • PipPipPipPipPipPipPip
  • Group: Members
  • Posts: 1,634
  • Joined: 02-August 04

Posted 22 December 2008 - 07:09 AM

View PostKen Nichols, on Dec 22 2008, 10:32 AM, said:

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, $Color
HotKeySet('{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))
	EndIf
WEnd

Func _HotKeyPressed()
	If @HotKeyPressed = '{F8}' Then
		$MID = Not $MID
	ElseIf @HotKeyPressed = '{F9}' Then
		$Color = Not $Color
	EndIf
EndFunc
 


#7 User is offline   COOLCOMPUTERGUY 

  • Cool.
  • Pip
  • Group: Members
  • Posts: 72
  • Joined: 27-October 08

Posted 22 December 2008 - 07:23 AM

Why not add a nircmd close drive command here:

[HKEY_CLASSES_ROOT\Drive\shell\Cmd Here\command]

This post has been edited by COOLCOMPUTERGUY: 22 December 2008 - 07:39 AM


#8 User is offline   COOLCOMPUTERGUY 

  • Cool.
  • Pip
  • Group: Members
  • Posts: 72
  • Joined: 27-October 08

Posted 22 December 2008 - 07:30 AM

[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.

This post has been edited by COOLCOMPUTERGUY: 22 December 2008 - 07:39 AM


#9 User is offline   Ken Nichols 

  • Group: Members
  • Posts: 6
  • Joined: 24-November 08

Posted 23 December 2008 - 11:51 AM

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?

#10 User is offline   MHz 

  • SendToA3X v1.7
  • PipPipPipPipPipPipPip
  • Group: Members
  • Posts: 1,634
  • Joined: 02-August 04

  Posted 26 December 2008 - 06:10 PM

View PostKen Nichols, on Dec 24 2008, 03:51 AM, said:

...
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.

#11 User is offline   Geej 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 572
  • Joined: 01-January 08
  • OS:XP Pro x86
  • Country: Country Flag

Posted 28 December 2008 - 08:11 PM

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.
[attachment=24459:DVDTray.au3]
#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
		endif

If $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')
	EndSwitch
	
Endif

	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)uninstaller
I just post the DVDTray.au3 code part as I'm loss how to get it display the help info by doubling clicking DVDtray.exe

This post has been edited by Geej: 29 December 2008 - 06:36 PM


#12 User is offline   MHz 

  • SendToA3X v1.7
  • PipPipPipPipPipPipPip
  • Group: Members
  • Posts: 1,634
  • Joined: 02-August 04

Posted 30 December 2008 - 09:58 AM

View PostGeej, on Dec 29 2008, 12:11 PM, said:

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')
	EndSwitch
Else
	_DisplayHELP()
Endif
 

And 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()
	EndSwitch
Else
	_DisplayHELP()
Endif
 


:)

#13 User is offline   Geej 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 572
  • Joined: 01-January 08
  • OS:XP Pro x86
  • Country: Country Flag

Posted 31 December 2008 - 11:01 AM

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()
EndSwitch
Else
_DisplayHELP()

Thanks again & have a happy new year. :hello:

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy