Page 1 of 1
name file with variable
Posted: Sat Nov 16, 2013 12:42 pm
by alberto.fre
Hello,
I'm new to programming.
My program ask the name of user
ask "what's your name?"
put it into tName
put "tName" into a field "score"
put (name & cr) into field "score"
then my program produces a file using the command
put field "score" into URL ( "file:tname")
but how can I produce a file with the name of the user (using the variable "tName")? Because the users are a lot, and I don't want that the file is overwritten
thanks
Re: name file with variable
Posted: Sat Nov 16, 2013 1:08 pm
by Klaus
Hi Alberto,
1. welcome to the forum
2. Leave out any (in)definite article in front of the object descriptor!
BAD: put xyz into A field "name of field"
Also Bad: put xyz into THE field "name of field"
Good: put XYZ into fld "name of field"
For lazy typers like me:
fld = abbreviation for field
btn = button
sb = scrollbar
grp = group
img = image
cd = card
3. Leave out the quotes around the VARIABLE!
They MUST not be in quotes! (Except in somerare cases

)
If you do, you will get the NAME of the variable instead of its content, as you have experienced here!
...
ask "what's your name?"
put it into tName
put tName into fld "score"
...
Best
Klaus
Re: name file with variable
Posted: Sat Nov 16, 2013 2:35 pm
by alberto.fre
thanks for your suggestion
the first part of the code works
but how can I produce a file with the name of the user (using the variable)?
In fact the program users are a lot, and I want generate more files with the users names
my code is
on mouseUp
put field "score" into URL ( "file:" & "name.txt")
end mouseUp
thanks
Re: name file with variable
Posted: Sat Nov 16, 2013 3:09 pm
by [-hh]
..........
Re: name file with variable
Posted: Sat Nov 16, 2013 3:37 pm
by alberto.fre
Thanks
my program is a quiz for the children (for a metacognitive study) with text, images and sound. The structure is: story -> questions (4 questions), 4 buttons for answers with suggests and feedback.
The choices of the children are written in a field "score", which then writes a text file.
Code: Select all
on mouseUp
ask "what's your name?"
put it into tName
put (tName & cr) into field "score"
put field "score" into URL ( "file:" & tName&".txt")
-- the next step is to send the file to an ftp address, but I have not yet worked out how to do and I have not yet evaluated an ftp server free
end mouseUp
Re: name file with variable
Posted: Sat Nov 16, 2013 3:55 pm
by [-hh]
..........
Re: name file with variable
Posted: Sat Nov 16, 2013 5:50 pm
by alberto.fre
Yes, thanks
1)
By the way, if you put tName &CR *into* fld "score" everything in fld "score" is overwritten
this is right. The text field is, for example,
Leonardo
1-1
2-3
3-1
4-3
etc...
because after the user name there are other buttons-response with command:
Code: Select all
on mouseUp
put ( "1-1" & cr) after field "score"
end mouseUp
2) thanks for the link
3) I tried for a long time, but I have not found a way to call a file with the name contained in a variable. But I'll try again.
thanks for everything
Re: name file with variable
Posted: Sat Nov 16, 2013 6:12 pm
by Klaus
Hi Alberto,
alberto.fre wrote:3) I tried for a long time, but I have not found a way to call a file with the name contained in a variable.
Do this:
Code: Select all
on mouseUp
ask "what's your name?"
## User canceled or did not enter anything, so we exit the handler!
if it is empty then
exit mouseUp
end if
put it into tName
## No need to set "the defaultfolder" when we can create abolute pathnames :-)
put specialFolderPath("Documents") & "/" & tName &".txt" into tPath
## "specialfolderpath()" does NOT return the trailing SLASH for that directory, so we need to add it -> & "/" & tName
## ALWAYS check! You are the developer! If the user makes any error, it is YOUR FAULT! :-D
if there is NOT file tPath then
## This way you can also see and check the pathname!
answer "No file:" & CR & tPath
exit mouseUp
end if
## File exists, we can load it:
put url("file:" & tPath) into fld "Name of your field here..."
end mouseup
Best
Klaus
Re: name file with variable
Posted: Sat Nov 16, 2013 6:20 pm
by alberto.fre
thank you!
I need time to understand all of your suggestions.
now I try
Alberto
Re: name file with variable
Posted: Sat Nov 16, 2013 6:55 pm
by alberto.fre
Ok, I probably something err.
is not sufficient to write?
Code: Select all
put field "score" into URL ( "file:" & tName".txt")
The program generates a file with the field (for me, is text field "score") into the program directory
when I try
Code: Select all
put specialFolderPath("Documents") & "/" & tName &".txt" into tPath
the program clears the field "score" and not generate any file
but I need to generate files alberto.txt, Harry.txt, Ron.txt .... etc.
thanks for your patience
Alberto
Re: name file with variable
Posted: Sat Nov 16, 2013 7:42 pm
by alberto.fre
Ciao
The code generates the file tName and doesn't generate the file with users names... why?
Code: Select all
on mouseUp
set the defaultFolder to specialFolderPath("Documents")
put "file:"& tName &".txt" into tPath
put fld "score" into URL (tPath)
end mouseup
bye
Re: name file with variable
Posted: Sat Nov 16, 2013 8:09 pm
by Klaus
Hi Alfredo,
in your script you do not fill the variable tName with content!
And when a variable is empty, you get the NAME instead!
Where is the content of the variable tName located?
A field? Or do you want to "ASK" first?
You need to put the correct content into tName somehow!
when I try
put specialFolderPath("Documents") & "/" & tName &".txt" into tPath
the program clears the field "score" and not generate any file
That line alone does not clear anything!
Please post the rest of the script!
Best
Klaus
Re: name file with variable
Posted: Sat Nov 16, 2013 8:12 pm
by alberto.fre
I solved, thanks for the advice.
the variable "tName" was empty!
In fact, the request of the user name is in another card.
I have to study well yet variables.
Thanks for your help
Re: name file with variable
Posted: Sat Nov 16, 2013 10:14 pm
by Klaus
Hi Alberto,
Gald I could help!
Check these great stacks to learn more about the basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html
Best
Klaus