https://github.com/jamiebuilds/the-super-tiny-compiler
I've imported this project in Eclipse. When I compile and run "the-super-tiny-compiler.js", it gets terminated instantly. Why doesn't it asks us to input the source code, and why doesn't it show the compiled output? Is something missing in this code?
There's no "main" code in there or any code to read a source file (or read from stdin). Only a function that takes the source as a string.
So it looks like it's meant as a library and you'll have to write your own IO code if you want to turn it into a command line application.
Alternatively you could use it from an HTML page and get the source from a textarea.
Related
In my electron renderer, I have the following script:
<script>require('main.js')</script>
In that file, everything is commented out and my program keeps crashing. If I change the above to:
<script src="main.js"></script>
The file loads, however I cannot use require within that file. What do I need to change to get this to start working?
Edit:
I think the issue might be because I am using pug to generate my html like so:
block content
script.
require('../../js/client/main')
When I use an actual HTML file, it loads without hanging.
So, it looks like the issue was that electron was trying to load a .js.map file from the wrong location. electron-pug was intercepting the url of the file and since it was not found it was throwing an error (not handling it very well it seems).
My solution to fix this was to have inline sourcemaps instead of having a separate file.
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.
I am trying to get the current working directory path using JavaScript.
When I execute from ubuntu: $root#ubuntu:/var/test/geek# firefox /var/www/application/test.html
I get /var/www/application in my alert box instead of /var/test/geek in my alert box.
I used the JavaScript code
var path=window.location.pathname
alert(path);
The above code gives the path of test.html
Editing: Is it possible to use a Perl script? Perl-CGI?
After knowing that it is not possible from JavaScript, tried to use a Perl script:
my $pwd=cwd();
and running the Perl script from HTML.
Executing from ubuntu: $root#ubuntu:/var/test/geek# firefox /var/www/application/test.html
I still get /var/www/application in page instead of /var/test/geek.
Is it possible from Perl?
I suggest not to rely on this. The pathname you got might not be the actual path. Most of the time, developers use the routing to hide the actual path.
The actual directory structure might be something like "D:\Test\Test\Test.html", but you will be getting something different due to routing.
I'm having a problem with a small batch script that I'm writing. The point of the batch script is to run a Javascript file that converts an XML file to a csv file and then run a Python script which will analyze the just created csv file and create another csv file.
The batch script is below.
start XML-CSV_Converter.js
python CSV_ANALYZER.py
exit
I didn't write the XML-CSV converter;
It can be found here. (http://gotochriswest.com/blog/2011/05/05/excel-batch-convert-xls-to-csv/) The only thing I changed was I removed all of the alerts and input prompts so it doesn't wait on any user input. Simply put, all it does it look at every XML file in the current directory and produces a csv file in the same directory.
Whenever I run the batch script, I keep getting an IO error in my Python script because even though it can see the created file, it isn't able to open the file.
The exact error is:
"IOError: [Errno 2] No such file or directory: 'NAME_OF_FILE.csv'"
The part of the Python script which is causing errors is listed below.
dirList = os.listdir("C:\FOLDER")
for fname in dirList:
if fname.find(".csv") != -1:
inputFile = open(fname,'r') <---- Script halts here
Anybody know what could be causing the file not being opened in the Python script?
If I run the JavaScript file manually, and then the Python script manually, it works perfectly. But when I try to chain them together in a batch file it breaks. I would appreciate any and all ideas!
Thanks in advance!
The start command launches a program (or, in your case, a file) and immediately returns, without waiting for the program to exit. This means that when Python tries to open the file, the JS script is probably still running and has the file open exclusively for writing.
You should run the JS file by directly calling the Windows Scripting Host (cscript.exe). Just replace the start command with:
cscript //nologo XML-CSV_Converter.js
This will ensure that when the Python command is run, the JS script will have completed its job and safely terminated.
You forgot to add the folder name to the file name in the open() call. listdir() returns only the file names, so unless its path argument matches the current directory, you'll have to add it to the returned file names to properly identify them.
As a side note, you should also avoid using backward slashes in normal strings. In this case "C:\FOLDER" happens to work, but "C:\folder" wouldn't, because \f would be converted to \x0c (the linefeed character). You should use either a raw string (r'C:\FOLDER' which doesn't convert escaped characters), an escaped backslash ('C:\\FOLDER'), or the more portable 'C:/FOLDER' (on Windows '/' is the alternative path separator).
Also a simpler way to check for the extension is to test whether the name ends with it (the s.find(t) == -1 is not very Pythonic). All in all, the modified code could look something like this:
folder = "C:/FOLDER"
for fname in os.listdir(folder):
if fname.endswith(".csv"):
inputFile = open(os.path.join(folder, fname),'r')
The root cause is "python CSV_ANALYZER.py" got executed while the XML-CSV_Converter.js was not finished.
By type "start XML-CSV_Converter.js", it won't wait for this command to finish before executing the next command.
If you have installed js interpreter, you can remove the `start' in the first line, an then it should work.
Is there a way to compile CoffeeScript to JavaScript code using JavaScript?
User writes CoffeeScript code to a textarea (or a codemirror editor..), and I want to compile the coffee code to js and execute it.
I know how to compile coffee to js with php, or other server side languages but I want to do it on client side.
Go to the CoffeeScript site and read the "text/coffeescript Script Tags" paragraph.
In fact, the little bit of glue script that runs "Try CoffeeScript" above, as well as the jQuery for the menu, is implemented in just this way. View source and look at the bottom of the page to see the example. Including the script also gives you access to CoffeeScript.compile() so you can pop open Firebug and try compiling some strings.
Don't forget the bare option, or else the compiled code will be wrapped
CoffeeScript.compile('alert "hello"', { bare: 'on' })
This project called js2coffee looks exactly what you need