system.callSystem on windows

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

i am trying to port a script to windows and am not having good luck with system.callSystem

On mac it's very straight forward and works as i'd expect it to, but on windows, this scripts gives me 2 errors:

Code: Select all

var osString = "ECHO YO";
var osCapture = system.callSystem(osString);
alert(osCapture)

After Effects warning: ERROR: The system cannot find the file specified.

then

Script Alert: undefined



Does system.callSystem work differently on windows?
Darkmoon_UK
Posts: 62
Joined: September 5th, 2006, 3:45 am
Location: Chiswick, London, UK
Contact:

Hello,

Yes I found the same as you. It seems that the behaviour of callSystem is to always look for a program with that name, whereas what you are probably wanting to execute is a 'DOS' style command.

I came up against this myself when automatically exporting frames of an animation using the BMP output module and wanting to strip the zeroes off the end e.g. where the standard output module gives frame.bmp00023
The solution is to run the command prompt itself, giving it the '/c' argument which lets you run a command as the next argument. Like so:

var myCommand = "echo Hello"

system.callSystem("cmd /c \""+myCommand+"\"");

Now, all those slashes and quotes look a bit tricky, I agree, but here's whats going on.... If you just wrote this:

system.callSystem("cmd /c "+myCommand);

The system would think that your 'command' was only the word 'echo' since there is a space after it, and spaces separate parameters, don't they?

What you need to do is put your command (our variable myCommand) into quotes, as cmd sees it, thus making it one command string. For clarity, the system must see this:

cmd /c "echo Hello"

Of course, this isn't as simple as just writing the quotes into our JavaScript line because it would confuse the JavaScript interpreter into thinking we're ending our callSystem command. So to insert a quote into the command string and have it remain invisible to JavaScripts interpreter we put an escape character '\' before it.

A limitation of this approach is that only one line can be executed at once. If you needed to execute many commands, especially where they depend on each other, it would be better to have your code write out a batch file which it then calls with this method.

I hope this is clear, good luck, and let me know if what you're trying to do still isn't working.

- Chris
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Ah! that did it.. Thanks for that Chris.. It's hard to figure these things out when you are on a mac and can't try things out.. If only the manual made mention that mac and windows work differently with systemCall... ahh.. one can only dream...

Anyway, there is one more thing to point out. DOS returns a Carriage return and a line feed when you poll it.. so if you are capturing the output for use in something else.. you'll probably want to remove the CR/LF as so:

Code: Select all

var temp = system.callSystem("cmd /c \""+myCommand+"\"");
temp=temp.replace(/(\r|\n)/g, ""); 
now temp will have the clean output from your command.

-Lloyd
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

This thread has been very helpful. I'm getting closer to getting this to work but I'm getting stuck still. Below is my test code. I'm trying to run a script in Photoshop from After Effects. The code below successfully launches Photoshop but doesn't pop up the alert that's in the "target.jsx" script. After Effects is waiting for Photoshop to finish and is frozen until I close Photoshop. The command that I'm building with the strings correctly runs the script from a dos prompt. Any idea why it's not working?

Code: Select all

#target "AfterEffects"
var runString = '"C:\\Adobe\\Adobe Photoshop CS2\\Photoshop.exe"'; 
var sString = ' "C:\\Documents and Settings\Byron\\Desktop\\target.jsx"';
system.callSystem("cmd /c \""+runString+sString+"\"");
alert(runString+sString);
Mylenium
Posts: 139
Joined: July 20th, 2005, 12:07 am

byronnash wrote:This thread has been very helpful. I'm getting closer to getting this to work but I'm getting stuck still. Below is my test code. I'm trying to run a script in Photoshop from After Effects. The code below successfully launches Photoshop but doesn't pop up the alert that's in the "target.jsx" script. After Effects is waiting for Photoshop to finish and is frozen until I close Photoshop. The command that I'm building with the strings correctly runs the script from a dos prompt. Any idea why it's not working?

Code: Select all

#target "AfterEffects"
var runString = '"C:\\Adobe\\Adobe Photoshop CS2\\Photoshop.exe"'; 
var sString = ' "C:\\Documents and Settings\Byron\\Desktop\\target.jsx"';
system.callSystem("cmd /c ""+runString+sString+""");
alert(runString+sString);
Have you tried putting the script in PS' scripts folder? Maybe it cannot resolve the path properly. You can also try routing the execution thru Bridge. Bridge by itself can open and close PS... Might be worth considering.

Mylenium
[Pour Mylène, ange sur terre]
nab
Posts: 203
Joined: November 29th, 2005, 3:00 am
Location: Royan
Contact:

Hey Byron,
you've probably already figured it out
but in case you're still encountering problems, you may try that (adjust to your path):
- first script (launched from AE): "script_1.jsx"

Code: Select all

var appStr = "C:\\Program Files\\Adobe\\Adobe Photoshop CS2\\Photoshop.exe";
var scriptStr = "C:\\Documents and Settings\\NAB\\Desktop\\script_2.jsx";
system.callSystem(appStr + " " + scriptStr);
alert("Back in AE");
-second script (executed in PS): "script_2.jsx"

Code: Select all

#target photoshop
alert("I'm in PS");
photoshop.quit()
Does that work for you ?
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

Thanks for the help. I did get it to work but it was tricky. I had to replace the slashes and also the spaces in the file paths.

Code: Select all

	if (system.osName.indexOf("Windows") != -1){//this is where the pc running stuff goes
				var runString = '"C:\\Adobe\\Adobe Photoshop CS2\\Photoshop.exe"';
				var sString = theFile.absoluteURI.replace(/\//g, "\\");//change the forward slashes to backslashes
				sString = sString.replace(/\\c\\/,'"C:\\');//correct the drive letter, assuming it is on the C drive
				sString = sString.replace(/%20/g," ");//change the %20 to spaces
				sString += '"';
				//alert(runString+" "+sString);
				var runit = system.callSystem("cmd /c \""+runString+" " +sString+"\"");
				if(runit){alert("we ran the PS script")};
}
The problem I'm having now is trying to get AE to wait on the PS script to finish before executing the rest of the script. Is that even possible? I tried putting the callSystem into a variable but it doesn't seem to be working. Ideas?
Post Reply