[jawsscripts] Re: Piping a string variable to a txt file as part of a script

  • From: "Paul Magill" <magills@xxxxxxxxxxx>
  • To: <jawsscripts@xxxxxxxxxxxxx>
  • Date: Fri, 29 Jan 2010 00:01:50 +1100

Hi Logan,

Rather long, and a little rushed due to limited time.  If there any errors, 
someone will point them out.

2 script examples down further.

In each example, it is presumes that you have inserted all the functions &
they are included in your code in the order of my original post, & have
compiled ok.

Important note: re file names under windows. a file name may not contain 
certain characters such as a colon. In my quick testing I used as a filename 
"10:25.txt", but windows will not accept the colon, so it failed.  When you 
set up your script to generate the filename, make sure that Windows will 
accept that name regarding any punctuation characters it contains.  An easy 
way to do this is in Windows explorer. Simply try to rename a file, F2, with 
your proposed filename, if it contains a Windows prohibited character, 
Windows explorer will let you know.

Note 2: You will need not only to tell Jaws the filename, but the path to 
the file.  If you provide only the filename, depending on Windows, it will 
be written to the the root folder, C:\, or the last folder used.
*** Jaws requires the backslashes in filenames when entered as constants, to 
be doubled. example to be read by character:
"C:\\TestFolder\\10-25.txt"
here I have used a dash instead of the colon generated by default by 
SysGetTime.
To add the path in your scripts you can do something like this:
let TimeNow = "c:\\TestFolder\\" + ; construct the time & file extension
this will be clearer when reading below...


To do it the way you originally intended:

; the user has hit the key combo for the input box, so this part needs to be
done before the box displays, as no processing can be done between the box
displaying and the box closing.

; the time has been formed and loaded into a string variable, for example
called TimeNow:
let TimeNow = "10-25.txt"
; you may have manipulated SysGetTime to load the time into the variable,
and added the file extension.

; there is no text to write at this point, so the string variable would need
to be nul.
 let TextToWrite = ""

; create the file
WriteFileFromString (TextToWrite, TimeNow)

; the user has entered the text & hit enter, so you have ensured the string
variable TextToWrite now contains the info to save.
; so append the text to the same file as created above

AppendStringToFile (TextToWrite, TimeNow)

; you may later append further text to the same file by placing the new text
in the variable TextToWrite then use  the same line above, ensuring that the
same file name is contained by the variable TimeNow.
; Note, these variable names are only examples

; so your script to get the user input and save the info might look
something like:
Script SaveUserInput ()
VAR
STRING TimeNow,
STRING TextToWrite

let TimeNow = ; collect current time & add file extension
 let TextToWrite = ""
WriteFileFromString (TextToWrite, TimeNow) ; creates an empty file
InputBox ("Please enter your info", "Enter info ", TextToWrite)
AppendStringToFile (TextToWrite, TimeNow)
EndScript

Unless there is a reason for seperately creating and appending to the file,
I think it may be more efficient to do it in one step as below
Script SaveUserInput ()
VAR
STRING TimeNow,
STRING TextToWrite

InputBox ("Please enter your info", "Enter info ", TextToWrite)
; the user has hit enter & the input box exits
let TimeNow = ; collect current time & add file extension
WriteFileFromString (TextToWrite, TimeNow) ; creates the file with the
entered text as its contents.
EndScript

* to read back the info in the file, use the read text function

To do this in another script, you must use a global variable for the file 
name, so it can hold the same filename when run

Script ReadInfo ()
var
string InfoInFile


let InfoInFile = ReadStringFromFile  (TimeNow)
; the variable InfoInFile will then contain the text in the file
*** Note that the variable TimeNow must be a global in all scripts using it 
for this to work.
End Script


Regards,
Paul from Aust

----- Original Message ----- 
From: "Logan McMullen" <loganmcmullen@xxxxxxxxxxxxxxx>
a script


Hi Paul,

Thanks for this code..

Could you quickly(if possible) expand on the comments included in the source
code as to how I might use these functions?

I get the general idea but wondered if you, or someone else) might be able
to give me a brief example of using these functions.

Cheers

Logan.

-----Original Message-----
From: jawsscripts-bounce@xxxxxxxxxxxxx
[mailto:jawsscripts-bounce@xxxxxxxxxxxxx] On Behalf Of Paul Magill
Sent: Tuesday, 19 January 2010 11:44 p.m.
To: jawsscripts@xxxxxxxxxxxxx
Subject: [jawsscripts] Re: Piping a string variable to a txt file as part of
a script

Hi Logan,

- this is not as straight forward as it might seem.  There are no FS built
in functions that write to text type files other than the INI functions,
such as IniWriteString etc.

These do not provide for appending, other than creating additional keys etc.

Fortunately, especially for me, Jamal Masrui provided to the list, a set of
functions that do that task very well.

Below are those functions, only slightly modified to suit my needs.

* The last one is what you are looking for.  It, and the other 2 file
accessing functions need the first function. I included the other 2 as you
may need them at some time.

Note 1: The read function reads the entire file into a string variable, &
the Write function writes a file which will contain only the contents of the

string variable passed to it.  i e. anything already in the file will be
overwritten. The append function adds to any text already in the file, but
the file must already exist.
Note 2: you need to look after any linebreaks that you may need, as none are

provided by the functions. - this is very useful in most situations


Object Function ObjectCreate (string S_Object)

Var

Object o_return

Let o_return =CreateObjectEx (s_object, True)

;SayString ("first")

If !o_return Then

Let o_return =CreateObjectEx(s_object, False)

;SayString ("second")

EndIf

If !o_return Then

Let o_return =GetObject(s_object)

;SayString ("third")

EndIf

Return o_return

EndFunction



String Function ReadStringFromFile (string S_File)

Var

Object Null,

Object o_system,

Object o_file,

String s_return

Let o_system =ObjectCreate("Scripting.FilesystemObject")

Let o_file =o_system.OpenTextFile(s_file, 1, 0)

Let s_return =o_file.ReadAll()

o_file.close()

Let o_file =Null

Let o_system =Null

Return s_return

EndFunction



Int Function WriteFileFromString (string S_Text, string S_File)

Var

Object Null,

Object o_system,

Object o_file

Let o_system =ObjectCreate("Scripting.FilesystemObject")

Let o_file =o_system.CreateTextFile(s_file, 1, 0)

o_file.write(s_text)

o_file.close()

Let o_file =Null

Let o_system =Null

EndFunction





Int Function AppendStringToFile (string S_Text, string S_File)

; *** this function *ONLY* appends to already existing files. use
WriteFileFromString to create the file

; each append adds to the previous record without line breaks etc

Var

Object Null,

Object o_system,

Object o_file

Let o_system =ObjectCreate("Scripting.FilesystemObject")

Let o_file =o_system.OpenTextFile (s_file, 8, 0)

o_file.write (s_text)

o_file.close()

Let o_file =Null

Let o_system =Null

EndFunction




----- Original Message ----- 
From: "Logan McMullen" <loganmcmullen@xxxxxxxxxxxxxxx>


Hi everyone,

I'd like to use an input box to get some content from a user and to then
have the variable that is returned by the box save/piped to a txt file that
the script also creates.

The process would go like this(** note keystrokes are not what I'm using in
the actual script)

1. User hits ctrl+p and gets presented  with the  input box
1(a) A text file with a filename  of the current system time is created.
2. User inputs  content(text) in the box and hits enter(string variable is
then created by the input box
3.  String variable is appended to a txt file that has been created.

I have the time variable already sorted and could use a batch file to create
the txt file  but would rather use all script functions to achieve this if
possible to remove the need for the batch file to be on the users machine.

Thoughts appreciated.

Logan.

__________
Visit and contribute to The JAWS Script Repository http://jawsscripts.com

View the list's information and change your settings at
//www.freelists.org/list/jawsscripts


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4785 (20100119) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4785 (20100119) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4786 (20100119) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4788 (20100120) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4791 (20100120) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4797 (20100122) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4800 (20100123) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4808 (20100126) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4811 (20100127) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4811 (20100127) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________
Visit and contribute to The JAWS Script Repository http://jawsscripts.com

View the list's information and change your settings at
//www.freelists.org/list/jawsscripts

__________ 
Visit and contribute to The JAWS Script Repository http://jawsscripts.com

View the list's information and change your settings at 
//www.freelists.org/list/jawsscripts

Other related posts: