PDA

View Full Version : Text saving from textField to a .txt file


safakoner
01-07-2004, 09:21 AM
Hi

I created a window included a texfield. I want to do this:
I added "save" and "open" to window menu. When I click save from the window menu this script must save the text in texfield to a .txt file. When I open same .txt file the text must load the textfield.

I can't found a doc. about textfield saving like this. Do you know something about it ?

Thanks

if (`window -q -exists ll`) deleteUI ll;
window -menuBar true -maximizeButton false -title "Test" -iconName "Test" -w 200 -h 200 ll;

menu -label "Window" -tearOff false;
menuItem -label "save";
menuItem -divider true;
menuItem -label "Open";

columnLayout -adj true;
frameLayout -label " Header" -cll false -width $fw;
rowColumnLayout -nc 1 -width 360 -cw 1 150;


textField -tx "Test" Tf_01;

setParent ..;
setParent ..;

showWindow ll;

galactor
01-07-2004, 11:34 AM
First you have to attach the command "saveText" to your save button: menuItem -label "save" -c saveText;
Then when pressed, it would write the content of the textField to you TempWindows folder.

global proc saveText()
{
string $textToBeSaved = `textField -q -tx Tf_01`;

$fileName = (`internalVar -userTmpDir` + "textField.txt");
$fileId=`fopen $fileName "w"`;
fwrite $fileId $textToBeSaved;
fclose $fileId;
}

:: Galactor ::

safakoner
01-07-2004, 12:05 PM
Thank You. This is very impressive.

I have four questions about it.

1. Can I load the .txt file with open from menu ?
2. Can I define custom file name ?
3. Can I define custom folder path ?
4. Can I use so many textfield ?

Do you know simple doc. about these operations ? I will create so many textfield in same script. I will need these operations.

Thanks

mhovland
01-07-2004, 01:19 PM
1. Can I load the .txt file with open from menu ?

Yes. The method would be:

use fopen to open the file for reading.
use fgetline to get each line of the file and add it into the textfield.

2. Can I define custom file name ?

Yes, just add a text filed to the UI asking for a file name, at save time, build the file name by querying the value of the text field.,

3. Can I define custom folder path ?

Sure, I can post the code I use for file dialog boxes that allow the user to pick a location (folder) to save to on their machine, when I get to work.

4. Can I use so many textfield ?

Use as many as you like :)

safakoner
01-07-2004, 03:15 PM
Thank you for your tips :bounce:

mhovland
01-07-2004, 05:25 PM
Here is the portion of a much larger exporter of mine that prompts the user for a save location and a file name. I build the information I need to write the file out from the input I recieve.

You should be able to figure out how this is working by going through the code. If you have any questions, let me know


global proc mhFileOut()
{

global string $curObjectNodeType;
global string $outDirTxt;
global string $lvlOutList;
global string $lvlFileName;
global string $lvlFileName;
global string $lvlPrefix;

if (`window -exists LvlOutWin`)
{
deleteUI -window LvlOutWin;
}

string $lvlWindow =`window -title "File Exporter v2.1" -width 200 -height 345
-sizeable 0 -tlc 150 150 LvlOutWin`;

string $form = `formLayout`;

string $dirOutBut = `button -width 150 -height 30 -label "Choose Output Directory" -c "getDestDirectory"`;
string $outTxt = `text -label "Output Directory:"`;
string $outDirTxt = `text -label ""`;
string $nameTxt = `text -label "Name:"`;
string $lvlFileName = `textField`;
string $expLVL = `button -label "Export File" -height 34 -c "expLvlFile"`;



formLayout -edit
-af $dirOutBut "left" 2
-af $dirOutBut "right" 2
-af $dirOutBut "top" 5

-af $outTxt "left" 2
-ac $outTxt "top" 2 $dirOutBut

-ac $outDirTxt "top" 2 $outTxt
-af $outDirTxt "left" 2

-af $nameTxt "left" 2
-ac $nameTxt "top" 2 $outDirTxt

-ac $lvlFileName "left" 2 $nameTxt
-ac $lvlFileName "top" 2 $outDirTxt
-af $lvlFileName "right" 2


-af $expLVL "left" 2
-af $expLVL "right" 2
-ac $expLVL "top" 2 $lvlFileName

$form;

showWindow $lvlWindow;

}


////////////////////////////////////
// Get Destination Directory //
////////////////////////////////////

global proc string getDestDirectory()
{

global string $targetDirectory;
string $targetNtDirectory;
string $targetPathElements[];
int $i, $pathElementsCount;
fileBrowser "getTargetDirectory" "Choose Destination Directory" "directory" 4;
tokenize($targetDirectory,"/",$targetPathElements);
$pathElementsCount=size($targetPathElements);
for ($i=0;$i<$pathElementsCount;$i++)
$targetNtDirectory=$targetNtDirectory+$targetPathE
lements[$i]+"\\";
return $targetNtDirectory;
}

///////////////////////////////
// Get Target Directory //
///////////////////////////////

global proc getTargetDirectory ( string $directory , string $type)
{
global string $outDirTxt;
global string $targetDirectory;
$targetDirectory=$directory + "/";
text -edit -label (""+$targetDirectory+"") $outDirTxt;
}

///////////////////////////
// Export the Level File //
///////////////////////////

global proc expLvlFile()
{
global string $curObjNodeType;
global string $lvlFileName;
global string $targetDirectory;
global string $lvlOutList;
global string $lvlPrefix;
//get the name of the file to be created
string $lvlFileNameTxt = `textField -q -tx $lvlFileName`;
$LvlFile = ($targetDirectory + $lvlFileNameTxt + ".txt");
$FileID = `fopen $LvlFile "w"`;

fprint $FileID "This is a test!\nAll is good\n";

fclose $FileID;
}

safakoner
01-08-2004, 08:22 AM
@mhovland : Thank you for this detailed example. This is very useful. :thumbsup:

CGTalk Moderation
01-17-2006, 03:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.