Jump to content

Just looking to improve


Recommended Posts

Im a virgin to this stuff so I just wrote this small script to open and close my disk drives. I was just looking for advice on how to make it a bit cleaner or more effective with less code. Any advice would be great. Thanks!

CODE
TraySetIcon('Shell32.dll', 8)

Opt('TrayMenuMode', 1)

$d = TrayCreateMenu('D:')

$on_d = TrayCreateItem('Open', $d)

$off_d = TrayCreateItem('Close', $d)

$e = TrayCreateMenu('E:')

$on_e = TrayCreateItem('Open', $e)

$off_e = TrayCreateItem('Close', $e)

$exit = TrayCreateItem('Exit')

While 1

$msg = TrayGetMsg()

Select

Case $msg = 0

ContinueLoop

Case $msg = $on_d

CDTray('D:', 'open')

TrayTip('Drive D', 'Opened', 1500)

$msg = 0

ContinueLoop

Case $msg = $off_d

CDTray('D:', 'closed')

TrayTip('Drive D', 'Closed', 1500)

$msg = 0

ContinueLoop

Case $msg = $on_e

CDTray('E:', 'open')

TrayTip('Drive E', 'Opened', 1500)

$msg = 0

ContinueLoop

Case $msg = $off_e

CDTray('E:', 'closed')

TrayTip('Drive E', 'Closed', 1500)

$msg = 0

ContinueLoop

Case $msg = $exit

Exit

EndSelect

WEnd

Link to comment
Share on other sites

I may be completely wrong on this, but I don't believe that you need to set $msg to 0 after each event, or use ContinueLoop.

[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

Link to comment
Share on other sites

Thank you!

CODE
TraySetIcon('Shell32.dll', 8)

Opt('TrayMenuMode', 1)

$d = TrayCreateMenu('D:')

$on_d = TrayCreateItem('Open', $d)

$off_d = TrayCreateItem('Close', $d)

$e = TrayCreateMenu('E:')

$on_e = TrayCreateItem('Open', $e)

$off_e = TrayCreateItem('Close', $e)

$exit = TrayCreateItem('Exit')

While 1

$msg = TrayGetMsg()

Select

Case $msg = 0

ContinueLoop

Case $msg = $on_d

CDTray('D:', 'open')

TrayTip('Drive D', 'Opened', 1500)

Case $msg = $off_d

CDTray('D:', 'closed')

TrayTip('Drive D', 'Closed', 1500)

Case $msg = $on_e

CDTray('E:', 'open')

TrayTip('Drive E', 'Opened', 1500)

Case $msg = $off_e

CDTray('E:', 'closed')

TrayTip('Drive E', 'Closed', 1500)

Case $msg = $exit

Exit

EndSelect

WEnd

Also, it would not let me edit my first post :)

Edited by JFpwn
Link to comment
Share on other sites

You could do it like this - also you might want to read the available CD Drives first and then create an array of those and then make the menu.

TraySetIcon('Shell32.dll', 8)
Opt('TrayMenuMode', 1)
$d = TrayCreateMenu('D:')
$on_d = TrayCreateItem('Open', $d)
$off_d = TrayCreateItem('Close', $d)
$e = TrayCreateMenu('E:')
$on_e = TrayCreateItem('Open', $e)
$off_e = TrayCreateItem('Close', $e)
$exit = TrayCreateItem('Exit')

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = $on_d
            $test = CDTray('D:', 'open')
            If $test = 0 Then
                MsgBox('', 'ERROR Opened - Drive D', 'Can NOT open drive')
            Else
                TrayTip('Drive D', 'Opened', 1500)
            EndIf
            
        Case $msg = $off_d
            $test = CDTray('D:', 'closed')
            If $test = 0 Then
                MsgBox('', 'ERROR Closed - Drive D', 'Can NOT close drive')
            Else
                TrayTip('Drive D', 'Closed', 1500)
            EndIf
            
        Case $msg = $on_e
            $test = CDTray('E:', 'open')
            If $test = 0 Then
                MsgBox('', 'ERROR Opened - Drive E', 'Can NOT open drive')
            Else
                $test = TrayTip('Drive E', 'Opened', 1500)
            EndIf

        Case $msg = $off_e
            $test = CDTray('E:', 'closed')
            If $test = 0 Then
                MsgBox('', 'ERROR Closed - Drive E', 'Can NOT close drive')
            Else
                TrayTip('Drive E', 'Closed', 1500)
            EndIf
            
            
        Case $msg = $exit
            Exit
    EndSelect
WEnd

2¢

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

I was looking at arrays but I fear thats to much for me right now. The only thing I have ever coded is HTML, CSS/DHTML, and VERY limited Javascript. Thanks for the input!

try this - untested on more than one drive as that is all i have on this laptop

TraySetIcon('Shell32.dll', 8)
Opt('TrayMenuMode', 1)

$types = DriveGetDrive("CDROM")  

Dim $drive[$types[0] + 1]
Dim $driveOpen[$types[0] + 1]
Dim $driveClose[$types[0] + 1]


For $i = 1 To $types[0] 
    $drive[$types[$i]] = TrayCreateMenu($types[$i])
    $driveOpen[$types[$i]] = TrayCreateItem('Open', $drive[$types[$i]])
    $driveClose[$types[$i]] = TrayCreateItem('Close', $drive[$types[$i]])
Next

$exit = TrayCreateItem('Exit')

While 1
    
    For $x = 1 To $types[0]
        $msg = TrayGetMsg()
        Select
            Case $msg = $driveOpen[$types[$x]]
                $test = CDTray($types[$x], 'open')
                If $test = 0 Then
                    MsgBox('', 'ERROR Opened - Drive ' & $types[$x], 'Can NOT open drive')
                Else
                    TrayTip('Drive ' & $types[$x], 'Opened', 1500)
                EndIf
                
            Case $msg = $driveClose[$types[$x]]
                $test = CDTray($types[$x], 'closed')
                If $test = 0 Then
                    MsgBox('', 'ERROR Closed - Drive ' & $types[$x], 'Can NOT close drive')
                Else
                    TrayTip('Drive ' & $types[$x], 'Closed', 1500)
                EndIf
            
            Case $msg = $exit
                Exit
        EndSelect
    Next
    
WEnd

2¢

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Not quite...

Your script writes all the open and close var's to one open and close TrayItem.

CODE
F: -> Open ;opens all drives

F: -> Close ;closes all drives

But the weird thing is it does it one-by-one.

Here is the code i was working on.... not very far... and it doesnt work lol

TraySetIcon('Shell32.dll', 8)
Opt('TrayMenuMode', 0)
Dim $drive, $a, $p, $c, $o
$o=1
$c=2

$drive = DriveGetDrive( "cdrom" )
For $a = 1 to $drive[0]
    MsgBox(0, 'Drive','Adding '&$drive[$a])
    $s = TrayCreateMenu($drive[$a])
    $o[$a] = TrayCreateItem('Open', $s)
    $c[$a] = TrayCreateItem('Close', $s)
Next

Sleep(10000)
Edited by JFpwn
Link to comment
Share on other sites

Array handling was one of the things I liked best about AutoIt (once I learned them). Here is your script, with a 1D array to hold the drive letters, and a 2D array to hold the control IDs of the TrayMenu items:

TraySetIcon('Shell32.dll', 8)
Opt('TrayMenuMode', 1)

Dim $avDrives = DriveGetDrive("cdrom")
Dim $avOpenClose[$avDrives[0] + 1][2]
$avOpenClose[0][0] = $avDrives[0]
$OpenMenu = TrayCreateMenu("CD Drive Open")
$CloseMenu = TrayCreateMenu("CD Drive Close")
$Exit = TrayCreateItem("Exit")

For $d = 1 To $avDrives[0]
    $avOpenClose[$d][0] = TrayCreateItem('Open ' & $avDrives[$d], $OpenMenu)
    $avOpenClose[$d][1] = TrayCreateItem('Close ' & $avDrives[$d], $CloseMenu)
Next

While 1
    $TrayMsg = TrayGetMsg()
    Switch $TrayMsg
        Case $Exit
            Exit
        Case Else
            For $d = 1 To $avOpenClose[0][0]
                If $TrayMsg = $avOpenClose[$d][0] Then
                    CDTray($avDrives[$d], "Open")
                ElseIf $TrayMsg = $avOpenClose[$d][1] Then
                    CDTray($avDrives[$d], "close")
                EndIf
            Next
    EndSwitch
WEnd

Say it with me: "Arrays are our friends, arrays are our friends, arrays ..."

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Maybe this could give you some direction....

From Welcome to Autoit 1-2-3

Posted Image

; Script Format Demonstration.

; 1. Place required include files.
; 2. Set Autoit options, as needed.
; 3. Declare Variables.
; 4. Set Desired Hot Keys.
; 5. Create the GUI.
; 6. Display the GUI.
; 7. Set/Update control information.
; 8. Set the Tray Menu
; 9. Start the Loop, and "Listen" for a message.
; 10. Create the Functions.


; here is an Example


; 1. Includes
#include <GuiConstants.au3>
#include <String.au3>

; 2. Autoit Options
Opt("GUICloseOnESC", 1)         
;1 = ESC  closes script, 0 = ESC won't close.
opt("TrayMenuMode", 1)   
; Default tray menu items (Script Paused/Exit) will not be shown.
opt("TrayOnEventMode", 1)
;TraySetClick (16); right click

; 3. Declare Variables use, Dim, Global or Local.
Dim $Text, $Cloudy = 1
; see help for more info.

; 4. Set Hot Keys
HotKeySet("{F1}", "About_GUI")
; when F1 is pressed, goto function "About_GUI".

; 5. Create the GUI
$Window = GUICreate("My Weather Program")
$combo = GUICtrlCreateCombo("", 120, 50, 150, 20)
$button = GUICtrlCreateButton("Press here for Daily Weather Report", 100, 300, 200, 40)

; 6. Display the GUI
GUISetState()

; 7. Set the control information

; this will split the string called $days, by each comma.
$days = StringSplit("Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday", ",")
; the return is an array $days[0] is 7, the total
; $days[1] contains "Sunday" ... $days[7] contains "Saturday"

; this will use a for/next loop to set the $combo information with the $days.
For $x = 1 to $days[0]
    GUICtrlSetData( $combo, $days[$x])
Next

; 8. Set the tray menu
    $About_tray = TrayCreateItem ("About Weather")
    TrayItemSetOnEvent (-1, "About_GUI")
    TrayCreateItem ("")
    $exit_tray = TrayCreateItem ("Exit Weather")
    TrayItemSetOnEvent (-1, "Set_Exit")
    
    TraySetState ()

; 9. Start the loop
While 1
    ; "Listen" for the message
    $message = GUIGetMsg()
    
    If $message = $GUI_EVENT_CLOSE Then Set_Exit()
    
    If $message = $button Then
        ; read the selected day in the combo
        $Text = GUICtrlRead( $combo )
        ; call the function with the info
        Day_Function($Text)
    EndIf

WEnd

; 10. Create the Functions 

Func Day_Function($Text)
    If $Text = "" Then Return
    ; randomly choose amount of rain
    $rain = Random(5, 100, 1)
    ; call another function for clouds
    Cloud_Function()
    If $Cloudy = 1 Then
        $Clouds = "Cloudy"
    Else
        $Clouds = "Clear"
    EndIf
    ; show the weather information.
    MsgBox(64, "Your Weather","On " & $Text & " You can expect,   " & @CRLF & $Clouds & " skies in the afternoon and evening   " & @CRLF & _
    "The chance of rain is approximately " & $rain & " percent   ")
EndFunc

Func Cloud_Function()
    If $Cloudy = 1 Then
        $Cloudy = 2
        ; leave this function now
        Return
    EndIf
    If $Cloudy = 2 Then
        $Cloudy = 1
        Return
    EndIf
EndFunc

Func About_GUI()
    MsgBox(64, "Welcome!", " this is your Weather Service      ")
EndFunc    

Func Set_Exit()
    MsgBox(0,"","Good-Bye!",1)
    Exit
EndFunc
    
; All of these functions are UDF's, User Defined Functions

; When you have completed a Script use Tidy under "Tools" above to "clean" it up.
; Then make a comment at the top, like this

#cs ===============================================================================
    *AutoIt 1-2-3     ver 1.0.1 - 02.01.2006
    Autor:             Valuater
    E-mail:            XPCleanMenu@aol.com
    Language:       English
    OSystem:         Windows Xp
    Requirements:     Legal copy of Microsoft Windows Xp
    Construction:     AutoIt 3.1.1+, SciTE 1.64
    Features:         Automated Program, Reads Text, Runs Scripts, etc
    - Use = Tutorials, Help Files, Installers, Presentations, etc
    - ...
    
    Thanks to all, Enjoy...
#ce ===============================================================================

8)

NEWHeader1.png

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