const workingFolder = "/Internal/Documents/MyData";
function OnStart() {
lay = app.CreateLayout("Linear", "VCenter,FillXY");
txt = app.AddText(lay, "The file was not read");
// Check if the necessary permissions are granted.
if (checkPerm()) {
read();
write();
} else {
// If permissions are not granted, request them.
requestPerm();
}
app.AddLayout(lay);
}
// Check if the required permissions are granted.
function checkPerm() {
return !!app.CheckPermission(workingFolder);
}
// Request permissions from the user.
function requestPerm() {
// Show a popup to inform the user that permission is required.
app.ShowPopup("Allow access to the Documents folder.", "Permission required");
// Request permission to access the internal storage.
app.GetPermission("internal", OnPermission);
}
// Handle the result of the permission request.
function OnPermission(path) {
// If permission is not granted, show an alert.
if (!path) {
app.Alert("Permission not granted!");
} else if (!checkPerm()) {
// If the permission is granted but the folder is not selected, alert the user.
alert("Make sure to select the Documents folder.");
requestPerm(); // Request permission again.
} else {
// If permissions are granted and folder is selected, proceed to write and read.
write();
read();
}
}
function write() {
// Create content with the current date and time.
const content = "Last visit: " + new Date().toLocaleString();
// Ensure the working folder exists.
app.MakeFolder(workingFolder);
// Write the content to a file named "test.txt" in the working folder.
app.WriteFile(workingFolder + "/test.txt", content);
}
function read() {
// Read the content of the file "test.txt".
const content = app.ReadFile(workingFolder + "/test.txt");
txt.SetText(content);
}