Writing an running a batch file from Photoshop

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

byronnash

Writing an running a batch file from Photoshop

Post by byronnash »

I need to have PS create and run a batch file so that I can automatically trigger a script in After Effects. I'll be using the file.execute() method to fire off a batch file. I am on a PC and I understand the windows part of it. I do not have a mac to test on so I'm having to do some research.

Do you think this would work on a mac?
Code: Select allvar exe_s = new File("testing.sh");
exe_s.open("w");
exe_s.writeln("MacHD/Applications/Adobe After Effects/AfterFX.exe -s alert('hello');");
exe_s.writeln("rm testing.sh");
exe_s.close();
exe_s.execute();
xbytor

Writing an running a batch file from Photoshop

Post by xbytor »

Things work differently on a mac. The script has to have is execute bit set and there is now way to do that from JavaScript. Which means you have to do a little prep work.

The first step is to create a shell script that you run to install the shell-script-wrapper. I put mine under the csx folder since I already had other stuff there. You could just set it to Presets/Scripts.

Code: Select all#!/bin/bash
#
# Change this if you have Photoshop CS2 installed somewhere else
# or if  you want the script wrapper installed somewhere else.
#
IDIR="/Applications/Adobe Photoshop CS2/Presets/Scripts/csx"

if [ ! -e "$IDIR" ]; then
   echo Error: Missing target exec directory.
   exit 1
fi

cd "$IDIR"
mkdir -p macexec.app
mkdir -p macexec.app/Contents
rm -f macexec.app/Contents/macexec
touch macexec.app/Contents/macexec
chmod 755 macexec.app/Contents/macexec

cat > macexec.app/Contents/macexec.env << EOF
#!/bin/bash
# insert any env variables or path changes you need here...

# If you were calling ImageMagick, you would need something like this
#export MAGICK_HOME=$MAGICK_HOME
#export PATH="\${MAGICK_HOME}/bin:\${PATH}"
#export DYLD_LIBRARY_PATH="\${MAGICK_HOME}/lib"
EOF

echo Mac exec prep work completed

# EOT


After this is ready, you can now invoke scripts in the Mac using this code from xtools: http://ps-scripts.cvs.sourceforge.net/* ... b/xexec.js ... b/xexec.js

And you invoke it like this:

Code: Select all    // set the mac script root. does nothing on WinXP
    Exec.setMacRoot(new Folder(app.path + "/Presets/Scripts/csx"));

    var cmd = "/Applications/Adobe After Effects/AfterFX.exe -s alert('hello');";

    var res  = Exec.system(cmd, 5000); // wait seconds for a response
    alert(res)


-X
byronnash

Writing an running a batch file from Photoshop

Post by byronnash »

Sorry to bug you about this again X, I thought I had a workaround using applescript but that method isn't giving up full keyboard control. I tried what you suggested a while back a got very confused and never got the thing working.

Let me explain how I understood it:

I need to put the first section of code into a shell script with a .sh extension?

I need to have a copy of your xexec.js script in the same folder as my other script(s)?

I'm not sure where the xexec.js script gets called in the last code snippet. I'm assuming this code gets put into my PS script though.
xbytor

Writing an running a batch file from Photoshop

Post by xbytor »

Here is the new config script:
Code: Select all#!/bin/bash
#
# macPSScript.sh
#
# $Id: macPSScript.sh,v 1.1 2007/03/16 19:59:55 anonymous Exp $
#
# This is the Mac exec installation script.
# This creates the necessary files and folders needed to use HP2 mode
# under OSX.
#
# Installation instructions:
#
# To install the macScript support, open a terminal window and:
#
# Run this script:
#   % ./macPSScript.sh
#
# When this script runs, it creates some folders and files underneath
# '/xtools' which allows xtools to call shell scripts from within Photoshop.
# When the script is complete it displays some of configuration information
# that verifies that everything installed correctly.
#
# You can run this script repeatedly with no ill effects.
# To undo its changes, delete the Presets/Scripts/xtools/macexec.app folder and
# all of its contents.
#
# Change this if you have Photoshop CS2 installed somewhere else
IDIR="/Applications/Adobe Photoshop CS2/Presets/Scripts/xtools"

if [ ! -e "$IDIR" ]; then
    mkdir "$IDIR"
fi

cd "$IDIR"
mkdir -p macexec.app
mkdir -p macexec.app/Contents
rm -f macexec.app/Contents/macexec
touch macexec.app/Contents/macexec
chmod 755 macexec.app/Contents/macexec

cat > macexec.app/Contents/macexec.env << EOF
#!/bin/bash
#
# Insert any needed env settings.....
#
EOF

# EOT


You only need to run this once to configure your machine for xtools.
Put xexec.js in the same folder as your script. Then include it in your script like this:

Code: Select all//@include "xexec.js"

Then you need this bit of code to let xexec know where the mac-trick code is at:

Code: Select all    Exec.setMacRoot(new Folder(app.path + "/Presets/Scripts/xtools"));

I'm not sure where the xexec.js script gets called in the last code snippet.

This line:
Code: Select allExec.system(cmd, 5000);
is what calls code in xexec.js.

I'm assuming this code gets put into my PS script though.

Yes.

-X
byronnash

Writing an running a batch file from Photoshop

Post by byronnash »

Thanks for your help x, I feel like an idiot. I'm making some progress but still getting stuck. When trying to run the shell script I kept getting this:
Code: Select alljhollars-power-mac-g5:~/Desktop jhollars$ % ./macPSScript2.sh
-bash: fg: %: no such job
Then I ran it without the "%" and it did not error. Although I received no feedback in the terminal.

Code: Select alljhollars-power-mac-g5:~/Desktop jhollars$ ./macPSScript2.sh

I'm guessing it partially worked because there is a folder in my scripts folder called "xtools" with "macexec" in it.

When I run my photoshop script I'm getting an error of Code: Select allUncaught JavaScript exception: execute failed: "I/O error"
Below is my PS script.Code: Select all//@include "xexec.js"

// set the mac script root. does nothing on WinXP
//alert(app.path);
    Exec.setMacRoot(new Folder(app.path + "/Presets/Scripts"));



    var cmd = "/Applications/Adobe After Effects 7.0/Adobe After Effects 7.0.exe -s alert('hello');";



    var res  = Exec.system(cmd, 5000); // wait seconds for a response

    alert(res)

I really appreciate your help. I definitely owe you a drink or something!
xbytor

Writing an running a batch file from Photoshop

Post by xbytor »

The Unix command line is very picky about spaces in file names. Try this:

Code: Select allvar cmd = "\"/Applications/Adobe After Effects 7.0/Adobe After Effects 7.0.exe\" -s alert('hello');"

-X
byronnash

Writing an running a batch file from Photoshop

Post by byronnash »

I'm not sure the sh file ran correctly. Should there be more that just macexec in the xtools folder? Also, I'm not entirely sure what the filename is for After Effects. On the PC it's "AfterFX.exe". On a mac, there is a file called "Adobe After Effects 7.0.app". It's not clear how to reference it in scripting.
xbytor

Writing an running a batch file from Photoshop

Post by xbytor »

There should be a file
Code: Select allPresets/Scripts/xtools/macexec.app/Contents/macexec

It should contain this text:
Code: Select all#!/bin/bash
#
# Insert any needed env settings.....
#

-X