Sign in to follow this  
Followers 0
paul1149

Can't get script to repeat

8 posts in this topic

I've got a script that I use to clean up and capitalize file names. It's useful for downloaded videos.

The structure is basically:

  • various declarations
  • call a GUI dialog that asks where the file is and gives the option to exit: _Dialog()
  • define the various string functions: Func DoStuff ()
  • define the GUI: _Dialog ()
  • define GUI functions: OnYes() etc...
The script currently needs to be fired for each file. I would like to query the user with the dialog again, and hen either fire the script again or exit. But wherever I put another _Dialog() call, the dialog does appear but it's inert, with no functionality. The script is paused and must be manually exited.

Can someone tell me generally what I'm missing here? I can post the code if necessary.

Thanks,

p.

Share this post


Link to post
Share on other sites



posting your code would help in finding the problem..

Here you go. Thanks.

#include <StringTitleCase.au3>

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.0.0
 Author:        pb  5/09

 Script Function:
1. select target filename in file manager.
2. fire script
4. script will invoke Title Case and remove underscores,
    doublespaces, flashgot verbiage from filename
5.  Note: If OnNo is chosen, previous active window might need to be the file manager.

#ce ----------------------------------------------------------------------------
    
; Script Start - Add your code below here
    AutoItSetOption("sendkeydelay", 10)
    AutoItSetOption("WinTitleMatchMode", 2)
    
;where is file? Use ONE of the two following locations.

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)

Global $ExitID 
Global $Location
Local $x
Local $Title_In

_Dialog()

Func DoStuff ()
    WinWaitActive($Location)
    Send("{F2}")
    Send("^x")  
    $x = ClipGet()
    $x = StringReplace ($x, "_", " ")
    $x = StringReplace ($x, " attachment filename video", "")
    $x = StringReplace ($x, " videoplayback", "")
    $x = StringReplace ($x, "  ", " ")
    $Title_In = StringLower ($x)
    $x=_StringTitleCase($Title_In)

ClipPut($x)
    Send("^v")
    Send("{ENTER}")
    Send("{Down}")

;_Dialog ()
EndFunc


Func _Dialog()
    Local $YesID, $NoID

    GUICreate("Custom Msgbox", 210, 80)
    GUICtrlCreateLabel("Is File on Desktop?", 10, 10)
    $YesID = GUICtrlCreateButton("Yes", 10, 50, 50, 20)
    GUICtrlSetOnEvent($YesID, "OnYes")
    $NoID = GUICtrlCreateButton("No", 80, 50, 50, 20)
    GUICtrlSetOnEvent($NoID, "OnNo")
    $ExitID = GUICtrlCreateButton("Exit", 150, 50, 50, 20)
    GUICtrlSetOnEvent($ExitID, "OnExit")

    GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")
    
    ; Set accelerators for y and n and esc
    Dim $AccelKeys[3][2]=[["y", $YesID], ["n", $NoID], ["{ESC}", $ExitID]]

    GUISetAccelerators($AccelKeys)

    GUISetState()  ; display the GUI

    While 1
        Sleep(1000)
    WEnd
EndFunc   ;==>_Main

;--------------- Functions ---------------
Func OnYes()
    $Location = "Desktop"
    GUIDelete();
    DoStuff()
    Exit
EndFunc   ;==>OnYes

Func OnNo()
    $Location = "C:\"
    GUIDelete();
    DoStuff()
    Exit
EndFunc   ;==>OnNo

Func OnExit()
    Exit
    
EndFunc   ;==>OnExit

Exit

Share this post


Link to post
Share on other sites

paul1149,

The main problem was that you had your While...WEnd loop in the wrong place. You need it the main part of the script so that all your functions return there - having it in a function means that the function never ends and can cause problems, as you discovered. :D

Another tip - if you want to reuse a GUI, try creating it just the once and then Hide/Show it as required. :

Take a look at this - which I think does what you want:

#include <StringTitleCase.au3>

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.0.0
 Author:        pb  5/09

 Script Function:
1. select target filename in file manager.
2. fire script
4. script will invoke Title Case and remove underscores,
    doublespaces, flashgot verbiage from filename
5.  Note: If OnNo is chosen, previous active window might need to be the file manager.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
    AutoItSetOption("sendkeydelay", 10)
    AutoItSetOption("WinTitleMatchMode", 2)

;where is file? Use ONE of the two following locations.

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)

Global $hGUI
Global $ExitID
Global $Location
Local $x ; These 2 are Global because of their position - all you do with Local is get Au3Check to fire an error
Local $Title_In

; Create the GUI just the once and then hide/show it as necessary
_Dialog() 

; This is where you want the loop!!!!!
While 1
    Sleep(10)
WEnd

Func DoStuff ()

    MsgBox(0,"Function", "Doing  Stuff")
    ; put your code here

EndFunc


Func _Dialog()
    Local $YesID, $NoID

    $hGUI = GUICreate("Custom Msgbox", 210, 80)
    GUICtrlCreateLabel("Is File on Desktop?", 10, 10)
    $YesID = GUICtrlCreateButton("Yes", 10, 50, 50, 20)
    GUICtrlSetOnEvent($YesID, "OnYes")
    $NoID = GUICtrlCreateButton("No", 80, 50, 50, 20)
    GUICtrlSetOnEvent($NoID, "OnNo")
    $ExitID = GUICtrlCreateButton("Exit", 150, 50, 50, 20)
    GUICtrlSetOnEvent($ExitID, "OnExit")

    GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

    ; Set accelerators for y and n and esc
    Dim $AccelKeys[3][2]=[["y", $YesID], ["n", $NoID], ["{ESC}", $ExitID]]

    GUISetAccelerators($AccelKeys)

    GUISetState()  ; display the GUI

    ; You do NOT want the loop here!!!!!!!!!!!!!!!!!
    ;While 1
    ;    Sleep(1000)
    ;WEnd

EndFunc   ;==>_Main

;--------------- Functions ---------------
Func OnYes()
    $Location = "Desktop"
    ; Hide the GUI, do not delete it
    GUISetState(@SW_HIDE, $hGUI) ;GUIDelete();
    DoStuff()
    ; And now show it again rather than create a new one
    GUISetState(@SW_SHOW, $hGUI) ;Exit

EndFunc   ;==>OnYes

Func OnNo()
    $Location = "C:\"
    GUISetState(@SW_HIDE, $hGUI) ;GUIDelete();
    DoStuff()
    GUISetState(@SW_SHOW, $hGUI) ;Exit
EndFunc   ;==>OnNo

Func OnExit()
    Exit
EndFunc   ;==>OnExit

I hope everything is clear - please ask if not. :huggles:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba, that's working great. Thanks much. Also, thanks for the very clear comments you added, to clue me into what you were doing. The Show/Hide option seems a better way to do it than to recreate each time. May I ask, what exactly does the While statement actually do here?

Thanks again,

p.

Share this post


Link to post
Share on other sites

paul1149,

Have you tried removing it and running the script? :huggles:

That is the idle loop that keeps the whole script active - if you do not have it, the script just........ends. :D

While on the subject of While...WEnd idle loops, in OnEvent mode it is vital to put a Sleep(10) in the loop or your CPU starts running at 100%. If you are using MessageLoop mode, then the GUIGetMsg() command sorts that out for you automatically while waiting for messages to arrive.

Look in the Help file under <GUI Reference> for more details.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0