Electron get data from custom extension file - node.js

I am using electron and trying to achieve a result where the user clicks on the saved file which opens the electron application and gets the data of the file.
So far I have done is:
1) Created the custom extension registry and added the file open command using reg file.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.wtpd]
#="Water Treatment Plant Design File"
[HKEY_CLASSES_ROOT\.wtpd\DefaultIcon]
#="C:\\\\Users\\\\user\\\\Downloads\\\\wtpd_file.ico"
[HKEY_CLASSES_ROOT\.wtpd\shell]
[HKEY_CLASSES_ROOT\.wtpd\shell\open]
[HKEY_CLASSES_ROOT\.wtpd\shell\open\command]
#="\"C:\\Users\\user\\Desktop\\ENV\\electron.exe\" \"%1\""
2) Now it opens the electron application but i want to get the data inside of the .wtpd file in order to calculations.
I have tried to use this but the links in this examples are broken. https://www.theodo.fr/blog/2015/12/link-files-to-application-in-windows/

It seems that Windows passes the file path as an argument to the application (your Electron app in this case). So you can use process.argv to get the file path.
var filePath = process.argv[1];
var data = fs.readFileSync(openFilePath, 'utf-8');
Obviously you may want to add checks such as process.argv.length >= 2 to ensure the argument was passed (in case the application was opened manually, not invoked by windows). But this is generally how you could get the file contents.

Related

Unable to create text file using Android Studio Kotlin. Neither able to build apk with text file manually added via gradle

I am new to kotlin.
I normally used val file = "Data.txt" that easily creates the file. But now the app is almost completed I noticed that code is not able to create file anymore. Whenever I tries to create file (execute it with a button and app crashes). With so many errors (one of them) "java.io.FileNotFoundException: /data/user/0/com.example.waterledger/files/Data.txt (No such file or directory)"
When I locate the folder and manually added the file there. The app works without any problem (it easily read, writes, modify the file "Data.txt") but when I build apk it didn't get that text file that is manually created via gradle so the app crashes when I press the button
I don't think that is the proper way to create a file in Kotlin. What your code is doing is simply creating a variable named file whose value is "data.txt".
However, Kotlin provides a number of methods to create files. One of them is the writeText() method. So you could create your file like this using the writeText() method
val fileName = "data.txt"
var file = File(fileName)
file.writeText("")
The "" will simply give an empty string, as argument to writeText(), to write data to file. You may provide the string you would like to write into this file.

Adding to the PATH on Windows in node.js - sending WM_SETTINGCHANGE?

I'm writing an installation script (in node.js, specificially slush/gulp although I don't think that matters) that sets up some common tools on our developer machines.
For one of these tools, I need to modify the PATH environment variable on Windows machines.
So far the best way I've found to do this is using the winreg package to modify the Registry directly (in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path), which works great (aside from the need to run the entire install script in an elevanted command prompt).
However, it requires either a machine restart for the new PATH to take effect (not even just opening a new command prompt, as it would usually), OR sending the user into their system preferences to just open up the Environment Variables dialog box and click OK.
This detailed answer covers some of what needs to happen beneath the hood when you change an environment variable programmatically. I'm assuming the WM_SETTINGCHANGE message (details here) is sent to the system when clicking OK in that Environment Variables dialog box.
So, how could I go about sending the WM_SETTINGCHANGE message from node.js? Is that possible?
I can offer non-native solution (not sure if native exists). It updated the value for me without restarts.
I'm talking about reg.exe tool that is shipped with Windows starting at least from Windows XP.
The algorithm:
1. Form a command for update, e.g.:
const scriptContent = `REG ADD HKCU\\Environment /v Path /t REG_SZ /d "${newPath}" /f`
HKCU\Environment - is the path to your variable in registry,
Path - name of the variable to update,
REG_SZ - type of the variable,
"${newPath}" - new PATH contents (fully old content with added new paths. Using quotes just in case we have white spaces there),
/f - force rewrite (basically, this command is for creation. So, if this variable doesn't exist, it will be created, otherwise - overwritten).
2. Write this contents to a script file, e.g. script.bat:
const fs = require("fs");
const scriptPath = 'script.bat';
fs.writeFile(scriptPath, scriptContent);
3. Execute the script file:
const child_process = require("child_process");
child_process.exec(scriptPath);

Electron - Use require in local Windows network

I'm building an Electron application. It uses jquery to do some things.
I insert jquery with this in my index page:
<script>
window.$ = window.jQuery = require('jquery');
</script>
And jquery is stored in /node_modules/jquery.js.
This works fine when i run the app local or on an network drive. But if i run it on the local network it can't find the module jquery.
So:
X:\app\Electron.exe can load jquery, while
\\servername\share\app\Electron.execan't.
I've to use a relative path, because the app can be runned from different locations.
I did try to use other relative paths.
I haven't found a solution! But I do have a workaround.
I compressed the hole project with WinRar:
Select all files -> Add to archive
Archive name -> Name of your Electron application.
Archiving options -> Create SFX archive
Now go to "Advanced" tab -> SFX Options
Run after extraction -> electron.exe (or if you renamed it, that name)
Go to "Modes" tab -> Unpack to temporary folder
Silent mode -> Hide all
Ok -> Ok
Now you get one .exe file. What happens if you run it from any place:
The SFX archive extracts itself in a temporary location (appdata).
After extraction the app will be executed.
So you never get the problem, because it's local.
But i think it will cost some performance...?
Create a batch file with:
pushd \\server\share\
your-electron-program.exe
popd
This will dynamically create a mapped drive and get rid of it once the program ends.
If you don't like having a .bat file and the command window, buy Batch Compiler and compile it into an Exe. Or other similar program.

ChromeWorker File Not including in the extension package

Hi I am building a Mozilla Extension through CFX tool. I have used ChromeWorker in it. It is working fine while i am running : cfx run command. But while building up a package using cfx xpi the Chrome Worker file is not included in the xpi package.
I am using this to create the worker thread.
var tworker = new ChromeWorker("chrome://addons/content/t_worker.js");
my t_worker.js file is present in addons/lib
I have also put one chrome.manifest file in the package that contains :
content addons ./resources/addons/lib/
Please tell the possible reason for this problem and also how to fix it .
Try moving your file into the data folder then do:
const self = require('sdk/self');
var tworker = new ChromeWorker(self.data.url('t_worker.js')
im totally not sure of this syntax, i just typed off top of my head
I putted that worker file in the lib thats why its not working. I just changed the location of my file to data folder and made changes in my chrome.manifest file : content addons ./resources/addons/data(previously it was lib)/ . Its working fine Thanks to #Noitidart for the suggestion of putting it in data folder

using FileChooser to save a file with default filename

I wat to save a file.I use this.
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showSaveDialog(null);
But in the dialog I want to suggest a name for the file, so that the user only selects a directory for the given file.The name of the file is known already.So i want to suggest that filename.
ThankYou.
This is now fixed in Javafx 2.2.45 (bundled with java 7.0_45 now) and you can do what the OP is suggesing with the following property of fileChooser, setInitialFilename, used as such:
FileChooser myFile = new FileChooser();
myFile.setInitialFileName("Whatever_file_I_want.coolFile");
Now, I don't think there is anyway to STOP the user from choosing a different file, but at leas this will give them a default you want them to pick.
Initial file name providing - it is a thing, which requires to transfer your string (initial name) through native call, to the call of the native file chooser. It is a complex thing, and you can look at these issues about its implementing :
http://javafx-jira.kenai.com/browse/RT-16111 (main one)
http://javafx-jira.kenai.com/browse/RT-24588
http://javafx-jira.kenai.com/browse/RT-24612
They all have fix version lombard, so, they are fixed in JDK 8.
So, you can specify initial file name for a file, starting from JDK 8 (you can access it, downloading JDK early access).
Recently, I've tested this feature, and it is working.
There is a method setInitialName() or smth like that.
And, as I've mentioned, it is a complex thing, and you are not likely to be able to implement it by yourself (until you are able to build jfx).
So, the decision - to wait until JDK8 release, or to use early access builds. Or, to use your own implementation of file chooser.
Here's a workaround that worked for me:
you can use javafx.stage.DirectoryChooser to select a directory for the file you want to save and after saving create a new file in this directory with the default name and extension.
DirectoryChooser dc = new DirectoryChooser();
File file = dc.showDialog(null);
if (file != null) {
file = new File(file.getAbsolutePath() + "/dafaultFilename.extension");}

Resources