/    Sign up×
Community /Pin to ProfileBookmark

can Javascript call unix command?

Hello,

I am creating an archive disk of a site to be used locally on the user’s machine. I have a php search script that uses grep via shell_exec() for searching the site.

I know very little about Javascript, and wonder if Javascript is able to call unix commands. If it seems feasible, I would be amenable to learning Javascript and rewriting the script in Javascript to be used on the client side.

Is the unix call possible?

Allasso

to post a comment
JavaScript

21 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jun 04.2009 — No, JavaScript cannot interact with the server on its own. JavaScript can call PHP, and get information from it though, it's called AJAX.
Copy linkTweet thisAlerts:
@AllassoauthorJun 04.2009 — thank you, Declan.

Perhaps I did not state my intention clearly. I am creating an archive disk that can be copied onto anyone's computer and be used locally without a server. Since Macs are UNIX based, I thought if there was a way for Javascript (or any other client-side browser programming language) to execute system commands on the local machine, I could call up my search script and have the same browser based search capabilities that it had while being hosted on the server (at least if someone was using a Mac).
Copy linkTweet thisAlerts:
@Declan1991Jun 04.2009 — No, JavaScript runs in a sand-box, meaning that it cannot interfere with the user's computer. So JavaScript cannot be used as a server-side language, regardless of whether it's local or not. But JavaScript can call different PHP (or any other server side language), and get input from them. So if you write a PHP script to search, JavaScript, through a method known as AJAX, can access the search results.
Copy linkTweet thisAlerts:
@AllassoauthorJun 05.2009 — Thank you, Declan,

I looked into AJAX, and as far as I can tell, it would still be trying to run php on the server side, which I think is what you just said. The problem is, I want to run it on the local side. Mac OSX comes with a PHP module, but I don't know any way of implementing it without using the Apache webserver, putting the page in the server path, and accessing it through localhost, which I don't want the user to have to do (most users I am dealing with wouldn't have a clue how to anyway). I was hoping there was a way of implementing it locally through the web browser without going through all of that. I would like them to be able to stick the CD in, drag a folder to their computer and go. Of course I would have to be able to input data to the script through a post or get request as well.

It's looking like I am wanting something that just can't be done.
Copy linkTweet thisAlerts:
@Declan1991Jun 05.2009 — I don't understand exactly what you want, but a small portable server seems to be in order. Maybe the PHP forum could help you find a small portable PHP installation that could run automatically? Then it wouldn't be difficult to control from the browser.
Copy linkTweet thisAlerts:
@rnd_meJun 06.2009 — you can setup your browser so that javascript can run executables, but you won't be able to get any return from the application.

you can pass a fair amount of data to the executable this way; about 50kb in firefox.

you just need to open a window under a specific mimeType, and associate your application with that type when prompted.

firefox offers you "do this each time"...

after the first time, your application will run without interaction every time that mimeType is opened.

you can use this function to fire a certain application (choose first time):
[CODE]function download(data){
window.open("data:x-application/myApp," + escape(data));
}[/CODE]


it takes one argument, the data (just like alert)...
Copy linkTweet thisAlerts:
@AllassoauthorJul 26.2009 — Hello, rnd,

I must have missed your reply way back then. I kind of gave up since then, but then thought I would do another search on it, and came across your reply.

Sounds like your little trick might work. If I can just launch a shell script and pass some arguments to it, I can use the script to do the rest: do the search, write an html page with the results, and open up the page in a new tab in the browser.

I just need to figure out how to get the form info (ie, the user inputted search terms) into the javascript. I guess I might need to learn some javascript.

Thanks.
Copy linkTweet thisAlerts:
@rnd_meJul 26.2009 — Hello, rnd,

I just need to figure out how to get the form info (ie, the user inputted search terms) into the javascript. I guess I might need to learn some javascript.

Thanks.[/QUOTE]



[CODE]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>shell open demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<h1>Search for something</h1>

<form>
<input name='search' />
<input value='Search' type='button' onclick="download(this.form.search.value)" />
</form>

<script type='text/javascript'>
function download(data){
window.open("data:x-application/myApp," + escape(data));
}
</script>

</body>
</html>[/CODE]


tested FF3.5...

when i click search, a popup asks me to "open or save".

i choose open with, selected notepad, clicked ok, and the input's text appeared verbatim in notepad.
Copy linkTweet thisAlerts:
@AllassoauthorJul 27.2009 — thank you, rnd_me,

It works just as you say.

I need to work out a few things still.

I cannot get FF to show my shell script as an option for an app to open with. I have tried changing the mime type to application/x-bsh, text/x-script and putting an .sh extension on the script, but to no avail.

I also notice that when data is entered in the form, if the return key is pressed, nothing happens, I only get the option of opening an application if the "search" button is pressed. This could be a problem since folks are used to entering data with the return key.

The other little pesky thing is that the downloads window pops up. Is there a way to stop that from happening?

Thanks,

Allasso

ps, replies may be delayed at times as I have to go to another location to get online.
Copy linkTweet thisAlerts:
@rnd_meJul 27.2009 — 
I cannot get FF to show my shell script as an option for an app to open with. I have tried changing the mime type to application/x-bsh, text/x-script and putting an .sh extension on the script, but to no avail.
[/QUOTE]

you should be able to manually browse to the folder where the app is.

firefox's application browse dialog only displays and accepts EXEs on windows, not sure about other systems. you might have to convert the script into an application. in window, there is a free tool called bat2com which turns batch files into .com files, which can be re-named to .exe and found by firefox. Again, i'm not sure about other systems, though i would be surprised if this was impossible to do.



I also notice that when data is entered in the form, if the return key is pressed, nothing happens, I only get the option of opening an application if the "search" button is pressed. This could be a problem since folks are used to entering data with the return key.
[/QUOTE]

add an event handler to the input box (google for info), or wrap your inputs in a form, duping the button's onclick to the form's onsubmit, adding "return false;" after the download() call.



The other little pesky thing is that the downloads window pops up. Is there a way to stop that from happening?
[/QUOTE]

this is a browser setting that can be changed in the config menu, but it's not something that can be controlled by javascript alone afaik.

glad it's somewhat working, keep plugging away, and good luck!
Copy linkTweet thisAlerts:
@AllassoauthorJul 27.2009 — okay, thanks, lot of good information here. My script is written for unix, so I am limited to dealing with Unix OS's here. I can browse to where the script is, but it is greyed out, but I think you are right, I am going to have to change it into an app, maybe wrap it in Applescript or something.

Thank you for the light at the end of the tunnel, and your investment here.

Allasso
Copy linkTweet thisAlerts:
@AllassoauthorAug 03.2009 — Update:

I wrapped the script as an app using Platypus, works great that way, Platypus is a nice little tool. Even gives a little barber pole.

Can't pass args to it explicitly, though, so I am writing the search parameters to cookie(s) and using grep and perl to extract them from the cookie file. Maybe there is a more elegant way to do that, but it works.

Just need to clean up the .part files as they are made, need to find a way to detect the default downloads directory. (don't know why FF doesn't do that)

Wish I could stop the downloads progress dialogue from popping up, but as long as the user just pushes it to the background rather than closing it out, they'll never see it.

The script is pretty simple, so maybe I will toy with doing something for windows. Don't they use batch files or something like that that work like shell scripts? Do they have grep and sed and perl with XP or Vista? What about PHP CLI? I could just do the whole thing in a PHP script.

Thanks for your help, RND, that was just the crack I needed.

Allasso
Copy linkTweet thisAlerts:
@rnd_meAug 04.2009 — 
The script is pretty simple, so maybe I will toy with doing something for windows. Don't they use batch files or something like that that work like shell scripts? Do they have grep and sed and perl with XP or Vista? What about PHP CLI? I could just do the whole thing in a PHP script.

Thanks for your help, RND, that was just the crack I needed.

Allasso[/QUOTE]


php, and perl are available for window.

I like jscript.net for scripting as well.

you can make a grep from a batch file wrapper on findstr.exe; the closest thing to grep.

no sed afaik, but vim does have a scriptable command-line version that works nicely.

as you may have guessed from the randomly-named .part files, firefox doesn't want you knowing the default downloads folder; that could compromise privacy.

there is likely an environment variable that can get you at least a folder or two above the Firefox download directory.

you might also look into auto-it for windows, it lets you create simple executables that work somewhat like macros. might be all you need to mess with to get up and running.

cheers!
Copy linkTweet thisAlerts:
@AllassoauthorAug 04.2009 — tyvm

I was going to try creating a download directory with an unusual name, and then grepping the directories I thought it might be,but when I try to change the download directory in the prefs, they don't seem to take... both on FF 3.0 and 3.5 (on Macs)

It's going good though, finally learning Javascript after all this time. Once I got over the initial shock, it wasn't too bad...
Copy linkTweet thisAlerts:
@savvykmsAug 04.2009 — Is the whole procedure of shell execution needed? If you are just trying to search (sorry of I am wrong):

It is pretty easy to code a client-side search engine with limited functionality. Since no new content is going to be added if it is just a static copy from a CD, then my suggestion would be a client-side javascript search engine with an [B]Array[/B] of [B]Array[/B]s of strings. 1 array for each page within an array for the site's pages. So then you just make a search function which enumerates the array and via keywords and maybe descriptions/titles too matches to a user's input from a text field (split by spaces using [B]split(" ").[/B]
[code=html]var searchTable = new Array(
new Array("key words here", "Title Goes Here", "Description goes here.", "key words go here"),
//...
);[/code]
Copy linkTweet thisAlerts:
@AllassoauthorAug 08.2009 — Does anyone know...

The project is going good. I thought I might fish for some more suggestions however, as I haven't been able to come up with an answer to this questions:

Overview: I have the search form page, when form is submitted, the search parameters are written to a cookie, which is then retrieved by the search script running on the local system. (for gory details on how the script is launched, see above)

I originally was returning the search results by using: open -a Firefox file. This however opens the results up in a new tab or window, which I would prefer not.

I can use a page redirect to go to the search results page after the script runs, and then the results will open up in the same page, great.

However, I need to be able to delay the redirect until the search is completed, time which can vary greatly (very large archive). My thought is to do this by setting a cookie from the search script (running on local system) to flag the javascript (running in browser) when the search is completed. The javascript can keep looping and checking for the flag, and when it is true, go ahead and open the search results file in the browser.

Firefox uses an SQLite database file for cookies, which makes it a little bit tougher than just dealing with a text file. It is actually a binary file, however, the cookie name and value show up in ASCII characters, so it is possible to find the values and extract them using sed and perl. This is how I pass the search parameters to the search script in the first place. I contain the value of the cookie in two obscure strings (eg jfhrutifj875869-cookie value-jdueifkf85746) and this way I can locate them in the midst of all the binary stuff by doing a regex search for the two identifiers. Works great.

So in order to send a cookie back, I tried doing this by first setting a cookie with javascript in the same way, and doing a regex search for that value. I don't have any problem finding the cookie, but when I try to replace it, the cookie stops working. I believe I am not botching the search and replace.

I don't really know anything about SQLite, so if someone can enlighten me on how to deal with this, I would much appreciate it. The point is, I would like to write to Firefoxes cookie database from a shell script. If anyone knows any way whatsoever to do this, it would make me very happy.

Thanks, Allasso
Copy linkTweet thisAlerts:
@AllassoauthorAug 09.2009 — thank you, savvykms,

I appreciate your suggestion. If you know of a way for javascript to read the local filesystem on a Mac, I am all ears. That would help immensely.

As far as I know, it is not possible for javascript to do that on a Mac at all. I have not begun developing this for PCs, yet, but I guess there is a way to access the local filesystem on a PC. But whatever I use this on, I would like it to work right out of the box. I would rather not have to have someone install some additional utility to make it work.

At any rate, it is working now, and I am just smoothing out some things as mentioned above. I am not interested in limited functionality, however. The script I am using is quite comprehensive. This is a very large archive as well. Also, it seems to me that a script running on the local system would be much faster than one running in the browser. But I may be wrong about that.

I appreciate your input. As I said, if you know of a way to read the local filesystem, please let me know.

Allaso
Copy linkTweet thisAlerts:
@rnd_meAug 09.2009 — you can load scripts from the local hard drive.

i had an exe that spit out a javascript call to monitor status.

the default text of the file (while it was busy) was something like setTimeout("myscr.src='file:///monitor.js'", 500);

in this fashion, it repeatedly called for re-loading itself...

directories always have weird URL, so drop a .htm file into the folder, and open it with firefox. copy the url path from the adress bar, and use it for your js callback.

once i got the data i needed,
Copy linkTweet thisAlerts:
@AllassoauthorAug 09.2009 — I'm not really clear on what you are saying, but I did something that sounds like maybe it was the same thing. It was simple really, I just had the search script load an initial js file as the results page which uses setTimeout to keep refreshing the page every 5 secs. Then once the search results are written into the file, they appear on the next refresh and the refreshing ends.

Was there more you were going to say? Looks like your post ended abrubtly.

I'm still interested in an answer to my other questions though. I can see it might be helpful to be able to have the browser and the local system communicate back and forth.
Copy linkTweet thisAlerts:
@AllassoauthorAug 09.2009 — I just wanted to clarify (simplify) one of my questions. Is it possible for a script to extract data from a file that has been loaded and use it explicitly?

In other words, can data be passed to a script by placing it in the file to be loaded, and then having the script read it somehow?

Thanks again, Allasso
Copy linkTweet thisAlerts:
@AllassoauthorAug 10.2009 — you can load scripts from the local hard drive.
[/QUOTE]


Duh, I guess that kinda answers my last question...

Sometimes I kinda get lost in the soup, and miss the obvious.
×

Success!

Help @Allasso spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...