JS script example for LocalFileOpen/LocalFileWriteString/LocalFileOpen

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

JS script example for LocalFileOpen/LocalFileWriteString/LocalFileOpen

Jump to solution
941 Views
Fred_Liu
Contributor II

Hello

  I tried to use FM3.1 JS(ActiveX) to write or read FM variable to my local folder but fail. Can I print out the "FMSTR_PROJECT_PATH"? Any related example for local file handle?

 

Thanks a lot

Fred

 

0 Kudos
1 Solution
938 Views
MichalH
NXP Apps Support
NXP Apps Support

Hello, 

The FMSTR_PROJECT_PATH represents path to a saved FreeMASTER project file (.pmp/.pmpx file).

Typically you will have your HTML page stored in the same directory as project file or (even better) in a subdirectory of a directory where the project file is stored. This will enable you to pack the HTML and all other resource files (pictures, script files, etc) to the .pmp file when saving (see User Guide section 5.3).

See a minimalistic test page code below. Save it as "control.htm" together with the project file and specify its name as the Control page URL in project options.

 

control_page_html.png

 

<html><head></head>
<body>
<OBJECT id="pcm" height="0" width="0" classid="clsid:48A185F1-FFDB-11D3-80E3-00C04F176153">
</OBJECT>

<script
 language="JScript">

function TestWrite() {
    var handle = pcm.LocalFileOpen("FMSTR_PROJECT_PATH/" + file_name.value, "a");
    if(handle) {
        var characters_written = pcm.LocalFileWriteString(handle, to_write.value + "\r\n");
        pcm.LocalFileClose(handle);

        alert(characters_written + " characters written to '" + file_name.value + "'");
    }
    else {
        alert("Cannot open '" + file_name.value + "' "  + pcm.LastRetMsg);
    }
}

</script>

FreeMASTER Write test <br>

filename <input id="file_name" type="text" value="test.txt"> <br>
text <input id="to_write" type="text" value="string to write"> <br>
<input id="do_write" type="button" onclick="TestWrite()" value="Write!">

</body></html>

Regards,
Michal

View solution in original post

0 Kudos
1 Reply
939 Views
MichalH
NXP Apps Support
NXP Apps Support

Hello, 

The FMSTR_PROJECT_PATH represents path to a saved FreeMASTER project file (.pmp/.pmpx file).

Typically you will have your HTML page stored in the same directory as project file or (even better) in a subdirectory of a directory where the project file is stored. This will enable you to pack the HTML and all other resource files (pictures, script files, etc) to the .pmp file when saving (see User Guide section 5.3).

See a minimalistic test page code below. Save it as "control.htm" together with the project file and specify its name as the Control page URL in project options.

 

control_page_html.png

 

<html><head></head>
<body>
<OBJECT id="pcm" height="0" width="0" classid="clsid:48A185F1-FFDB-11D3-80E3-00C04F176153">
</OBJECT>

<script
 language="JScript">

function TestWrite() {
    var handle = pcm.LocalFileOpen("FMSTR_PROJECT_PATH/" + file_name.value, "a");
    if(handle) {
        var characters_written = pcm.LocalFileWriteString(handle, to_write.value + "\r\n");
        pcm.LocalFileClose(handle);

        alert(characters_written + " characters written to '" + file_name.value + "'");
    }
    else {
        alert("Cannot open '" + file_name.value + "' "  + pcm.LastRetMsg);
    }
}

</script>

FreeMASTER Write test <br>

filename <input id="file_name" type="text" value="test.txt"> <br>
text <input id="to_write" type="text" value="string to write"> <br>
<input id="do_write" type="button" onclick="TestWrite()" value="Write!">

</body></html>

Regards,
Michal
0 Kudos