Call Javascript function from external file with Applescript - javascript

Is it possible to call a javascript file function in applescript? I can get it to work when written 100% in the applescript file or I can use "do javascript file some file". Is there a way to mix the two? Something like this:
tell application id "com.adobe.Photoshop"
tell current document
do javascript "function(Variable1,Variable1)" in file PathtoJSX
end tell
end tell
The function is located in the external file but I want to pass it variables from applescript.
Update:
I posted it when someone else ask but PathtoJSX is the "Path to Javascript file (.jsx)" so PathtoJSX = file "desktop:yourFile.jsx"

I think what you want to do is have the function part of the code in the file and send the arguments via AppleScript as a list (Array), which is pretty easy to do:
The contents of the jsx file could be:
testFunc(arguments);
function testFunc(t) {
alert('argument one: ' + t[0] + '\r' + 'argument two: ' + t[1]);
}
and the AppleScript could be:
tell application id "com.adobe.Photoshop"
activate
do javascript PathtoJSX with arguments {"foo", "bar"}
--I tested using:-- do javascript (choose file) with arguments {"foo", "bar")
end tell
Whatever you use for the JS code (text or file), you just make sure you use the arguments array reference in that code. Note that if you test this (as I did) by using a text variable instead of an alias (file reference), the return character would need a double escape.

Try this:
tell application "Adobe Photoshop CS4"
activate
do javascript (file "/Users/Michael/Desktop/somescript.jsx") with arguments {"foo", "bar"}
end tell
Regards.

Related

Need to convert Web Form JavaScript to a js file

Basically I have three functions embedded into my form. I want to move this into a js file that I have already. I did this previously and my pop ups are working but now I want to move one function which executes the onclick event for a download button and the other two functions belonging to my autocomplete extender so I can display the results how I want them.
I have been messing around but I cannot seem to get this working.
This is my JavaScript function in Web Forms
function Download() {
__doPostBack("<%= btnDownload.UniqueID %>", "OnClick");
}
This is what I have tried in a js file
function Download(button) {
__doPostBack(button, 'OnClick');
}
and this is how I am calling it
ClientScript.RegisterStartupScript(Me.GetType(), "download", "Download(" & btnDownload.ClientID & ");", True)
please can somebody give me a clue to what I am missing and before I forget yes, the file is in my headers
<script type="text/javascript" src="js/importBuyer.js"></script>
The javascript function __doPostBack("<%= btnDownload.UniqueID %>", "OnClick"); expect a string as the client ID of the button. So you need to construct the calling script with ID parameter as string as follows:
ClientScript.RegisterStartupScript(Me.GetType(), "download", "Download(""" & btnDownload.ClientID & """);", True)
Note the double double-quotes ("") above so that the rendered javascript will be like :
Download("btnDownloadID");
From the example you've posted it seems that the webform way was sending the UniqueID and you are sending the ClientID
So you probably only need to send the UniqueID property instead.
I am wondering why should you do that. I would expect you to move from webforms to an API base backend but I can't understand why you want to separate the js integration part while keeping the webforms backend - I would understand if you'd separated the js logic part... not the part that communicates with the webforms
Hoped that helped
Here is a link describing the difference

How can i call a javascript function from a file for pug?

Heres my code but removed some changed code to something smaller!
Javascript code:
// /JS
function callme() {
var test = 1
alert(test);
}
Pug code:
// /first.pug
var funct = require('../JS');
button(onclick='clickme()') click
script.
function clickme() {ยจ
// trying to call callme function from my javascript file but i really dont know how.
callme();
}
Sorry about this question i dont use pug but this was already made with pug so i cannot go changing it since it has alot more code but didnt post all not needed code here.
Pug has no capability to directly run JavaScript. It is used to generate HTML.
You are already generating HTML with embedded client-side JavaScript.
You need to write the HTML to include the external JavaScript.
i.e. <script src="/url/to/JS.js"></script>
In Pug that would be:
script(src="/url/to/JS.js")
Make sure your HTTP server gives the JS a public URL!

How to access the same JSON file from both javascript and python?

NOTE: This is not for web programming. We use javascript to interface with low level hardware, hence let's not go with jQuery APIs etc.
I have a javascript file that performs a sequence of actions on a device, and I have a python file that will be invoked later to validate these actions. There is a set of hardware information hard-coded in both javascript file and python file. I want to avoid this duplication of information by putting these info into a JSON file so both can access it.
// Javascript
var hardware_info = JSON.parse(load('hardware.json'));
// load() is probably not standard javascript API, but it basically copies that code into the existing script.
Already failed by this step because 'hardware.json' is not using javascript syntax...
I already validated the json using jshint/jslint, hardware.json looks like this:
{
"hardware1": {
"ID": "xxx"
},
"hardware2": {
"ID": "yyy"
}
}
The following Python works well for accessing the json, there is not much to it:
with open('hardware.json', 'r') as f:
data = json.load(f)
It looks like load() executes the specified file, not read it and return the contents. If this is your only option to read another file, then I suggest you use JSONP instead of JSON.
JSONP works by adding a callback around the data. Instead of:
{"key": "value"}
the file contains a function call with the data being passed in:
callback({"key": "value"});
This is meant to be executed by a JavaScript engine, causing it to execute the callback. load() would execute your file, and the callback function would be called as a result, passing in the data.
When used on the web, you'd call a JSONP service and pass in the name of the callback the service should add, but when just sharing a configuration file between a JS engine and Python, you'd hardcode that callback name.
In Python, you'd have to strip off the callback text before loading it as JSON data. That could be as easy as just removing the first N and last M characters:
with open('hardware.json', 'r') as f:
jsonp_data = f.read()
# remove first 9 and last 3 characters to remove JSONP callback
data = json.loads(jsonp_data[9:-3])
A little more sophisticated technique could use newlines:
callback(
{"key": "value"}
);
to make it easier to remove the first and last line in Python. Or you could use jsonp_data.partition('(')[-1].jsonp.rpartition(')')[0] to take everything between the first ( and the last ) character in the string. Etc.

Reading file in PERL using ActiveX

I have been trying to read a file in PERL using ActiveX Control. I was successful in reading the file using ActiveX Controls in HTML. So I changed the HTML code to PERL cgi using "Print" Statement. After that I am not able to read the file the file using ActiveX, other JavaScript functions are working good.
print "<script src='/EnvelopeUtility/EnvelopeJS.js'></script>";
Above line of code is used to invoke the external JavaScript.
print "<td><input type='button' name='btn1' value='Process' onClick='getData(document.getElementById('sampleFile').value)'></td>";
Above line of code is an button in HTML, which onClick invokes the JavaScript method getdata()
var obj = new ActiveXObject('Scripting.FileSystemObject');
Above line of code is present in external JavaScript which creates an ActiveX Object to read the file.
readFile = obj.OpenTextFile(path, 1, false);
while(!readFile.AtEndOfStream)
{
readFile = obj.OpenTextFile(path, 1, false);
.................
Above line of code is the one which reads the file line be line.
The above code is working fine in HTML but in CGI after using print statement am not able to read the file. Please do suggest what should be changed in order to read the file.
You have a simple HTML error.
onClick='getData(document.getElementById('sampleFile').value)'
Your attribute value is delimited by ' characters, but you try to use literal ' characters as raw data inside the attribute.
Consequently the value of the attribute becomes getData(document.getElementById( which isn't valid JavaScript.
This would have been picked up if you had used a validator.
If you want to use a ' as data in an attribute value delimited by ' then you have to represent it as '.

Transferring javascript from a view to a seperate JS file

I am working on a legacy application and I want to move some JS code onto a separate JS file.
I will have to refractor some of the code to do this. I can put #Url.Content statements into data attributes in the HTML.
But how would I replace this line of code?
var array = #Html.Raw(Json.Encode(ViewBag.JobList));
A separate JS file will not know what #Html.Raw means.
Server side code like that cannot run in a seperate javascript file. My solution for such problems is having a short javascript part in the head that runs on the onload event. There you can set variables that you can use in a seperate javascript file:
in the head:
array = #Html.Raw(Json.Encode(ViewBag.JobList));
in the seperate javascript file:
var array;
Then, in the seperate javascript file you can do with your array whatever is necessary.
The ViewBag.JobList data is only known at HTML page generation time. To include it in an external JavaScript file, you have to have another ASP.NET resource that recalculated ViewBag.JobList and then served as part of a dynamic JavaScript file. This is pretty inefficient.
Instead, do what you're doing with the URLs: pass the data through the DOM. If you're writing into normal DOM instead of a script block, you don't need the raw-output any more (*), normal HTML escaping is fine:
<script
id="do_stuff_script" src="do_stuff.js"
data-array="#Json.Encode(ViewBag.JobList)"
></script>
...
var array = $('#do_stuff_script').data('array');
// jQuery hack - equivalent to JSON.parse($('#do_stuff_script').attr('data-array'));
(Actually, the raw-output might have been a security bug, depending on what JSON encoder you're using and whether it chooses to escape </script to \u003C/script. Writing to HTML, with well-understood HTML-encoding requirements, is a good idea as it avoids problems like this too.)
I think you need to create action with JavaScriptResult
public ActionResult Test()
{
string script = "var textboxvalue=$('#name').val();";
return JavaScript(script);
}
But, before proceeding please go through following links
Beware of ASP.NET MVC JavaScriptResult
Working example for JavaScriptResult in asp.net mvc
I would also follow MelanciaUK's suggestion :
In your javascript file, put your code inside a function :
function MyViewRefactored( array ){
... your code ...
}
In your view, leave a minimal javascript bloc :
<script>
var array = #Html.Raw(Json.Encode(ViewBag.JobList));
MyViewRefactored( array );
</script>

Categories

Resources