Skip to main content
Inspiring
December 13, 2011
Answered

Illustrator script to open file in Photoshop

  • December 13, 2011
  • 3 replies
  • 9432 views

The Bridge SDK has a script to open a selected file in Photoshop.

Can this  be done from Illustrator instead?

It seems that the cross-dom function open() should work in either an illustrator or bridge script, yet the following does NOT work in Illustrator.

Fresh from the Bridge 5.1 SDK, it doesn't work in Bridge either, but that's for a different forum.

var t = app.activeDocument.fullName;

Photoshop.open(new File(t));

It provides me with Error 2: Photoshop is undefined.

So, any ideas on how to make this work?

This is all leading up to the real problem, which is to create a 150dpi jpg file, which Illustrator won't do.

Thanks All.

--Alex

This topic has been closed for replies.
Correct answer Muppet Mark

There are several ways in which you could get you output *jpeg to 72 dpi at a given set of dimensions… On way would be to scale the whole art in AI before exporting it… Up to you if you save this change or not… You can have PS open the image and alter the resolution after exporting… Or on the mac you could use some utility like the shell SIPS which you also have AppleScript access to via Image Events scripting… Anyhows here is a very basic example of how th pass a file object to PS using the bridgetalk messaging system… The Adobe way of doing it so to speak… I included a filp just so's you could see the changes made… Have fun…

#target illustrator

var aiFile = File( '~/Desktop/zdfb.jpg' );

var psScript = 'app.displayDialogs = DialogModes.NO;';

psScript += 'app.open(' + aiFile.toSource() + ');';

psScript += 'app.activeDocument.resizeImage( undefined,undefined,72,ResampleMethod.NONE);';

psScript += 'app.activeDocument.flipCanvas( Direction.HORIZONTAL );';

psScript += 'app.activeDocument.close( SaveOptions.SAVECHANGES );';

psScript += 'app.displayDialogs = DialogModes.ALL;';

//$.writeln( psScript );

btMessaging( 'photoshop',psScript );

function btMessaging( targetApp, script ) {

          var bt = new BridgeTalk();

          bt.target = targetApp;

          bt.body = script;

          bt.onResult = function( inBT ) { alert( 'Done…' ) };

          bt.onError = function( inBT ) { alert( 'NOT Done…' ) };

          bt.send( 20 );

};

There are numerous ways in which you can construct the script 'string' contents on the fly but you get the general idea…

3 replies

aklymanAuthor
Inspiring
December 16, 2011

Okay, things I've learned: A) No scripting language will export a dimensioned JPG from Illustrator CS5.1, only a scaled one. B) AppleScript can call an Action directly, but JavaScript can't. C) I need to to be drawing more and scripting less, since abstract makes SO much more sense than these holes in scripting support.

I have worked out a solution to my export of a correct sized and resolution JPG:

1) I created a JavaScript that calls an AppleScript (thanks Carlos, you showed how to do this in VB, and Mark, you chimed in last year how to do this in AppleScript:

     var file = File('/Applications/Adobe Illustrator CS5.1/Presets.localized/en_US/Scripts/AlexAutomation/StandardExport.app');

     file.execute();

2) I created an AppleScript, which I saved as an .app (NOT .scrp) and stored conveniently in with my JavaScript. Unfortunately, this puts it in the scripts menu too, although it errors when it runs (I may move it later, but for now keeping them together to make it easier to install elsewhere).

    tell application "Adobe Illustrator"

    activate

    do script "Export Standard JPG" from "Standard Actions"

    end tell

3) I created an Action in Illustrator with my JPG export presets, named "Export Standard JPG" and in the set "Standard Actions" that I created to do some other things.

Works fine, but now I feel like I need to take a shower.

Thank you Mark and Carlos for having discussed the multiple-script approach in other posts.

Muppet MarkCorrect answer
Inspiring
December 17, 2011

There are several ways in which you could get you output *jpeg to 72 dpi at a given set of dimensions… On way would be to scale the whole art in AI before exporting it… Up to you if you save this change or not… You can have PS open the image and alter the resolution after exporting… Or on the mac you could use some utility like the shell SIPS which you also have AppleScript access to via Image Events scripting… Anyhows here is a very basic example of how th pass a file object to PS using the bridgetalk messaging system… The Adobe way of doing it so to speak… I included a filp just so's you could see the changes made… Have fun…

#target illustrator

var aiFile = File( '~/Desktop/zdfb.jpg' );

var psScript = 'app.displayDialogs = DialogModes.NO;';

psScript += 'app.open(' + aiFile.toSource() + ');';

psScript += 'app.activeDocument.resizeImage( undefined,undefined,72,ResampleMethod.NONE);';

psScript += 'app.activeDocument.flipCanvas( Direction.HORIZONTAL );';

psScript += 'app.activeDocument.close( SaveOptions.SAVECHANGES );';

psScript += 'app.displayDialogs = DialogModes.ALL;';

//$.writeln( psScript );

btMessaging( 'photoshop',psScript );

function btMessaging( targetApp, script ) {

          var bt = new BridgeTalk();

          bt.target = targetApp;

          bt.body = script;

          bt.onResult = function( inBT ) { alert( 'Done…' ) };

          bt.onError = function( inBT ) { alert( 'NOT Done…' ) };

          bt.send( 20 );

};

There are numerous ways in which you can construct the script 'string' contents on the fly but you get the general idea…

aklymanAuthor
Inspiring
December 19, 2011

Thanks Mark, that's exactly the structure of what I was trying to do when I opened this thread, so I've marked yours as the correct answer. This was a great intro to BridgeTalk, I see that the psScript is essentially a string with all of the instructions I want to send to PS, and the btMessaging function is what is executing the string once in PS.  Very nice and easier to follow that I expected.

I'm going to use this approach for more complicated PS modifications, but use my JS/AS/Action solution for a single-App approach.

Thanks for laying this out for me, I really appreciate it.

-_Alex

Larry G. Schneider
Community Expert
Community Expert
December 13, 2011

You can Export a jpeg file to any resolution you wish, just not do a save using Save for Web & Devices. And you are right, you can't script it.

aklymanAuthor
Inspiring
December 13, 2011

Thanks Larry. I left out in my descriptions that I wanted to export the jpg using Javascript (I know, I made an assumption that it would be understood). Unfortunately, javascript in Illustrator doesn't offer  exportOptions.resolution for the exportFile method if you have JPEG as the ExportType. The best it can do is horizontalScale and verticalScale, which can give me the number if pixels I want, but only at the default dpi (72dpi).

I believe (possibly incorrectly, I haven't done enough homework yet) that Photoshop does not have this same problem of resolution scriptability when saving as a JPG, which is why I'm trying to connect Illustrator to Photoshop in the fewest possible steps (not relying on Bridge).

Thanks

Alex

aklymanAuthor
Inspiring
December 13, 2011

God forbid, I may have to write an AppleScript to call an Action. Maybe that would be easier, but not as elegant or as fun as really learning Javascript for once.

CarlosCanto
Community Expert
Community Expert
December 13, 2011

try this

var t = app.activeDocument.fullName;

photoshop.open(new File(t));

aklymanAuthor
Inspiring
December 13, 2011

Carlos, thanks...just tried it, still spits out the same error. Is the only change between what we posted the capitalization of the App name?