Skip to main content
Participant
December 30, 2010
Question

Keeping variables across runs of a script

  • December 30, 2010
  • 4 replies
  • 8029 views

Well, I'm a noob at Photoshop scripting. I knew Javascript, and I've been trying to work my way with the Photoshop DOM for about a month, but now I'm trying to do something that I can't find anywhere, neither in Google nor in the documentation... Sorry if this sounds obvious.

First I'm asking to open a PDF file, and then I ask to select a folder where the pages will go. Then I save in an array the file object for the PDF, the folder object, the current page... so I can get it later. Now I open the first page, and stop the script to do some manual work. After that, I would load the script, check if the array where I saved that exists, and if it does, save that page and open the next.

Now, I can't find any way to keep variables between runs of a script, nor to keep the script waiting in the background. Somewhere in the Javascript reference it says that the variables are kept, but every time it asks me to select the PDF again. So I'm kind of lost there. Could you help me?

By the way, I'm using Photoshop CS5, and I script with Javascript.

This topic has been closed for replies.

4 replies

NightSkyGuy
Inspiring
November 22, 2017

picknos I found this: extendscript - Saving per-user or per-document preferences in a Photoshop script

on StackExchange.  I realize it's a very old question, but I figure below together with the answer given by c.pfaffenbichler​ would help subsequent readers.

Overflowconst kMyFlag = app.stringIDToTypeID( "myFlag" );
const kMyNumber = app.stringIDToTypeID( "myNumber" );
const kMySettings = "mySettings";

function saveSettings()
{
 
var desc = new ActionDescriptor();
  desc
.putBoolean(kMyFlag, true);
  desc
.putInteger(kMyNumber, 42);

 
// "true" means setting persists across Photoshop launches.
  app
.putCustomOptions( kMySettings, desc, true );
}

function getSettings()
{
 
var desc = app.getCustomOptions( kMySettings );
 
return [desc.getBoolean( kMyFlag ), desc.getInteger( kMyNumber )];
}

Note further in the same answer set is an example of saving options "per document" using app.activedocument.info.instructions

picknosAuthor
Participant
December 30, 2010

Oh, I see. Thanks to all of you.

JJMack
Community Expert
Community Expert
December 31, 2010

I'm late to this thread.  I couple of years ago I started to do some Scrip hacking.  I don't  know much about scripting only hack I use downloaded script as a starting point.  I wanted to Pass Pram to Scripts and also remember things between  two executions of a script.

 

When CS3 was released I discovered that Adobe added Plug-in support to scripting for Fit Image was converted to a script in CS3.  That ment tha I now hat a way to pass Parms to a Script.  I coyld write it as a Plugin and the record using it in an action.  Thar Parms I wanted to pass would be recorded by the Action and when the action was played the parme would be passed to the script and Its dialog would not be displayed.

 

For me Actions are easy where Scripts are work.  I wanted to create Scripts that could be used as utilities for actions put in a little logic. I wanted to create scripts that could be used twice in an action once in the the beginning of an action to save some of the documents current setting the action would then be free to make changes to to these setting for when the script would be run the second time at the end of the action the script would retrieve the saved setting and restore document  to its original settings.   I did not want  to write files yet I wanted to keep the information with the document.and be able to write and use more then on script if needed perhaps process more then one document at a time. I did not want to write one supper save everything and restore everything script.   I was able to actually hack these.    I used a field in the documents meta-data.  These scripts first look for its footprint in the current document metadata. If its not there its the first pass and the  information is placed into the meta data. The second time the script is run the information is retrieved and removed from the meta-data  and the document originale settings are restored.  Done properly the meta-data never gets written.  The Action can not contain any stops or saves btween the to executions of the scripts.

 

Examples cam be found in my crafting actions package

 

 

JJMacksCraftingActions.zip

Contains:

Action Actions Palette Tips.txt

Action Creation Guidelines.txt

Action Dealing with Image Size.txt

Action Enhanced via Scripted Photoshop Functions.txt

CraftedActions.atn Sample Action set includes an example Watermarking action WM900x600.jpg

Sample Actions.txt Photoshop CraftedActions set saved as a text file. This file has some additional comments I inserted describing how the actions work.

 

12 Scripts for actions

 

 

 

JJMack
Inspiring
December 30, 2010

If you  want to use the DOM to save the variables between runs instead of a file as C.Pfaffenbichler suggested, you need to use app.setCustomOptions() and app.getCustomOptions(). CustomOptions is covered in the Javascript guide under the application object. Some of the Adobe scripts that ship Photoshop use CustomOptions if you need to see a working example.

Pedro Cortez Marques
Legend
March 26, 2018

HI Michael L Hale

The app.putCustomOptions() and app.getCustomOptions() works really well.

When it is persistent, does is it stored in the photoshop "registry"?

And is this secure enought and fast to put and get?

Do you think it is safe to move all variables from external objects, like JSON, xml, etc to this other method app.putCustomOptions()?

I like it a lot but I want to know if it is something that you are using for a long time and with success.

Finally, last question.

If I want to save some variables inside a document (like a psd) when I save it, I am using XMP metadata on the image or XMP inside a specific layer metadata.

Would you do the same if you needed to have those vars in a psd image?

JJMack
Community Expert
Community Expert
March 26, 2018

Michael is no longer with us. IMO keeping data with a document in metadata is a good way just keep in mind document are often saved or duplicated with new names for version. Some saved image files may have metadata striped.  PSD files should not see metadata striped. If you use metadata you may want to save the document when you change the data to force the data to disk and also include the current backing file path in the metadata to be able to tell version from the original document.  Keeping data in the document I think would be better then an associated external files for copying and renaming documents may loose the associated with the external file.

JJMack
c.pfaffenbichler
Community Expert
Community Expert
December 30, 2010

First off converting pdfs in Photoshop may be inadvisable depending on the kind of editing you have to do.

Other than that you could always write a txt-file to store the information to some pre-determined location (kind of a preference-file so to speak) and have the Script check for it before the dialog is raised.

If such a file is found you can have the Script evaluate it and when the last page of the pdf is reached remove it.

Hope this is halfway clear …

But wouldn’t it be easier to just run the Script across the whole of the pdf, save the individual documents but not close them, so that you can just edit and close them one by one? (Depending of the number of pages this could admittedly be risky or get very slow.)

picknosAuthor
Participant
December 30, 2010

Well, thanks to both, I'll try to see what I can do ^^

But well, this kind of suggested that variables are kept:

"Scripting shares a global environment, so any script executed at startup can define variables and functions that are available to all scripts. In all cases, variables and functions, once defined by running a script that contains them, persist in subsequent scripts during a given application session. Once the application is quit, all such globally defined variables and functions are cleared. Scripters should be careful about giving variables in scripts unique names, so that a script does not inadvertently reassign global variables intended to persist throughout a session."

(Javascript Tools Guide CS5, page 12)

That's why I kept trying to keep the array instead of going for something else...

Message was edited by: picknos

Inspiring
December 30, 2010

Although you can use globally defined variables, they need to be in a startup script to stay in memory. Normal scripts( ones run using File-Scripts ) can set global variables but the are removed when the script stops.

However globally defined variables are for the most part a bad practice. They take up limited resources and it's easy to overwrite variables thus breaking some other script( or have some other script change the values in your global variables ).

The cross-DOM Adobe scripts are good examples of scripts that need global variables and methods. Even they limit globals to the minimum required and use local variables where they can.