File.execute Tutorial

Documentation, Reference material and Tutorials for Photoshop Scripting

Moderators: xbytor, DavideBarranca, Paul MR

punkard

File.execute Tutorial

Post by punkard » Tue Feb 12, 2008 10:56 pm

This tutorial is great, however I do have a question...is it possible to return whatever is returned in a cmd window to a string?

For instance, while using a program called perforce,

If I type "p4.exe add [filename]" in a command prompt window, it will return "[file] opened for add"

How would I return that to a string as the 'bat.execute()' just returns a value of 'True'

Cheers

xbytor

File.execute Tutorial

Post by xbytor » Tue Feb 12, 2008 11:33 pm

Internal, the code executes something like this on the command line:

Code: Select all   p4.exe add filename > results.txt

and it returns the contents of results.txt as the return value. If the output from p4.exe is not redirectable, I have no way of capturing it.

Also Exec.execute doesn't wait for return a value: it returns immediately and the exe called runs in parallel. Exec.system, on the other hand, is synchronous and probably what you really want to use.

-X

stEEdZ

File.execute Tutorial

Post by stEEdZ » Sun Mar 08, 2009 11:11 pm

Hi,
does anyone know how to start a compiled c++ file on an Mac Os?
Code: Select allvar myxmp = new File ("/Users/meep/Documents/workspace/ModXMP");
myxmp.execute();


the command to start this file in Terminal is "./ModXMP". But im just not able to transfer this command into the windows execute code.

thx a lot for your help!
steedz

xbytor

File.execute Tutorial

Post by xbytor » Mon Mar 09, 2009 4:13 am

Your code looks fine. If the program is executable, it should run. An alternative would be:

Code: Select allapp.system ("/Users/meep/Documents/workspace/ModXMP");

Mr_White

File.execute Tutorial

Post by Mr_White » Fri Mar 20, 2009 10:25 am

I have the same problem on Mac osX.
Solution tempraire which I use:
In the preferences of the file, in the "open with" select the terminal (only this file).

and that work.

var shscript = new File ("/Users/myscript.sh");

shscript.execute

and the script work with the terminal correctly (equal to ./myscript.sh).

*sry for my bad english :p

xbytor

File.execute Tutorial

Post by xbytor » Fri Mar 20, 2009 3:17 pm

Then general problem with creating and executing shell scripts from with PS/JS is that you have no means of setting the 'execute' bit. This means that you have to write a wrapper script ahead of time that will read a file for a command line that includes your script as an argument. This actually works pretty well.

Another alternative is to use the app.system command and do something like app.system(". myscript.sh"). In theory, this should work as well but this is also a synchronous call unlike File.execute.

cobracobra

File.execute Tutorial

Post by cobracobra » Mon Sep 20, 2010 4:27 pm

A brilliant tutorial!

But is there a way to run .exe silently? I mean - every time I start to execute a file - a Windows console pops ups no matter what. Even if I try to run notepad.exe... Is there a way to run an .exe without any windows?

Thanks!

ddyer

File.execute Tutorial

Post by ddyer » Thu Aug 29, 2013 9:03 pm

-- the bad news:
The whole methodology of this hack is suspect - creating a batch file and executing it is a very malware-friendly technique, and someone, somewhere will find a way to use it that way. I expect a future security patch to break any scripts that use this method. Also, as far as I can determine, it already doesn't work at all on macs. Not only are the details of scripting very different, but even if you get the details right, there's no way to execute the resulting script.

-- the good news:
I found a better way. Assuming you have control over the application you want to run, use $.setenv({key},{name}) to pass arguments to the application via the environment strings. This works for both mac and pc and doesn't require creating new executables at runtime.

Mike Hale

File.execute Tutorial

Post by Mike Hale » Thu Aug 29, 2013 10:02 pm

1. This is an old thread so the code may need updating. You can check the current version of xtools, it may have already been updated.
2. Being able to run another app from extendscript is so useful that Adobe has added an app method since this was written.
3. Any script can be a security risk. You should only run scripts from trusted sources.

I am unclear about how your approach works. Can you post an example? How do you call the app and pass it the env string? What if you need the called app to pass something back and it doesn't support setting env stings?

ddyer

File.execute Tutorial

Post by ddyer » Fri Aug 30, 2013 12:14 am

> 2. Being able to run another app from extendscript is so useful that Adobe has added an app method since this was written.

If adobe wanted to make "exec" a useful, standard part of the environment, making argulent passing and output
capturing part of the spec (and eliminating the need for kludges like this) would be step 1.

>3. Any script can be a security risk. You should only run scripts from trusted sources.

Being able to execute an existing executable is indeed useful, but being able to write your own script and execute it
is a major risk. The risk here is not that your installed javascripts are rogues, but that the random javascripts you
download via browsers will use the same capabilities, even though they're not supposed to be able to.

>I am unclear about how your approach works. Can you post an example? How do you call the app and pass it the env string? What if you need the called app >to pass something back and it doesn't support setting env stings?

AFAIK setting the env strings is a one-way street, you can set them and pass data in, but the target app can't use them to pass
results back. I pass the names of desired result files as one of the arguments.

in javascript, approximately:
$.setenv('arg1_name','arg1_value')
new File("appname").exec()

and in the app:
char *arg = getenv('arg1_name')

Post Reply