Skip to main content
Inspiring
December 26, 2013
Answered

Set zoom in CS5

  • December 26, 2013
  • 2 replies
  • 2907 views

We've been using the below Java code to set the zoom of a document in Photoshop 7 (yeah, I know - ancient) to fit the screen (like hitting Ctrl + 0 on the PC) but it no longer works in CS6. The runMenuItem command is generating "Error 1242: Illegal argument - argument 1."

function runMenuItem(item){ // from xbytor

cTID = function(s) { return charIDToTypeID(s); };

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putEnumerated( cTID( "Mn  " ), cTID( "MnIt" ), cTID( item ) );

desc.putReference( cTID( "null" ), ref );

executeAction( cTID( "slct" ), desc, DialogModes.NO );

}

runMenuItem("FtOn");// fit on screen ( same as ctrl-0 )

Is there some other way this can to be done in CS6?

Thanks,

Ken

This topic has been closed for replies.
Correct answer Michael_L_Hale

app.runMenuItem(charIDToTypeID(("FtOn")));

Newer versions of Photoshop have an application method for calling menu items.

2 replies

Inspiring
December 23, 2014

These scripts work in CS6 (at the very least), and will set your zoom levels to 6.25, 8.33, 12.5, 50, 66.67, 100, 200 respectively.

6.25%

function getScreenRes(){

   var ref = new ActionReference();

   ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

   return executeActionGet(ref).getObjectValue(stringIDToTypeID('unitsPrefs')).getUnitDoubleValue(stringIDToTypeID('newDocPresetScreenResolution'))/72;

}

function setZoom( zoom ) {

   cTID = function(s) { return app.charIDToTypeID(s); };

   var docRes = activeDocument.resolution;

   activeDocument.resizeImage( undefined, undefined, getScreenRes()/(zoom/399.7), ResampleMethod.NONE );

   var desc = new ActionDescriptor();

   var ref = new ActionReference();

   ref.putEnumerated( cTID( "Mn  " ), cTID( "MnIt" ), cTID( 'PrnS' ) );

   desc.putReference( cTID( "null" ), ref );

   executeAction( cTID( "slct" ), desc, DialogModes.NO );

   activeDocument.resizeImage( undefined, undefined, docRes, ResampleMethod.NONE );

}

setZoom (25);

8.33%

function getScreenRes(){

   var ref = new ActionReference();

   ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

   return executeActionGet(ref).getObjectValue(stringIDToTypeID('unitsPrefs')).getUnitDoubleValue(stringIDToTypeID('newDocPresetScreenResolution'))/72;

}

function setZoom( zoom ) {

   cTID = function(s) { return app.charIDToTypeID(s); };

   var docRes = activeDocument.resolution;

   activeDocument.resizeImage( undefined, undefined, getScreenRes()/(zoom/300.1), ResampleMethod.NONE );

   var desc = new ActionDescriptor();

   var ref = new ActionReference();

   ref.putEnumerated( cTID( "Mn  " ), cTID( "MnIt" ), cTID( 'PrnS' ) );

   desc.putReference( cTID( "null" ), ref );

   executeAction( cTID( "slct" ), desc, DialogModes.NO );

   activeDocument.resizeImage( undefined, undefined, docRes, ResampleMethod.NONE );

}

setZoom (25);

12.5%

function getScreenRes(){

   var ref = new ActionReference();

   ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

   return executeActionGet(ref).getObjectValue(stringIDToTypeID('unitsPrefs')).getUnitDoubleValue(stringIDToTypeID('newDocPresetScreenResolution'))/72;

}

function setZoom( zoom ) {

   cTID = function(s) { return app.charIDToTypeID(s); };

   var docRes = activeDocument.resolution;

   activeDocument.resizeImage( undefined, undefined, getScreenRes()/(zoom/200.0), ResampleMethod.NONE );

   var desc = new ActionDescriptor();

   var ref = new ActionReference();

   ref.putEnumerated( cTID( "Mn  " ), cTID( "MnIt" ), cTID( 'PrnS' ) );

   desc.putReference( cTID( "null" ), ref );

   executeAction( cTID( "slct" ), desc, DialogModes.NO );

   activeDocument.resizeImage( undefined, undefined, docRes, ResampleMethod.NONE );

}

setZoom (25);

50%

function getScreenRes(){

   var ref = new ActionReference();

   ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

   return executeActionGet(ref).getObjectValue(stringIDToTypeID('unitsPrefs')).getUnitDoubleValue(stringIDToTypeID('newDocPresetScreenResolution'))/72;

}

function setZoom( zoom ) {

   cTID = function(s) { return app.charIDToTypeID(s); };

   var docRes = activeDocument.resolution;

   activeDocument.resizeImage( undefined, undefined, getScreenRes()/(zoom/50), ResampleMethod.NONE );

   var desc = new ActionDescriptor();

   var ref = new ActionReference();

   ref.putEnumerated( cTID( "Mn  " ), cTID( "MnIt" ), cTID( 'PrnS' ) );

   desc.putReference( cTID( "null" ), ref );

   executeAction( cTID( "slct" ), desc, DialogModes.NO );

   activeDocument.resizeImage( undefined, undefined, docRes, ResampleMethod.NONE );

}

setZoom (25);

66.67%

function getScreenRes(){

   var ref = new ActionReference();

   ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

   return executeActionGet(ref).getObjectValue(stringIDToTypeID('unitsPrefs')).getUnitDoubleValue(stringIDToTypeID('newDocPresetScreenResolution'))/72;

}

function setZoom( zoom ) {

   cTID = function(s) { return app.charIDToTypeID(s); };

   var docRes = activeDocument.resolution;

   activeDocument.resizeImage( undefined, undefined, getScreenRes()/(zoom/37.5), ResampleMethod.NONE );

   var desc = new ActionDescriptor();

   var ref = new ActionReference();

   ref.putEnumerated( cTID( "Mn  " ), cTID( "MnIt" ), cTID( 'PrnS' ) );

   desc.putReference( cTID( "null" ), ref );

   executeAction( cTID( "slct" ), desc, DialogModes.NO );

   activeDocument.resizeImage( undefined, undefined, docRes, ResampleMethod.NONE );

}

setZoom (25);

100%

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

doMenuItem = function(item, interactive) {

   var ref = new ActionReference();

   ref.putEnumerated(cTID("Mn  "), cTID("MnIt"), item);

   var desc = new ActionDescriptor();

   desc.putReference(cTID("null"), ref);

   try {

     var mode = (interactive != true ? DialogModes.NO : DialogModes.ALL);

     executeAction(sTID("select"), desc, mode);

   } catch (e) {

     if (!e.message.match("User cancelled")) {

       throw e;

     } else {

       return false;

     }

   }

   return true;

}

doMenuItem(cTID('ActP')); // Set Zoom to 100%

;doMenuItem(cTID('ZmIn')); // Zoom in on time more. (100 %)

200%

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

doMenuItem = function(item, interactive) {

   var ref = new ActionReference();

   ref.putEnumerated(cTID("Mn  "), cTID("MnIt"), item);

   var desc = new ActionDescriptor();

   desc.putReference(cTID("null"), ref);

   try {

     var mode = (interactive != true ? DialogModes.NO : DialogModes.ALL);

     executeAction(sTID("select"), desc, mode);

   } catch (e) {

     if (!e.message.match("User cancelled")) {

       throw e;

     } else {

       return false;

     }

   }

   return true;

}

doMenuItem(cTID('ActP')); // Set Zoom to 100%

doMenuItem(cTID('ZmIn')); // Zoom in on time more. (200 %)

Known Participant
December 23, 2014

Now if we could just set the keycenter value...

Michael_L_HaleCorrect answer
Inspiring
December 26, 2013

app.runMenuItem(charIDToTypeID(("FtOn")));

Newer versions of Photoshop have an application method for calling menu items.

TNTGuysAuthor
Inspiring
December 26, 2013

That worked perfectly Michael, thanks so much.

I did a quick search but didn't find anything by way of a list of these. I found one reference that mentioned a PDF in which the ScriptListener was described. I've download it and am about to give it a try.

Do you know if there IS, indeed, a list of these somewhere?

Again, many thanks,

Ken

Inspiring
December 26, 2013

There are a couple of lists but I think the best way to find the IDs you need is to use the scriptlistener plug-in for things that can be recorded to the log. Some things don't log, so for those you can try using 'insert menu item' in an action, save the action, then either look at the action file with an hex editor or use xbytor's xtools to convert the action file to javascript.

Here is a list of view related IDs

charIDToTypeID("FtOn"); // fit window

charIDToTypeID("ActP"); // actual pixels

charIDToTypeID("ZmIn"); // zoom in

charIDToTypeID("ZmOt"); // zoom out

stringIDToTypeID("screenModeFullScreen");

stringIDToTypeID ("screenModeStandard");

stringIDToTypeID( "screenModeFullScreenWithMenubar" );