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

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);

Related

Electron get data from custom extension file

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.

How to not display KeyStorePassword on the command line?

when configuring https for play framework, I have to use following configuration when running the background task.
play -Dhttps.port=9443 -Dhttps.keyStore=keystore.jks -Dhttps.keyStorePassword=password run
I don't want to display the keystore password on the command line. It shouldn't be visible for all users on that machine.
HTTPS configuration can either be supplied using system properties or in application.conf
I recommend to use a combination of environment variables and the application.conf
Put your sensitive information in environment variables
Reference these environment variables from the application.conf:
Like this:
https.keyStore = defaultvalue
https.keyStore = ${?MY_HTTPS_KEY_STORE_ENV}
The question mark means that if there is no value found for MY_HTTPS_KEY_STORE_ENV then the defaultvalue from above will be used

Inno Setup - How to replace UserName with string

first attempt at posting this question was with severe jet lag. This re-edit of the question I only have the associated sleep deprivation...
I want to know the best way to use the installer to capture path information and then remove any reference to the user profile.
My program is designed to run from a central network location. To reduce network traffic some files are copied to a local drive, eg. AppData\MyProg\
I use a file browse dialogue so the person installing can specify the location:
pg_LocalPaths := CreateInputDirPage(pg_CentralPaths.id,
'Confirm File Locations For User Settings', 'To improve user experience these locations should be off the network.',
'Default Locations are:', False, 'New Folder');
// Add file browswer item (with an empty caption)
pg_LocalPaths.Add('Supporting files will be copied here by the application:');
pg_LocalPaths.Add('User settings (for a single user) will be saved here:');
// Set initial value
pg_LocalPaths.Values[0] := GetPreviousData('pg_LocalPaths0', ExpandConstant('{userappdata}\{#pFolder}'));
pg_LocalPaths.Values[1] := GetPreviousData('pg_LocalPaths1', ExpandConstant('{userappdata}\{#pFolder}'));
During install the local drive location is specified and stored in a config file.
An issue that I failed to predict is that when the program is installed, the path in the config file is defined for a single user, namely the user performing the install. In practice any user should be able to run the program...
To get the correct path for any given user, my software looks for a string "{UserName}" and replaces it with: Environ(UserName)
MyPath = Replace(MyPath, "{UserName}", Environ(UserName))
So, for example, in the installer I need to replace:
C:\Users\My Name\My Program OR C:\Users\My Admin\My Program
with something like:
C:\Users\\{UserName}\My Program
I alreay have this working in My Program but I am not sure of the best way to get it working in Inno... My inital thought was to write the config file like this:
StringChangeEx(MyPath, "My Name", "{UserName}", True)
Would like some perspective on this, not sure how to get it working for all situations.
Cheers,
You're doing it the wrong way.
Don't store the complete path in the config file; merely store a value that indicates that the user wishes to store data in the usual per-user location. (Or don't store any value, since that should be the default anyway.)
Then in your application, on every run of your application, use the Shell API to fetch the current AppData path for the current user and append your app's unique subfolder to this.
Note that it is perfectly valid for the user's AppData path to not contain their username, and not even be on C:. Don't make assumptions; use the Shell API. That's what it's for.
(Exactly which one to use and how to call it varies depending on target OS and your programming language of choice, which you haven't specified.)

Code Behind RStudio Server Export Function

I am currently using RStudio-server on Linux redhat. One nice feature of RStudio-server is that I can export from the server to my Windows desktop. Does anyone know the code behind the export drop-down?
The export function can be found via the Files tab:
(More >> Export...)
I would like use code to automate the exporting of objects. I figured I should be able to perform this export using the system function, but I am having trouble.
Thanks for any help.
I think this post might help you,
Spacedman explains that you can trigger the export by the use of the R function "browseURL", with the URL parameter replaced by the ftp path to the file.
If you absolutely want to trigger this export with a system command, perhaps you could create an R script taking as parameter the file to export and launch that script with the system() function =) Although I can't see clearly the advantages of such a process.
[edit] : After having tried it today, I realise my answer wasn't complete :
If you try the function browseURL on files such as "whateverRscript.r", it will display it in a tab of your browser, rather than trigger the download.
In order to actually make your browser download this kind of file, maybe you can zip it first.
To complete the automation process, just change the parameters of your browser such that it won't "ask everytimes where to stock the downloaded files"
This is what worked for me: run it on Server side. Working browser is required (I used Chrome)
my_data_file_name <- "data.RData"
# set file name
save(Data, file=my_data_file_name)
# save data to file
current_dir <- getwd()
# capture current working directory on server
my_export_file_path <- paste0(current_dir, '/', my_data_file_name)
# create a path for file to export
browseURL(my_export_file_path)
# export to local disk using browser's capabilities

Inno-Setup checking file location prior to installation, then using it during installation

I need to check for the location of a file during program installation utilizing inno setup. I then need inno setup to use the location of that file in the "Filename" line to create a desktop ICON for program initialization. I have the code for the "Icons" option working fine with the exception of how to do the above.
Here is the line of code I am currently using;
Name: "{commondesktop}\SA - NH Bricscad V12"; Filename:"**c:\program files\septic assistant\new hampshire\support\**SA - NH Bricscad V12.exe"; IconFilename: "C:\Program Files\Septic Assistant\New Hampshire\Support\Bricscadlogo.ico"; Comment: "Septic Assistant the only Septic Design Program"
Hi-Lited section would be the path to the exe file that I need inno setup to search for.
Any assistance with this would be very much appreciated.
Bruce
Just use a {code:...} constant and corresponding [Code] function that returns the appropriate path for your [Icons] entry. You will probably also want to use a Check function to avoid installing the icon in the case that you cannot find the correct location.
Another option is to use a {reg:...} constant, assuming that the path you are trying to locate is specified somewhere in the registry (which is usually the case).
If the path is not already specified somewhere well-defined in the Registry when the other app is installed, and you don't have some other means to quickly identify where the other app is located (note that doing a global search of the user's HD is not a valid option), then you should add a page that prompts the user to enter the location themselves (which you can then verify that they have chosen the correct location). You can see examples of prompting the user for information and then doing something with that info in the CodeDlg.iss example included with Inno, and in the ISXKB wiki.

Resources