How to open Sublime Text with Node.js - javascript

I'm working on a file management system built with node.js and electron.
The file management displays a list of files in a folder and allows the user to run custom commands to batch process the files, such as renaming, custom grouping ect...
However I want to implement a feature that if the user clicks on a file it will open with the text editor of their choice.
I can't get the sublime text editor (or any for that matter) to open with node.js code.
I have looked into other questions here on SO such as "Launch an external application from node.js" and "Is it possible to execute an external program from within node.js?" but none of the answers successfully work for me.
Most of the answers open the file with the "default" program of the operating system which is not what I want. I want to be able to choose the program that opens the file.
I found this code below that calls on global path variables to open external software from node:
const exec = require("child_process").exec
exec('yourApp').unref()
But of course it doesn't work if you replace 'yourApp' with a file path pointing to sublime_text.exe like so:
var exec = require('child_process').exec;
exec('C:/Program Files/Sublime Text 3/sublime_text.exe').unref()
Any help would be very very much appreciated. Thank you

You need to use this as your path:
C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe

I figured it out. It turns out it takes a little bit more code than I first posted to open an external file. The full code looks like this:
// Create a child process
var spawn = require('child_process').spawn;
var child = spawn('Path_To_.exe', ['parameters', 'Path_To_File']);
Also just in case anyone is wondering it was not necessary to escape sequence the spaces within the file name.
Thank you for everyone who offered to help!

Related

Get directory of Current active file when developing a VSCode extension

I am developing a VSCode extension to generate templates.
However, I cannot figure out how to get the location of the VSCode file that I am currently editing.
When I search for this question, most answers say to do something like vscode.window.activeTextEditor.document.uri.fsPath - however - this gets you the directory for the target VS code window (the Extension Development Host window) - which is not what I want
I want to get the directory of the VS code extension file I am editing (such as the extension.ts file).
Any help greatly appreciated.
If you mean the extension's path:
context.extensionPath
For extension.ts try something like:
context.asAbsolutePath("extension.ts") // if at top level
If you don't know the directory structure of the file you are interested in, like src/otherDirectory/otherSubDirectory, I think you will have to search/walk for it with fs commands starting from the extensionPath. See https://dustinpfister.github.io/2018/07/20/nodejs-ways-to-walk-a-file-system/ for example.

DOM operations in VSCode Webview used to edit all local paths

I want to create some extension to VSCode using its Webview feature. I want to show HTML preview as a feature of my extension. As I can see in VSCode API documentation of WebView the paths for resources have to be registered and edit in a special way.
I want to load HTML from the file and then edit and register all paths to CSS and JS into it to create the preview of this one. In my opinion, using some DOM manipulating is the easiest way to create it. Simple import of jsdom doesn't work, even if the same example works in plain Node.js. Have anyone done a similar thing in the way it works well? Maybe there is some other way to solve this problem.
Thank you in advance.
I have done this in the past and the way that it worked was to read that file (with fs in my case) that contains the HTML and render it afterwards. Any resources you want to use have to be passed inside the localResourceRoots array as an option in the vscode.ViewColumn.
Here is a full example:
const panel = vscode.window.createWebviewPanel(
'catCoding',
'Cat Coding',
vscode.ViewColumn.One,
{
// Only allow the webview to access resources in our extension's media directory
localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, 'media'))]
}
);

Illustrator file that runs commands upon opening

This is a long shot, but is there a possible way to store a JavaScript (or any sort of executable code) within an Adobe Illustrator file?
The reason I'm asking this is because I'm currently trying to automate the process of importing render results from Autodesk Maya. The concept is that as soon Maya is done rendering all frames/layers, a MEL script could generate a file that Illustrator could open and run commands found in it, directing illustrator to the render results and start importing them.
The original idea was to give a system command via MEL script to launch Illustrator straight away after rendering completion and somehow start the process. But since this automation is for not-so-tech-savvy people, an application calling for the launch of another one would be rather frustrating and maybe confusing.
Having Maya generate a file that can complete the task when the user opens it is a much preferred solution. Give more control to the user and does not overload a system that is already busy with more application calls.
Think of it like a .mel file, where upon opening, it launches the needed application (Maya) and when the application is ready, carries out the commands included (MEL). Is there a way to do that with Adobe applications, Illustrator in particular, where a file automatically is recognized as an Illustrator file (eg. .ai), launches application and then runs code contained in it (eg. JS)?
ANY help is welcomed, but I would like to avoid applescripts/VBS as they are platform specific and can be difficult to manage between Mac/Windows.
Thanks.
This is a relatively broad question as there can be many ways to achieve this.
I'll try to give you one possible solution here, it clearly might not be the best.
Your needs:
Create a "file" or something that imports all the Maya's rendered images in an illustrator scene.
Can be executed whenever you want (No post render process that opens illustrator)
Non-tech people like my mom have to be able to use it.
Cross-platform (Win/Osx)
Solution:
Create a post-render script (mel or python) for Maya
Concerning this script (.mel or .py):
Is run once all frames have been rendered
Copies an existing JavaScript (.jsx) file in the folder where the frames have been rendered
Creates two executable files (.bat and .command, both for Windows and OSX)
Concerning the .jsx file:
Creates a new .ai file
Imports the rendered frames one by one and adds them in a new artboard
Saves the .ai file in the current folder
Concerning the .bat and .command executables:
Run Illustrator
Execute the .jsx script on startup
To sum up, once the frames will have been rendered, all three files (.jsx, .bat, .command) will be created in the same folder as the frames.
When your artists will want to create their Illustrator scene, they'll just have to double click on the .bat or .command file to automatically run Illustrator and import the rendered frames and save the file.
The command to run a script on Illustrator (CS 5.1) on Windows is and this will be the content of the .bat file:
"C:\Program Files (x86)\Adobe\Adobe Illustrator CS5.1\SupportFiles\Contents\Windows\Illustrator.exe" C:\Path\To\Script.jsx
You can easily make an equivalent for the .command file.
Script.jsx can be something like this:
// Get the fullpath of the script
var script_fullpath = $.fileName
// Only get the folder path
var script_folder = Folder(script_fullpath).path
// Get renderred frames
var rendered_frames = Folder(script_folder).getFiles("*.exr");
if(rendered_frames.length == 0){
alert("No images to import");
}else{
// Loop through all the images
// Create a new artbook
// Import Fram
// Then save the file here: script_folder + "/" + "illustratorFile"
}

Using the `runScript` function to run a JXA script does not allow parameters

I use JXA to script workflows for Alfred 2 and recently tried to run a script from within another script. I need to pass some text between the scripts, so I decided to use parameters, but whenever I try to pass a string, a number, an array or anything else that isn't an object to it, it gives the error "Error on line 4: Error: An error occurred.". If I do pass an object, the second script (the one being run by the first script) receives an empty object rather than the one passed to it. The same happens when the first script is an AppleScript, but if the second script is an AppleScript, it all works perfectly. Passing arguments through osascript from the command line also works. Is the API broken or is there something that I'm doing wrong?
First script:
var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.runScript(new Path("/path/to/second/script.scpt"), { withParameters: "Hello World!" });
Second script:
function run(args) {
return args;
}
Edit:
If the second script is edited as below, the dialogue is displayed but the runScript method of the first script still returns an error.
function run(args) {
var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.displayDialog(args.toString());
return args;
}
Edit 2:
The runScript function actually seems to be working perfectly other than the problem with the parameters. The error isn't actually being thrown, just displayed by the Script Editor, and execution continues after the call to runScript as if nothing had happened. The returned value also work perfectly, despite the parameters not working.
A note about Alfred 2 workflows
To run some code in Alfred 2 (triggered by a search, keyboard command, etc.), it must be typed into a box in the app, not in a file.
The box to enter code in is very small and does not provide syntax highlighting, and this makes editing code difficult and annoying. For smaller files, it is okay, but for larger files it is easier to use a short script to run a script file. I've tried Bash, which would be the simplest option, but Alfred 2 does not provide an option to escape single quotes. I also cannot use script libraries (to my knowledge, correct me if I'm wrong), as the code is not in a script bundle and all of the required files need to be within the same folder (for exportation reasons).
I don't know how to avoid the runScript error, but I can suggest an alternative approach: load the script as a script library.
Using a script library
Turning a script into a library can be as simple as saving the script to ~/Library/Script Libraries. If your script file is named script.scpt and has a run handler, and you save it to the Script Libraries folder, then you can then invoke it from another script like so:
Library("script").run(["Hello, world!"])
Script libraries are documented in the JXA release notes for OS X 10.10, in the WWDC 2014 session video introducing JXA, and in the AppleScript Language Guide.
Embedding a script library inside of a script bundle
According to the AppleScript Language Guide documentation for script libraries, there is a search policy for finding Script Libraries folders. The first place it searches is:
If the script that references the library is a bundle, the script’s bundle Resources directory. This means that scripts may be packaged and distributed with the libraries they use.
To apply this to the example given in the question, you would need to re-save the first script as a script bundle, and then embed the second script inside of the first script.
For example, if you re-save the first script as script.scptd, then you could save the second script embedded.scpt to script.scptd/Resources/Script Libraries/embedded.scpt. You should then be able to use Library('embedded') to access the script library.
To re-save an existing script as a script bundle, you can either use the File > Export... menu item in Script Editor, or you can hold down option while selecting the File menu to reveal the File > Save As... menu item. The File Format pop-up menu lets you choose the Script bundle format.
Once you have a script bundle open, you can reveal the bundle content panel by using the Show Bundle Contents menu item or toolbar button. You can then use the gear menu to create the Script Libraries folder inside of the Resources folder, and then you can drag a script into that folder.

Cant adding JS file, or any other, to a Titanium project folder

So i am making a mobile app. in this app, on my main screen, i have a button that when clicked it will load another frame, or js file. this i know how to do but when pressing the button it gives me an error, "cant find file". after 3 hours of web searching i have tried changing the path names to every possible combination and moveing the file around. after doing so i came to realize that next to the new file i created, or any new file i create, there is an "*" and a question mark. for example if i make a new file called login.js it will show up on the explorer window as *?login.js, where the question mark is much smaller symbol. Is there a manifest i must add it to, like in netbeans, because it seems as if there is an error in the file but it is empty.
function signup(e){
var menu = Ti.UI.createWindow({
url: 'Main-Menu.js'
});
menu.open();
}
i also did: Titanium.include('/controllers/Main-Menu.js');
but always getting the same error, the JS file is in my apps controller folder.
This *? indication comes from the git plugin that is shipped with Titanium Studio. It indicates that the file is not commited.
To answer your "can't find file" - question, I need to know some more details about your environment (target platform, sdk version, example code, ...)

Categories