I am trying to turn an old javascript program of mine into a service on a mac using automator. When I use my Code, it doesn't work. I have checked what I can, and I have found that the most basic issue I encounter is that the .length property from javascript does not work. This simple program doesn't return what it should. It returns nothing:
function run(input) {
var ina = 'hello';
var newn = ina.length;
return newn;
}
Your code returns 5.
input is an array of 0 elements. Not of much use in script editor. When run from a command line it is the parameters.
As a side note run is the run handler for all AppleScript (including JXA), so you would not call function run. It is called for you.
UPDATE:
In automator, The result (5) is sent to the next automator task. You can do a "Set Value of Variable" right after you do "Run JavaScript" with your script to capture the 5.
Related
I am reading data from a serial port and with test case capturing the required output, then this output is sent to html
now while reading from serial port, there are few unicode charachters in the output.
I can remove them by using
.replace(/[^\x0000-\xFFFF]/g, "").trim();
there are approx 50 places where I need to use this replace and trim method, so I am trying to define a function, where I can pass the output to the function and it will give me clean output.
So I do not have to write .replace and .trim to every output.
here is what I tried till now.
This is the function I define to do the replace and trim.
function cleanoutput () {
var output = output.replace(/[^\x0000-\xFFFF]/g, "").trim();
}
This is the function calling to the output
display.adults = cleanoutput (adults.slice(28, 31));
By doing this I am getting error in function cleanoutput
error - "Cannot read property 'replace' of undefined"
I am just learning nodejs, need help on making this work.
There are two problems with your function: first, you are using replace on a variable that probably is not defined yet but is probably intended to work on a parameter, that is not defined either.
The second problem is that your function is not returning anything, so it is implicitly returning undefined.
display.adults will be undefined after the execution of the cleanoutput function.
You can fix those problems with something like this:
function cleanoutput (output) {
return output.replace(/[^\x0000-\xFFFF]/g, "").trim();
}
My suggestion is to dive a little bit deeper into javascript functions.
There are plenty of articles, blog posts, and youtube videos on the topic and I'm sure you will find a good one.
Can't figure out why exactly a variable can't be used to help call a value in an array with JS/Jade. This is being called inside a script on a .jade file.
The array contains roughly 400 entries and one of the entries is as follows:
myFood[10]['Cake'] = 50
When using the variable i instead of directly putting in the number 10 an error occurs.
Works:
alert (i) // 10
alert (#{myFood[10]['Cake']}) // 50
Error:
alert (#{myFood[i]['Cake']}) // Error, Cannot read property 'Cake' of undefined.
First of all, know that Jade recently was renamed to "pugjs".
I'm assuming that i is a javascript variable as you stated in the comments.
The pug (jade) context and the browser Javascript context are two very different things, so if you define i in browser Javascript, jade will not see it as a variable, just plaintext waiting to be interpreted by the browser. That's why myFood[i] is undefined and thus resulting in this error.
The correct way to define a jade variable is by prefixing your line by a dash (-), as described here.
The full code is:
- var i = 10;
alert (#{i}) // 10
alert (#{myFood[10]['Cake']}) // 50
You note that I also changed the first alert to tell pug to replace the #{i} part by the actual value of i.
If you want to access each value I would highly suggest that you use loops instead of using an i to access the array.
Mixing pug and javascript is a bit tricky, so good luck!
This is one of the first time's I'm looking at javascript, so please excuse the newbish question.
I'm trying to read the code for a specific function on a website that is of interest to me. I didn't write anything for the website, so cannot really comment on the general structure. This is almost like reverse engineering. Where it's called (in a js/main.js) looks like:
$(document).ready(function() {
$('#search').funcA();
From what I understand this is saying from the file/class or whatever that comesf rom the id search, call funcA. My questions is: how do I see the file that is called with #search?
funcA is almost certainly a jQuery plugin (or part of jQuery itself). The first thing I would try in your situation is searching for "jQuery funcA" on Google.
Whether or not it is actually part of jQuery, you can see the source for that function by running:
$('#search').funcA
in a REPL, such as your browser's console, or:
console.log( $('#search').funcA );
as long as the toString function for that function hasn't been overwritten and it is not a reference to a native function.
funcA appears to be defined as a jQuery method; try
console.log($.fn.funcA)
Open javascript console in the same browser window (I used chrome) that is displaying the page that contains that code. Then just execute this line:
> $('#search').funcA
You should see the body of funcA. Random example output when I did $("#myownid").show:
function funcA (a,b,c){var d,e;if(a||a===0)return
this.animate(cu("show",3),a,b,c);for(var
g=0,h=this.length;g
...
If you manage to see the body of the function, you should be able to infer likely sources (or post them here and we should be able to point you further)
The console.log suggestions here are nice use of Function.prototype.toString (and in some browsers some console magic), but I'd use debugger instead. Chrome has quite nice debugging tools for stuff like this and the debugger statement will get you there with ease.
var test = $('#search').funcA;
debugger;
Open the console and start investigating. When the execution of your code hits that breakpoint, you'll see handy tools like this
Right-clicking test there should also give you the option to "Show function definition" which will show you where the function was actually defined as source code.
And if you want to investigate even further from there, you can always set similar breakpoints right from the Chrome dev console.
Short version: Open the console and run $("#search") it will return a jquery object containing the dom node that has an id of search.
Long version:
$("something")
Is jquery (a java script library) for select elements by css selector returning a jquery object.
https://learn.jquery.com/using-jquery-core/selecting-elements/
$(document).ready(function() {
Is jquery for when my document (basically the page) is ready for me to muck with run this anonymous function.
https://learn.jquery.com/using-jquery-core/document-ready/
$('#search').funcA();
Selects a set of elements, in this case the single element with id "search" and then run funcA on each of them using the element as the scope. So it would run funcA on the element with ID "search" with the search node being the value of the special scope variable (scope is referenced through the key word "this", it can get rather complex).
So in essence what your seeing is:
When my document is ready find the search element and run my function funcA on it.
I'm updating an existing website running on Expression Engine. So far, I've stayed away from any code I didn't write or couldn't understand. I recently must have altered some bit of code someplace (helpful, I know) and now a block of JS I didn't write is causing an error that seems to bypass the document.ready() event. The window.load() event however is still taking place.
In the Chrome DevTools Console, the error "Uncought TypeError: Cannot call method 'replace' of UNDEFINED" points to the definition of a function "fixedEncodeURIComponent" pasted below.
$("#MessageContainer.Counted").counter({
type: 'char',
goal: 250,
count: 'down'
}).change(function(){
var TEMP = fixedEncodeURIComponent($(this).val());
$("#Message").val(TEMP);
});
var TEMP = fixedEncodeURIComponent($("#MessageContainer.Test").val());
$("#Message").val(TEMP);
function fixedEncodeURIComponent (str) {
str=str.replace(/"/g, '');
return encodeURIComponent(str).replace(/[!'()*]/g, escape);
}
As I interpret the error, this function is being passed a variable that is not a string. I added an alert(str) to the function definition and the result was UNDEFINED as I expected. The first of several unknowns for me is which call to the function 'fixedEncodeURIComponent' is being passed a bad variable. I assume that it's the first call, but that's just a guess. It so happens that this first call contains a syntax I have never encountered before. I don't know how to interpret what happens when $(this) is passed as a function argument.
Any insights would be greatly appreciated. Also, if there's more information you need please let me know. The client's site is password protected but I can include any code you request.
Thank you.
I'm taking a guess that the }); on line 3 is exiting a document.ready context. If that's the case then your second call to fixedEncodeURIComponent may be getting called before the DOM is even loaded.
Start by wrapping
var TEMP = fixedEncodeURIComponent($("#MessageContainer.Test").val());
$("#Message").val(TEMP);
in a
$(function() {
// code
});
block. If that doesn't work, check that #MessageContainer.Test actually matches an element. Since this is code you inherited, the class name "Test" clues me in that the block in question might be a remnant of someone trying to debug an issue and maybe it should have been removed.
I suspect $("#MessageContainer.Test") since it looks like its supposed to be an ID selector instead of what it actually is when jQUery parses it(which is an ID selector combined with a class selector). $("MessageContainer\\.Test") allows you to select an element with ID MessageContainer.Test
I have a function called 'checkdata(code)' in javascript, which, as you can see, takes an argument called 'code' to run and returns a 15-char string.
So, I found out (and tested) how to call no-argument functions in javascript, but my problem is that when I call checkdata(code), I always get a 'none' return value.
This is what I'm doing so far:
wd = webdriver.Firefox()
wd.get('My Webpage')
a = wd.execute_script("return checkdata()", code) //Code is a local variable
//from my python script
print a
I'm making this, since I read it on an unofficial selenium documentation and here: link
But, as I said before, I just keep getting none printed.
How can I call my function passing that parameter?
Build the string
a = wd.execute_script("return checkdata('" + code + "');")
Rather than building a string (which means you'd have to escape your quotes properly), try this:
a = wd.execute_script("return checkdata(arguments[0])", code)