Copy link to clipboard
Copied
Please excuse my inexperience with writing scripts. I am mostly a copy - cut - paste scripter.
I have an extension panel for my users in Photoshop that has buttons for menu items and tools. I also have two buttons that run scripts that were complied from Actions scripts. What I have learned is that these buttons on the extension panel do not work if the two action scripts are not loaded into the Action panel.
What I would like to have happen is for the scripts to run without having to have them loaded into the Actions panel. Below is what I have in my hotscript.jsx file for one of the Actions. How would I code this so it does not require the scripts to be loaded into the Actions panel?
function selShade()
{var idPly = charIDToTypeID( "Ply " );
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var idActn = charIDToTypeID( "Actn" );
ref2.putName( idActn, "Shade" );
var idASet = charIDToTypeID( "ASet" );
ref2.putName( idASet, "Shade" );
desc3.putReference( idnull, ref2 );
executeAction( idPly, desc3, DialogModes.NO );
}
The Second script is this:
function selCreatepdf()
{var idPly = charIDToTypeID( "Ply " );
var desc14 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref6 = new ActionReference();
var idActn = charIDToTypeID( "Actn" );
ref6.putName( idActn, "Before After Presentation" );
var idASet = charIDToTypeID( "ASet" );
ref6.putName( idASet, "Before After PDF" );
desc14.putReference( idnull, ref6 );
executeAction( idPly, desc14, DialogModes.NO );
}
I hope this is possible because it would eliminate my users having to load Actions into the Actions panel.
Thanks in Advance for your help - Sam
Hi Sam,
I strongly second Chuck's advice. Your ScriptingListener code should be equivalent to:
function selShade() { app.doAction("Shade", "Shade") }
function selCreatepdf() { app.doAction("Before After Presentation", "Before After PDF") }
that is, you're calling Actions loaded into ActionSets - if they're not loaded, your calls will fail.
I strongly second Chuck's advice. If you made the actions, or in any case if you have the actions yourself, you might automatically convert them into javascript
...Copy link to clipboard
Copied
The nearst I've seen to do this is found here.
http://www.tonton-pixel.com/blog/scripts/utility-scripts/play-actions-file-action/
Copy link to clipboard
Copied
Thanks Philip - but I don't think that is going to work for me. It requires more steps than I want my user to go through.
Copy link to clipboard
Copied
Maybe Mikaeru might be able to show you how to script it without the UI ?
Copy link to clipboard
Copied
One of the issues with playing an action through a script is that you have to also have the actions properly loaded. Most of the time, it's better just to use scriptListener to record all you want to do with the action and just create a script. The reason what you have above is not working is that they are both functions, and there is nothing calling those functions. You would need something like:
#target photoshop
selShade();
And then the rest of your code to work. You save that as a regular text file with the .jsx extension. Or you can omit the first line declaring the function and the last brace.
Copy link to clipboard
Copied
Hi Sam,
I strongly second Chuck's advice. Your ScriptingListener code should be equivalent to:
function selShade() { app.doAction("Shade", "Shade") }
function selCreatepdf() { app.doAction("Before After Presentation", "Before After PDF") }
that is, you're calling Actions loaded into ActionSets - if they're not loaded, your calls will fail.
I strongly second Chuck's advice. If you made the actions, or in any case if you have the actions yourself, you might automatically convert them into javascript using xbytor's script ActionToJavascript.jsx (find xtools download here on SourceForge). Then include the code and just call those functions, no need to have users install actions anymore (and by the way your code will be a little bit more sealed).
Hope this helps,
Davide Barranca
---
www.davidebarranca.com
www.cs-extensions.com
Copy link to clipboard
Copied
Thank you Chuck and Davide - I will take you advice and rescript using the suggestions. I will post back here with my results and hopefully success.
Thanks again - Sam
Copy link to clipboard
Copied
If you did want to run an action without loading the actionset and no user input, Michel's page explains how it is done :-
http://www.tonton-pixel.com/JSON%20Action%20Manager/jsDoc/symbols/jamActions.html
Here is an example using his code.
#target photoshop;
app.bringToFront();
/*************************************************
Play a specific action contained in an actions file (.atn), without the need to load the file in the Actions Palette.
http://www.tonton-pixel.com/JSON%20Action%20Manager/jsDoc/symbols/jamActions.html
Utility script using the "JSON Action Manager" scripting library.
© 2011-2015 Michel MARIANI.
******************************************************/
//------------------------------------------------------------------------------
// jamActions.jsxinc v4.4 (minified)
if(typeof jamActions!=='object') {var jamActions={};(function() {jamActions.isActionsFile=function(file) {return(file.type==='8BAC')||file.name.match(/\.atn$/i);};jamActions.isActionsPalette=function(file) {return((file.type==='8BPF')&&file.name.match(/^Actions Palette$/i))||file.name.match(/^Actions Palette.psp$/i);};function readBEInt(file,byteCount) {var bytes=file.read(byteCount);var intValue=0;for(var index=0;index<byteCount;index++) {intValue=(intValue<<8)+bytes.charCodeAt(index);} return intValue;} function readBytes(file,byteCount) {return file.read(byteCount);} function readByteString(file) {var stringLength=readBEInt(file,4);return readBytes(file,stringLength);} function readUnicodeString(file) {var unicodeString="";var unicodeLength=readBEInt(file,4);for(var index=0;index<unicodeLength;index++) {var unicodeChar=readBEInt(file,2);if(unicodeChar!==0) {unicodeString+=String.fromCharCode(unicodeChar);}} return unicodeString;} function readEventId(file) {var eventId=0;var eventType=readBytes(file,4);switch(eventType) {case'TEXT':eventId=app.stringIDToTypeID(readByteString(file));break;case'long':eventId=app.charIDToTypeID(readBytes(file,4));break;default:throw new Error("[jamActions readEventId] Unrecognized event type: '"+eventType+"'");break;} return eventId;} function skipDouble(file) {file.seek(8,1);} function skipDoubles(file,doubleCount) {file.seek(doubleCount*8,1);} function skipInt8(file) {file.seek(1,1);} function skipInt16(file) {file.seek(2,1);} function skipInt32(file) {file.seek(4,1);} function skipInt64(file) {file.seek(8,1);} function skipBytes(file,byteCount) {file.seek(byteCount,1);} function skipByteString(file) {var stringLength=readBEInt(file,4);skipBytes(file,stringLength);} function skipUnicodeString(file) {var unicodeLength=readBEInt(file,4);skipBytes(file,unicodeLength*2);} function skipId(file) {var idLength=readBEInt(file,4);if(idLength) {skipBytes(file,idLength);} else {skipBytes(file,4);}} function skipClass(file) {skipUnicodeString(file);skipId(file);} function skipObject(file) {skipClass(file);var itemCount=readBEInt(file,4);for(var itemIndex=0;itemIndex<itemCount;itemIndex++) {skipId(file);skipItem(file);}} function skipList(file) {var itemCount=readBEInt(file,4);for(var itemIndex=0;itemIndex<itemCount;itemIndex++) {skipItem(file);}} function skipItem(file) {var typeId=readBytes(file,4);switch(typeId) {case'obj ':skipReference(file);break;case'Objc':case'GlbO':skipObject(file);break;case'type':case'GlbC':skipClass(file);break;case'VlLs':skipList(file);break;case'doub':skipDouble(file);break;case'UntF':skipBytes(file,4);skipDouble(file);break;case'TEXT':skipUnicodeString(file);break;case'enum':skipId(file);skipId(file);break;case'long':skipInt32(file);break;case'comp':skipInt64(file);break;case'bool':skipInt8(file);break;case'alis':skipByteString(file);break;case'Pth ':skipByteString(file);break;case'tdta':skipByteString(file);break;case'ObAr':var objCount=readBEInt(file,4);skipClass(file);var itemCount=readBEInt(file,4);for(var itemIndex=0;itemIndex<itemCount;itemIndex++) {skipId(file);skipInt32(file);skipInt32(file);var doublesCount=readBEInt(file,4);skipDoubles(file,doublesCount);} break;default:throw new Error("[jamActions skipItem] Unrecognized item type: '"+typeId+"'");break;}} function skipReference(file) {var itemCount=readBEInt(file,4);for(var itemIndex=0;itemIndex<itemCount;itemIndex++) {var formId=readBytes(file,4);skipClass(file);switch(formId) {case'Clss':break;case'prop':skipId(file);break;case'Enmr':skipId(file);skipId(file);break;case'rele':skipInt32(file);break;case'Idnt':skipInt32(file);break;case'indx':skipInt32(file);break;case'name':skipUnicodeString(file);break;default:throw new Error("[jamActions skipReference] Unrecognized item form: '"+formId+"'");break;}}} jamActions.readActionDescriptor=function(file,insertVersionPrefix) {var versionPrefix="\x00\x00\x00\x10";var start=file.tell();if(!insertVersionPrefix) {if(file.read(4)===versionPrefix) {versionPrefix="";} else {throw new Error('[jamActions.readActionDescriptor] Unrecognized version prefix');}} skipObject(file);var end=file.tell();file.seek(start,0);var stream=versionPrefix+file.read(end-start);var actionDescriptor=new ActionDescriptor();actionDescriptor.fromStream(stream);return actionDescriptor;};jamActions.dataFromActionsFile=function(actionsFile,isPalette) {var that=this;function parseActionSet(file) {var actionSet={};actionSet.name=localize(readUnicodeString(file));actionSet.expanded=(readBEInt(file,1)!==0);var actionCount=readBEInt(file,4);actionSet.actions=[];for(var actionIndex=0;actionIndex<actionCount;actionIndex++) {var action={};action.functionKey=readBEInt(file,2);action.shiftKey=(readBEInt(file,1)!==0);action.commandKey=(readBEInt(file,1)!==0);action.colorIndex=readBEInt(file,2);action.name=localize(readUnicodeString(file));action.expanded=(readBEInt(file,1)!==0);var commandCount=readBEInt(file,4);action.commands=[];for(var commandIndex=0;commandIndex<commandCount;commandIndex++) {var command={};command.expanded=(readBEInt(file,1)!==0);command.enabled=(readBEInt(file,1)!==0);command.withDialog=(readBEInt(file,1)!==0);command.dialogOptions=readBEInt(file,1);command.eventId=readEventId(file);command.dictionaryName=readByteString(file);if(readBEInt(file,4)!==0) {command.actionDescriptor=that.readActionDescriptor(file,true);} action.commands.push(command);} actionSet.actions.push(action);} return actionSet;} var file;if(typeof actionsFile==='string') {file=new File(actionsFile);} else if(actionsFile instanceof File) {file=actionsFile;} else {throw new Error('[jamActions.dataFromActionsFile] Invalid argument');} var fileData;if(file.open("r")) {try {file.encoding='BINARY';var fileVersion=readBEInt(file,4);if(fileVersion===16) {fileData={};fileData.version=fileVersion;if(isPalette) {fileData.actionSets=[];var actionSetCount=readBEInt(file,4);for(var actionSetIndex=0;actionSetIndex<actionSetCount;actionSetIndex++) {fileData.actionSets.push(parseActionSet(file));}} else {fileData.actionSet=parseActionSet(file);}} else {fileData="Unsupported actions file version: "+fileVersion;}} catch(e) {fileData=e.message;} finally {file.close();}} else {fileData="Cannot open file";} return fileData;};jamActions.isLocalPlayCommand=function(command,actionSetName) {var localPlayCommand=null;if(command.eventId===app.stringIDToTypeID("play")) {var targetId=app.stringIDToTypeID("target");if(command.actionDescriptor.hasKey(targetId)) {var localReference=command.actionDescriptor.getReference(targetId);do {try{var desiredClassId=localReference.getDesiredClass();}catch(e){break;} switch(desiredClassId) {case app.stringIDToTypeID("command"):var localCommandIndex=localReference.getIndex()-1;break;case app.stringIDToTypeID("action"):var localActionName=localReference.getName();break;case app.stringIDToTypeID("actionSet"):var localActionSetName=localReference.getName();break;} localReference=localReference.getContainer();} while(localReference);} var continueId=app.stringIDToTypeID("continue");if(command.actionDescriptor.hasKey(continueId)) {var localContinue=command.actionDescriptor.getBoolean(continueId);} if((typeof localActionSetName!=='undefined')&&(localActionSetName===actionSetName)) {localPlayCommand=[localActionName,localCommandIndex,localContinue];}} return localPlayCommand;};jamActions.determineDialogMode=function(command) {var dialogMode;switch(command.dialogOptions) {case 0:dialogMode=command.withDialog?DialogModes.ALL:DialogModes.NO;break;case 2:dialogMode=DialogModes.NO;break;case 1:case 3:dialogMode=DialogModes.ALL;break;} return dialogMode;} var globalCommandHandler=null;jamActions.setCommandHandler=function(commandHandler) {globalCommandHandler=commandHandler;};jamActions.traverseAction=function(actionSet,actionLocator,fromCommandIndex,continuePlay) {function handleCommands(commands) {var commandMax=(continuePlay)?commands.length:fromCommandIndex+1;for(var commandIndex=fromCommandIndex;commandIndex<commandMax;commandIndex++) {if(globalCommandHandler!==null) {globalCommandHandler(commands[commandIndex]);}}} if(typeof fromCommandIndex==='undefined') {fromCommandIndex=0;continuePlay=true;} var actions=actionSet.actions;if(typeof actionLocator==='string') {var actionName=actionLocator;for(var actionIndex=0;actionIndex<actions.length;actionIndex++) {var action=actions[actionIndex];if(action.name===actionName) {handleCommands(action.commands);break;}}} else if(typeof actionLocator==='number') {var actionIndex=actionLocator;if((actionIndex>=0)&&(actionIndex<actions.length)) {handleCommands(actions[actionIndex].commands);}}};}());}
//------------------------------------------------------------------------------
if(documents.length){
Folder.current = new Folder ("~/Desktop/"); //Location of actionset folder.
var actionsFilePath = "Philip.atn"; //Name of atn file.
var ActionName = "Auto"; //Name of the action within the action set.
var fileData = jamActions.dataFromActionsFile (actionsFilePath);
if (typeof fileData === 'string')
{
alert (fileData + "\n" + "Actions file: “" + actionsFilePath + "”");
}
else
{
function executeCommand (command,ActionName)
{
if (command.enabled)
{
var dialogMode = jamActions.determineDialogMode (command);
app.executeAction (command.eventId, command.actionDescriptor, dialogMode);
}
}
jamActions.setCommandHandler (executeCommand);
jamActions.traverseAction (fileData.actionSet, ActionName);
};
};
Copy link to clipboard
Copied
Thanks to All who answered my query.
Davide - ActionToJavascript.jsx is the answer. It took a little figuring, but I got my panel to run the action scripts without the Actions. This will make things a lot simpler for my users.
What a great resource forums are. I would not have had a clue as to how to make this work.
Thanks again - Sam