Skip to main content
Inspiring
December 20, 2011
Answered

Random Curiousity; Self re-writing script?

  • December 20, 2011
  • 1 reply
  • 2316 views

This is something I've been wondering for a while, but is it possible to have a script re-write parts of itself the first time it runs?  For example, opening a dialog box on first run to locate a specific folder to run files from, then permanently writing that path to the script while simultaneously removing/disabling the part that pops up the dialog box?  This would be a very handy piece of script when sending scripts to others that use differing file/folder structure for storing their files than you use, and would prevent said person from having to manually edit the script (and possibly breaking the script in the process if they're unfamiliar with the syntax).  Anyway, just something I thought up that would be nice to reduce the trouble-shooting headaches I get when having others manually edit the script.  Any ideas?

dgolberg

This topic has been closed for replies.
Correct answer Paul Riggott

Well, as far as the topic of this thread; I think what I'll do is setup my scripts (the ones that require a static image file(s) to run with it) so that they look for a .csv file in the same folder as the script itself.  If one is not found, it will bring up a dialog box asking the user to locate the required image file(s), then create the .csv file (skipping this step if one is found).

As far as this other script, my boss wants me to do something similar in which they can edit the .csv file with an image file's name (or in this case, several image files) which are then run through our normal process (a process that is currently run through batch automation that uses both actions and a script).  I'll likely end up automating it through the script rather than Photoshop's batch automation window, but I'm stuck on one part... how to run the actions that change each project.  These customized actions record a transformation of the textures that are normally batched.  This action is then run on each texture to match the transformations to the project.  I should be able to automate everything through script except for this transform action, since it changes for every project.  In short, is there any way to have a script run a specific action?

Additionally; I've learned how to have a script look for a file in a specific location; but how about looking for a specific file in a general location (ie: having the script search all sub-folders until it finds a filename match).  I could probably find this through some searching, but if anyone already knows where to find this; could ya point me in the right direction?

The rest of the script I should have figured out already, it's just a matter of writing it/combining it with existing scripts now.

Funny how I never wanted to do any form of programming when I got out of college; now that's almost all I do... and enjoy it!


This might help...

function main(){
var Path = File($.fileName).parent;
var csvFile = File(Path + "/actions.csv"); //same folder as the script
if(!csvFile.exists){
    alert("Unable to find CSV file!");
    return;
    }
var sourceFolder = Folder.selectDialog("Please select Folder where files are to be found");
if(sourceFolder == null) return;
csvFile.open('r');
var data=[];
csvFile.readln(); //do not process the header line
while(!csvFile.eof){
   var line = csvFile.readln().replace(/^\s+|\s+$/g, ''); //remove leading and trailing spaces
   if(line.length>3) data.push(line); //Make sure it's not a blank line
}
csvFile.close();

for(var a in data){//process your files
var bits = data.split(',');
    var file = File(sourceFolder + "/"+bits[0]);
    //open (file);
    //runaction
    //app.doAction(bits[1].toString(), bits[2].toString());
    //do whatever
    //save and close you document
    alert(file +"\r will be running action - " + bits[1].toString() + " actionSet = " + bits[2].toString() );
    }
}
main();

CSV file format..

fileName,Action-Name,Action-Set

test.jpg,Action 1,Action set 1

test2.jpg,SuperAction,Super Actions

c.pfaffenbichler
Community Expert
Community Expert
December 20, 2011

I’m pretty certain that I at one point did assemble a Script that overwrote itself.

But I suspect this may not be the best practice and now prefer to store any information that I want to access in later runs of a Script in txt-files – this may also not be the best practice but it is workable.

In your case I guess you could have the Script check for the existence of the txt-file with the folder’s name, path, whatever and raise the dialog only if the txt-file or the folder itself are missing.

dgolbergAuthor
Inspiring
December 20, 2011

An interesting concept I hadn't considered.  Now that you mention it; I could probably get a lot of use from a .csv or .txt file reader (and I believe have to code to read such files).  Do you simply setup the script to look in the same folder the script itself is in for a file with a specific name/extension? 

Additionally, how did you get it to modify the script's code?  I work with a lot of scripts that use a specific image file(s) while also working in an automation; so having the dialog come up only once for the initial setup of the script to locate the static file(s) would be nice when passing the script on to others.

Paul Riggott
Inspiring
December 20, 2011

If you are using Photoshop CS5 you can use ScriptUI.environment.keyboardState.shiftKey and CustomOptions so that when the shift key is pressed you can have the UI to be shown, an example is here..

http://forums.adobe.com/message/3957442#3957442