Jump to content

Combine two scripts?


Recommended Posts

Hey guys,

I have two scripts running on a PC. The first (index.au3) monitors to see if Internet Explorer is running, if it is, no action, if it isn't, reopen it. It also locks the mouse at the top right hand side of the screen. Although I have added a sleep to this, it runs at 50% all the time.

The second script (ierefresh.au3) monitor the time and date stamp on a particular file, in thise case c:\inetpub\ftproot\content\flashmovie.htm. If the time/date stamp changes at any time, it closes internet explorer.

I tried to combine the basic code from index to ierefresh after the

If _ProcessExists("iexplore.exe") Then

ProcessClose("iexplore.exe")

but I just get errors upon compiling.

The background on this is that it runs a html file that points to a flashmovie. I remotely upload a new flash file then a new html via FTP. The iefresh script detects the change in the html file and closes ie, the index script then reopens the html file, which is now pointing to the new flash movie.

Could anyone please help?

IERefresh.au3

Index.au3

service.au3

servicecontrol.au3

Edited by Joshjosh
Link to comment
Share on other sites

Hey guys,

I have two scripts running on a PC. The first (index.au3) monitors to see if Internet Explorer is running, if it is, no action, if it isn't, reopen it. It also locks the mouse at the top right hand side of the screen. Although I have added a sleep to this, it runs at 50% all the time.

The second script (ierefresh.au3) monitor the time and date stamp on a particular file, in thise case c:\inetpub\ftproot\content\flashmovie.htm. If the time/date stamp changes at any time, it closes internet explorer.

I tried to combine the basic code from index to ierefresh after the

If _ProcessExists("iexplore.exe") Then

ProcessClose("iexplore.exe")

but I just get errors upon compiling.

The background on this is that it runs a html file that points to a flashmovie. I remotely upload a new flash file then a new html via FTP. The iefresh script detects the change in the html file and closes ie, the index script then reopens the html file, which is now pointing to the new flash movie.

Could anyone please help?

The index.au3 runs without any trouble at 0% cpu for me (did you try rebooting your test system?)... And the IERefresh.au3 is just all broken - compiling it shows what's wrong. Be sure to end an If block with an EndIf, a While block with a WEnd, and a Do block with an Until.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Yeah I see what you mean on the iefresh, could someone please assist me with this? I'm still very new to scripting and although i know you have to start and finish a command, not sure the formatting.

What I ultimately want is for ierefresh to monitor the html file, then if it changes, close internet explorer and reopen it straight away. Figured I'd just have this on a 10 second repeat.

What I have so far:

Global $file_to_monitor = "C:\inetpub\ftproot\content\flashmovie.htm"

Global $on_start_value = CheckForFileUpdate($file_to_monitor)

While 1

Sleep(3000)

$return_date = CheckForFileUpdate($file_to_monitor)

;ConsoleWrite(@CRLF & $return_date & " - " & $on_start_value)

WEnd

If $return_date <> $on_start_value Then

_ProcessClose() ;

$on_start_value = $return_date

EndIf

Func CheckForFileUpdate($file_to_check)

$file_time = FileGetTime($file_to_check, 0)

If Not @error Then

$yyyymd = $file_time[0] & "/" & $file_time[1] & "/" & $file_time[2]

$hhmmss = $file_time[3] & ":" & $file_time[4] & ":" & $file_time[5]

Return $yyyymd & " " & $hhmmss

EndIf

Return 0

EndFunc ;==>CheckForFileUpdate

Func _ProcessClose()

Local $time1, $time2

If _ProcessExists("iexplore.exe") Then

ProcessClose("iexplore.exe")

If Not ProcessExists("iexplore.exe") Then Run(@ProgramFilesDir &"\Internet Explorer\iexplore.exe -k C:\inetpub\ftproot\Index.htm")

EndIf

$time1 = TimerInit()

Do

Sleep(3000)

Return "0"

EndFunc ;==>_ProcessClose

If someone could please help with the formatting it would be greatly appreciated.

Link to comment
Share on other sites

(...)

Do

Sleep(3000)

Return "0"

EndFunc ;==>_ProcessClose

A Do block needs to be ended with an Until. You omitted the Until here. If your sleep(3000) is intended to loop until some point in time, use Until to specify what situation to wait for. If you just want to sleep(3000) and then return "0", just delete the Do.

Hint: use the Tidy! (CTRL+T from Scite in a saved script, or Tools>Tidy.) Very very useful to correctly indent your code, and makes it obvious if you did something wrong with blocks (as happened here).

/edit: hint 2, you can use MouseTrap to lock mouse coordinates more effectively if you still want that. Be sure to check out it´s helpfile example.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Thanks for that. I will just remove the Do as all I want it to do is check if the html file is updated, if so close, then open the internet explorer. Are you able to help me out with this? Also do I need the the includes that the start of the file or does one of the commands require it?

Thanks

Link to comment
Share on other sites

Is this what you need? If so, then I think you overthought it :)

Global $filetime, $file_to_monitor = "C:\test.txt"
_doStuff()

While 1
     Sleep(10)
     If FileGetTime($file_to_monitor, 0, 1) <> $filetime Then _doStuff()
WEnd

Func _doStuff()
     $filetime = FileGetTime($file_to_monitor, 0, 1) ; <- gets filetime as string, convenient for checking for changes
     While ProcessExists("iexplore.exe") ; keeps looping until no iexplore exists, in case multiple iexplore's are opened
          ProcessClose("iexplore.exe")
          Sleep(100)
     WEnd
     Run(@ProgramFilesDir & "\Internet Explorer\iexplore.exe [url="http:///spanhttp:///spanhttp:///spanhttp:///spana%20href=span%20class=span%20class=span%20class="]www.google.com[/url]")
EndFunc

By the way: I used a file called c:\test.txt for testing purposes. But this should work just as well for your file. I also used www.google.com as page for iexplore to open up. Be sure to change the url and the filename back to your circumstances!

/edit: changed indentation to look better

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Thanks so much for your help, that runs perfectly, and so much less complex. I will add the mouse command to in order to move the mouse off the screen.

If I could ask just one huge favour, I have another sciprt that does a similar thing, monitors a file time/date, but instead of closing a process, it restarts a service called Music. This does work fine, I just wanted to make sure I didnt make an obvious errors.

#include <ServiceControl.au3>
#include <Service.au3>

Global $file_to_monitor = "C:\Inetpub\ftproot\Music\Songs\playlist.txt"
Global $on_start_value = CheckForFileUpdate($file_to_monitor)

While 1 
    Sleep(1000)
    $return_date = CheckForFileUpdate($file_to_monitor)
   ;ConsoleWrite(@CRLF & $return_date & " - " & $on_start_value)
    If $return_date <> $on_start_value Then
        _RestartServices(); restarts service 'Music', could have some error checking here
        $on_start_value = $return_date; resets the on_start_value to newest date / time value
    EndIf
WEnd


Func CheckForFileUpdate($file_to_check)
    $file_time = FileGetTime($file_to_check, 0)
    If Not @error Then
        $yyyymd = $file_time[0] & "/" & $file_time[1] & "/" & $file_time[2]
        $hhmmss = $file_time[3] & ":" & $file_time[4] & ":" & $file_time[5]
        Return $yyyymd & " " & $hhmmss
    EndIf
    Return 0
EndFunc  ;==>CheckForFileUpdate
Func _RestartServices()
    Local $time1, $time2
    If _ServiceExists ("", "Music") = 1 Then
        If _ServGetState("Music") = "Running"  Then
            _StopService ("", "Music")
        EndIf
        $time1 = TimerInit()
        Do
            Sleep(1000)
            If _ServGetState("Music") = "Stopped"  Then ExitLoop
        Until TimerDiff($time1) > 20000
        _StartService("", "Music")
        $time2 = TimerInit()
        Do
            Sleep(1000)
            If _ServGetState("Music") = "Running"  Then Return "1" 
        Until TimerDiff($time2) > 20000; check timeout
    EndIf
    Return "0" 
EndFunc  ;==>_RestartServices

Thanks again!

Edited by Joshjosh
Link to comment
Share on other sites

Well, there's this saying: if it ain't broken, don't fix it :)

I don't completely agree with that view when programming is concerned. I would like to offer the following: if it ain't broken, then for heaven's sake keep a working copy.

For practise in efficiency you might want to turn this second script of yours into something looking more like the script I wrote for you. I'm not gonna sing twice for one cent :P Anyway, it should only be a small job, and if you have any trouble in the process, be sure to ask ofcourse! (Btw, in such small scripts my preferred way to improve is to just start over.)

/edit: Here are two small suggestions though:

1 - don't use elaborate date conversion yourself, use the already existing functions as much as possible to preserve eventual compatibility. (Check my example.) It also saves you a complete function :P

2 - prevent ExitLoop as much as possible - it is ugly (imho). Put or [condition for the exitloop] in the until line instead. It saves you a line and is clearer.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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