Sign in to follow this  
Followers 0
egalvez

Script works only if run manually

7 posts in this topic

Hello. I wrote an AutoIt script using version 3.0.102. The script opens for reading a text file, randomly chooses a paragraph, assigns the text of the paragraph to a variable, copies an empty html file and names it with a certain name, writes the paragraph to the blank html file and saves it with a certain name, then calls IE to display the html file containing the paragraph. It works great if I double-click the exe file in the Windows Explorer or if I run the exe from Start - Run - Browse. But if I run the exe using KeyText (a shareware keyboard macro utility) or if I run the exe from Peter's Ultimate Alarm Clock (a freeware alarm utility that can open files and run programs on a schedule), the exe doesn't work right. Instead of opening an HTML file with the paragraph in it, it opens an empty HTML file, and I get no error messages. The exe works in that it opens the HTML file, but the HTML file doesn't have anything it. What could cause this anomaly, i.e., if I manually run the exe it works great, but if I use another program to run the exe, it doesn't work right. Here's my code:

;Open text file containing 38 paragraphs, for reading
$file = FileOpen ( "C:\Program Files\AutoIt3\TheSufferingsOfChrist\chapter.txt", 0 )
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open chapter.txt file.")
    Exit
EndIf
;Read all 30755 characters of chapter and assign to $chars variable
$chars = FileRead ( "chapter.txt", 30755 )
$randomNum = int(Random (1,38)) ; Randomly get any one of 38 paragraphs in chapter
$location1 = StringInStr($chars, "     ", 0, $randomNum)   ; Find random occurrence of five spaces (beginning of a paragraph)
$location2 = StringInStr($chars, "     ", 0, $randomNum + 1)   ; Find next occurrence of five spaces (beginning of a paragraph)
$myParagraph = StringMid($chars,$location1,$location2-$location1)
FileClose ($file)
$htmlPt1 = "<html><head><title>The Sufferings of Christ</title></head><body><font size=5 face=arial>"
$htmlPt2 = $myParagraph
$htmlPt3 = "</font></body></html>"
$htmlcode = $htmlPt1 & $htmlPt2 & $htmlPt3
;Delete HTML file containing paragraph from previous instance of running this app
FileDelete("C:\Program Files\AutoIt3\TheSufferingsOfChrist\chap_html.html")
;Copy empty HTML file to chap_html.html file which is about to be written to
FileCopy("C:\Program Files\AutoIt3\TheSufferingsOfChrist\empty_chapter.html", "C:\Program Files\AutoIt3\TheSufferingsOfChrist\chap_html.html")
$htmlfile = FileOpen ( "C:\Program Files\AutoIt3\TheSufferingsOfChrist\chap_html.html", 1 )
; Check if file opened for reading OK
If $htmlfile = -1 Then
    MsgBox(0, "Error", "Unable to open chap_html.html file.")
    Exit
EndIf
;Write paragraph with HTML code to text file just opened
FileWrite($htmlfile, $htmlcode)
FileClose ($htmlfile)
$browserPath = "c:\Program Files\Internet Explorer\iexplore.exe"
;Open HTML file in browser so I can see the paragraph that's been randomly chosen
Run($browserPath & " c:\Program Files\AutoIt3\TheSufferingsOfChrist\chap_html.html", "", @SW_MAXIMIZE)

Any ideas? Thank you.

Share this post


Link to post
Share on other sites

Just a stab in the dark, but can you set the "Startup Folder" in these other apps? If so, try setting it to the same path as the EXE. Otherwsie, try setup a shortcut to the EXE (which will allow you to specify the Startup Folder), and launch the shortcut from your scheduling apps.

HTH :idiot:

Share this post


Link to post
Share on other sites

I don't got enough time to look it all up or review the script but since it loads the exe to a different directory everytime you access a file, has it access it @ScriptDir & "file" otherwise it will be requesting a file from the working directory that doesn't exist.

Share this post


Link to post
Share on other sites

;Read all 30755 characters of chapter and assign to $chars variable

$chars = FileRead ( "chapter.txt", 30755 )

Any ideas?  Thank you.

<{POST_SNAPBACK}>

Is the html file really empty, or is it that the browser window is empty? My guess is if you open the html in an editor, it will contain html code. I believe the problem is one with the default directory as you are not specifying a path to the data file when it is read. Since you already have a file handle, which includes the path, for the data file, try changing the FileRead line: $chars = FileRead($file, 30755).

You might also look at the FileGetSize function, and use it to determine the file size instead of hard coding 30755.


Phillip

Share this post


Link to post
Share on other sites

I wonder where such script may be useful and the only thing I can imagine is spam letters. Sometimes I get letters with random texts and links inside :E

Share this post


Link to post
Share on other sites

I had the same problem and ended up using the following "RunWait" code.

I noticed you use "Run".

RunWait("C:\Program Files\Internet Explorer\Iexplore.exe C:\Documents and Settings\Userid\My Documents\AutoItScripts\LucAu3\Heritage\Attendance.html", "",)

Replace *.html path to where you keep/write your *.html file.

RocTx

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks, all of you. And thank you, Larry. Per your observation, what worked was changing

$chars = FileRead ( "chapter.txt", 30755 )

to

$chars = FileRead ( $file, 30755 )

I wonder where such script may be useful and the only thing I can imagine is spam letters.

It's not for spamming. Rather it is just to cause a local HTML page to load in my browser periodically to remind me of something that I want to be reminded about. A paragraph is randomly selected from a chapter entitled The Sufferings of Christ from a book. It helps to understand that Christ's physical pain on the cross was hardly even felt by Him. By comparison, the anguish He was experiencing internally at the hiding of His Father's face and from bearing the weight of everyone's sins and guilt, almost completely eclipsed the physical suffering from His view. Thus, Mel Gibson's movie fell far short of depicting the nature of His sufferings.

Eric G.

Edited by egalvez

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