Electron: Store user editable data in .txt file - javascript

I am making an electron app that converts data from .txt files to Javascript arrays. This data is stored inside a folder called faces in the main directory. I also have a button in my app which, when clicked opens file explorer at the faces folder so the user can edit the .txt files. This works fine when running npm start, but then when I use electron builder to package my app, the app can no longer find the .txt files and the user cannot edit them (giving me lots of errors). Is there some way to have a folder of .txt files that the app uses to draw information from with Electron builder?
Edit
Below is the JS used:
//Import Lists from .txt files
var ears = fs.readFileSync('faces/ears.txt', 'utf8').split('\n');
var mouths = fs.readFileSync('faces/mouths.txt', 'utf8').split('\n');
var eyes = fs.readFileSync('faces/eyes.txt', 'utf8').split('\n');
//Opens faces txt docs in file explorer
function edit() {
shell.openItem(require('electron').remote.app.getAppPath() + '/faces')
}
Here is what happens when I open the packaged app (this is the win-unpacked result but the error is the same for .exe which runs with the installer):
As you can see it does not load an information and you can see it cannot find the faces folder or the .txt files.

I think the problem is that the path-joining character is different in each OS.
You can see that every slash was backslash(\) before your suffix, /faces.
Try using path module.
const path = require(path)
....
function edit() {
shell.openItem(
path.resolve(require('electron').remote.app.getAppPath(), 'faces')
)
}

The builded app is trying to write in the app folder itself. This is possible in dev, but not in production since che app became an asar archive. (app.asar). If you look at the error your app is trying to write inside app.asas, which is not a folder. So it's not possible to write inside it.
You probably want to save these kind of information not in the path where the application has been installed, but in some of the paths related with user config/preferences.
This: https://www.electronjs.org/docs/api/app#appgetpathname may help you retrive the right path.

Related

How to save/write a file from html with javascript?

I am doing an easy desktop app to crop photos, in order to do this I have a npm project with a few dependencies and an HTML file and launch the browser with the following command, chromium-browser --disable-web-security.
The problem is that when the app execute this code:
crop(event) {
cropper.getCroppedCanvas().toBlob((blob) => {
saveAs(new Blob([blob] ), './a.jpg');
});
And this download automatically the file in the download folder instead of the same folder where the index file is.
Any idea about how can I set a relative path to download the file?
Thanks.
If you are just targeting chrome, there is a way. You can use Chrome's FS functionality
https://web.dev/file-system-access/
If you first load the file using this API, you will have the FileHandler object to use when you want to save

Is it necessary to add an "android_assets" folder to my app's main?

I've finished making an app with HTML, CSS, and JS files, all of which I've stored in the 'assets' folder of Android Studio.
The app runs without crashing, but only this screen appears:
Empty app screen.
I've tried implementing each of the following file paths:
webView.loadUrl("file:///assets/menu.html");
webView.loadUrl("menu.html");
Neither of them work.
I've read that it's ideal to create a new folder in 'main' and call it 'android_assets' and then name the file path ("file:///android_assets/menu.html"); but I don't want to inadvertently create more problems in my code.
Should I create the 'android_assets' folder? Is there anything I'd risk in doing so?
Try this:
1.
Ensure your assets path is: assets/foldersIfAny/index.html
2.
Then use this:
webView.loadUrl("file:///android_asset/foldersIfAny/index.html");

What is the correct folder structure for a localized ios app made with cordova

Similar questions that don't give complete answers:
How to localize Cordova iOS projects?
How to localise a string inside the iOS info.plist file?
How to localize an iphone app for different languages?
How to localize app name in cordova for iOS?
problem
The app I'm currently working on needs some localized strings, but I'm having a hard time finding the correct cordova/apple docs that tell me exactly how to setup the correct file directory.
What I understand
You need to have a InfoPlist.strings resource file for every locale in your app.
This file follows a key = "value"; format
You need to set CFBundleLocalizations in your config.xml and list the locales in your app:
<config-file parent="CFBundleLocalizations" target="*-Info.plist">
<array>
<string>en</string>
<string>es</string>
</array>
</config-file>
What I do not understand
Is there some cordova config I need to add which sets up my folder structure correctly? In xcode's project editor help it states
In the Project navigator, every resource that is localized becomes a group containing the language-specific resources. For user interface files, the group contains the user interface file for the base language and a strings file for each additional language (the strings file has the same name as the user interface file but with a .strings file extension). For example, if you add German, Main.storyboard becomes a group containing Main.storyboard (Base) and Main.strings (German).
In the file system, Xcode creates a separate language folder to store the language-specific resources. The name of the folder is the language ID followed by the .lproj file extension—for example, de.lproj. if you choose German (de) from the language menu. Xcode adds the language-specific strings files for each user interface file to the language folder. The strings files that Xcode creates contain placeholders for the localizable text. Later, you’ll export and import these language folders for localization.
Doing any of this manually in xcode (outside of cordova) defeats the purpose of automating this with cordova. My current project structure looks like
package.json
config.xml
platforms/
ios/
cordova/
CordovaLib/
platform_www/
www/
dev-app.xcodeproj/
dev-app.xcworkspace/
dev-app/
config.xml
dev-app-Info.plist
Xcode project editor help gives me a picture of a correctly setup project, how do I use cordova (hooks maybe, whatever) to get this setup?

Node-Webkit won't open file

First I'd like to say thanks for taking the time to read this.
I am trying to open a JSON file that is in the following directory structure:
#--> Root Folder
--> App.exe
#--> Configuration
---> JSON File
So I used the Code:
var ConfigFile = "./Configuration/JSON.json";
Followed by:
var fs = require('fs');
var file_content = fs.readFileSync(ConfigFile);
var content = JSON.parse(file_content);
// Manipulate the Data
For some odd reason, Node-Webkit seems to be looking for the folder in a Temp Directory located in:
C:\Users\User\AppData\Local\Temp\nw9740_14956\Configuration
The file is not there, and thus in the Console I get the following Error:
Uncaught Error: ENOENT: no such file or directory, open
'C:\Users\User\AppData\Local\Temp\nw9740_14956\Configuration\JSON.json'
I am running Windows (as you can tell), and I would like for fs to pull the file from the Folder (Configuration) that is adjacent to the app.exe.
Any help is appreciated
I've only done this once, so I may be wrong, but it looks like you're bundling your app content into the exe? If you do this, node-webkit will extract the app contents into the %TEMP% folder and then run the content from there.
Try checking the command line arguments to see if arg[0] will point you to the actual node-webkit exe that's running the app. From there, you should be able to construct a path to your configuration data.

Combine all files in a folder as pdf

My current problem is that starting from the root folder I have to look into each folder and if one file exists then save it as a pdf with the folder name in a different location. If more then one file exists then combine this into one file, save it as a pdf with the folder name in a different location.
Example:
- Root Folder
- Folder1
- FileA.tif
- FileB.tif
- Folder2
- FileC.tif
- Result Folder
- Folder1.pdf (Contains FileA.tif and FileB.tif combined into one pdf)
- Folder2.pdf (Contains FileC.tif as pdf)
I am currently using Acrobat 9 Professional. I know I can do this manually, but I have to do this for hundreds of folders and each folder has one or more files.
My preffered solution would be in Acrobat Javascript on Windows XP.
Somethings that I am trying to figure out is if I can do some sort of:
For each folder in Root Folder
For each file in folder (Save the folder name too)
Combine files (I think I can mange this based on some examples I have seen)
I think that would give me a good start.
Any help would be greatly appreciated.
GhostView is a command-line program that can manipulate PDF files. Years ago, I used this to concatenate thousands of PDF files into a single PDF for download. It can convert multiple file types to PDF, plus you can apply all of the PDF security options to your final PDF as needed. You'll need the commercial version if you're packaging this for a product.
I am trying to solve the same problem here.
You need to install GhostScript and use some bat file to do the job.
I started to write this:
#echo off
set folpath=%~dp0
set PROG="c:\Program Files\gs\gs9.04\bin\gswin64"
set OUT= "%folpath%Combined.pdf"
pushd "%~dp0"
call %PROG% -o %OUT% -sOUTPUTFILE="%folpath%Merged.pdf" -dBATCH "%folpath%doc2.pdf" "%folpath%doc1.pdf"
pause
But it is not ready :)

Categories

Resources