• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Pausing script until user interaction

Explorer ,
Nov 13, 2018 Nov 13, 2018

Copy link to clipboard

Copied

Hey everyone!

I wrote a script that is a part of bigger script and my export action

It checks if job is for one of my two clients (based on text frames content), then if layer named "farmakod" exists, then if "farmakod" is empty.

var docRef = app.activeDocument;

function doesLayerExist(layers, name) {

    for (i = 0; i < layers.length; i++) {

        if (layers.name == name) return true;

    }

    return false;

};

function checkPharmaCode() {

    if (doesLayerExist(docRef.layers, "farmakod")) {

        var targetLayer = app.activeDocument.layers.getByName("farmakod");

        if (targetLayer.pageItems.length == 0) {

            alert("Missing pharmacode on 'farmakod' layer!");

        } else {};

    } else {

        alert("Missing layer 'farmakod'!")

    }

};

function checkPharmacy() {

    var frames = docRef.textFrames;

    for (var x = frames.length - 1; x >= 0; x--) {

        if ((frames.contents.indexOf("CLIENT1") > -1) || (frames.contents.indexOf("CLIENT2") > -1)) {

            checkPharmaCode()

        } else {}

    }

};

checkPharmacy();

Right now i only have an alert information, but the script (and action) are stil going after closing an alert.

And since the script is exporting my file as pdf, i dont want to wait until the end of the proccess, just to fix everything, and start script once again.

I was doing some searching and as far as i know there is no simple way to stop a script to let user make some changes (in my ex. creating a layer and inserting a content).

Palette window is not an answer - i can do my changes, but script is still working

Dialog window - stops a script, but doesn't allow to click anything besides window itself.

Is there any workaround for this issue?

Like setting $.sleep for 10min, and after clicking OK on palette window the $.sleep would be interrupted or changed to 0?

Or something like clearTimeout ?

Also some code review would be appreciated

Sorry for my english.

TOPICS
Scripting

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
People's Champ ,
Nov 15, 2018 Nov 15, 2018

Copy link to clipboard

Copied

Then just condition the script execution to layer's presence.

var main = function() {

var doc, layers, farmakodLayer;

if ( !app.documents.length ) {

alert("This scripts needs an open document !");

return;

}

doc = app.activeDocument;

layers = getLayers(doc);

if ( typeof layers["farmakod"]=="undefined" ) {

alert("You need farmakod layer to get this script running");

return;

}

alert("Now you can execute sub routines");

}

function getLayers(kDoc){

var kLayers = kDoc.layers, n = kLayers.length, o = {}, nKLayer;

while ( n-- ) {

nKLayer = kLayers;

o[nKLayer.name] = nKLayer;

}

return o;

}

main();

HTH

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

Thank you very much for your answer but it's not really the solution for my problem

As I said the script i posted is a part of a bigger script that is a part of an action that i use to export files as Normalized PDF via Esko plugin.

The action looks like this:

action.jpg

After canceling the script an action will go on, and i would want to fix the problem before exporting a file.

Like:

if there is no "farmakod" layer or it's empty, the script is paused and the user can create the layer and put necessary objects, and then let the action continue

unfortunately i have no idea how to script the export part (which would be a simple solution) since it's a third party plugin (in fact, I found a tutorial on scripting export process among ESKO files, but I did not understand much of it, and I do not know if I can share it publicly with someone who would help me with this issue)

maybe the solution would be starting an action (only Export as... part) from script and condition execution as you suggested

Like:

script {

     if everything is ok

           continue process normally:

               do script stuff

               execute action "Export as.." - via Esko plugin

               do other script stuff

     else

          abort process

}

is this possible?

also i have no idea how your script works, especially getLayer function

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 18, 2018 Nov 18, 2018

Copy link to clipboard

Copied

Is there a way you can invert your flow to make the script play your actions instead?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

I could be wrong but I think there is no "pause" concept in ExtendScript. Of course, one can argue there is a $.sleep command but it just freezes the script engine execution and the app in a modal state.

So you probably need to check eveything before you run the whole routine just as you indicated.

My getLayer function intends to avoid execution errors when using doc.layers.getByName ("someLayerName"). The latter throws error if no such layer exists. So that's a way I found to possibly deal with non existing layers by creating a javascript object whose properties are layers's names. Eventually, I can "reach" a non existing layer without errors.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 19, 2018 Nov 19, 2018

Copy link to clipboard

Copied

Loic.Aigon​ thank you for your answers and improved script

unfortunately not what i wanted, but exactly what I expected

Silly-Vit seems to me that it is very likely and to be done

just like i wrote earlier

script {

     if everything is ok

           continue process normally:

               do script stuff

               execute action "Export as.." - via Esko plugin

               do other script stuff

     else

          abort process/do nothing

}

I have seen your post about dynamic action scripting, is this a right direction to do what we are talking about ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 20, 2018 Nov 20, 2018

Copy link to clipboard

Copied

Ok i found a way to play action via script (much simpler than i thought),

I'm currently rewriting my script to work just as you suggested,but i have encountered another problem (error 1200), which I will no longer discuss in this post, not to make a mess

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 20, 2018 Nov 20, 2018

Copy link to clipboard

Copied

Salut, autre solution...

// JavaScript Document
/*getLayerbyName.js*/
if (app.documents.length){main();}
function main() {
doc = app.activeDocument;
var testLayer = getLayers(doc,"farmakod");
if (testLayer == undefined) {
      alert("You need farmakod layer to get this script running");
      return;
      }

//run  action
alert("Passed ")
}

function getLayers(relativObjet,layerName)
{
    try {
    var klayer = relativObjet.layers.getByName(layerName);
    }  catch (e) {
        //alert( "The specified layer doesn’t exist" );
        return undefined; // undefined if not exist
        }
    return klayer;
}

de elleere

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 20, 2018 Nov 20, 2018

Copy link to clipboard

Copied

Oui mais l'intérêt de l'objet c'est que tu peux le charger au début du script et y revenir au besoin sans rappeler la fonction

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 22, 2018 Nov 22, 2018

Copy link to clipboard

Copied

I think is it possible to do in dynamic action

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 07, 2023 Jul 07, 2023

Copy link to clipboard

Copied

Hi, bro (  or sis (:      )
5 frigging years later, but it's one of the first results on Google so it might help some people.
You can open a "file select" 

var result = File.openDialog ('Select File...')
which would open a file explorer (finder for you apple weirdos😄 ). you can return data to the application by selecting a file (it would end up in the result var as a File object). this would stop the script and wait for action
(it might be worth checking the docs for similar interactions)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 09, 2023 Jul 09, 2023

Copy link to clipboard

Copied

LATEST

If those layers are missing, wouldn't you just want to exit the larger script so it can be fixed, then execute the script again? Could you integrate what you have at the beginning of the script to exit out? 

Edit: For some reason, replies hadn't shown up when I replied to this. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines