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

[scripting]

New Here ,
Aug 04, 2022 Aug 04, 2022
PS version: Photoshpp 2022 160.064
 
app.activeDocument is not updating after editing smart object content. Strangly it does work if I add an alert but not when using app.refresh or sleep. It seems as if its a matter of a delay between the UI and the script.
In general setting app.activeDocument does not seem to work as expected. Anyone experience the same?
 
 
TOPICS
Actions and scripting , SDK
1.6K
Translate
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

correct answers 1 Correct answer

Mentor , Aug 11, 2022 Aug 11, 2022

It looks like the object from the library is being opened asynchronously and we can't keep track of it in the normal way.

 

Alternatively, we can abort the script, wait for the library element to open, and then continue (i'm using alert for notification since $.writeln doesn't work fine when fired from an event):

var s2t = stringIDToTypeID,
    evt;

// check run mode: normal or from event
try { evt = arguments[1]; } catch (e) { }

if (!evt) {
    // if no event
    (r = new ActionReference()).p
...
Translate
Adobe
Community Expert ,
Aug 04, 2022 Aug 04, 2022
Translate
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
Mentor ,
Aug 04, 2022 Aug 04, 2022

Hi @Gili5E4B! For some tasks, it's better to use the Action Manager code (not DOM).

However, without code, the essence of your problem is not fully understood.

Translate
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 ,
Aug 04, 2022 Aug 04, 2022

Could you provide a Script to demonstrate? 

Translate
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
Contributor ,
Aug 04, 2022 Aug 04, 2022

You can cheat a bit using a try-catch-finally block, or by using $.sleep(500); after editing the document.

 

try{
   //editing the smart object
} catch(e){
   $.writeln("error: "+e);
} finally{
   //continue your code
}

The try-catch forces the code inside the try part to be finished completely before starting on the finally part.

 

Alternatively... $.sleep(milliseconds) suspends the calling thread for the given number of milliseconds allowing Photoshop time to update the app.activeDocument object.

 

Hope it helps 😉

Translate
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 ,
Aug 05, 2022 Aug 05, 2022

A script example:

 

if (curLayer.kind == 'LayerKind.SMARTOBJECT') {
            app.activeDocument.activeLayer = curLayer;
            app.displayDialogs = DialogModes.NO;
            $.writeln("Identified a smart object at " + app.activeDocument.activeLayer.name);
            try {
                app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
            } catch(e) {
                $.writeln("error: "+e);
            } finally {
                $.writeln("Stepped into smartobject " + curLayer.name);
                $.writeln("app.documents 1: " + app.documents.length);
                $.sleep(2000);
                $.writeln("app.documents 2: " + app.documents.length);
            }

 

Logging Result:

Identified a smart object at Group 1
Stepped into smartobject Group 1
app.documents 1: 1
app.documents 2: 1
 
No matter using try catch or the use of $.sleep result is the same.
In Photoshop I do see the smartobject PSD is opened. If I add an alert instead of sleep result is 2 as expected.
@jazz-y , @c.pfaffenbichler , @jefbr 

Translate
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 ,
Aug 05, 2022 Aug 05, 2022

The problem appers to be connected to the smart object being a linked library object.
When I try the same on regular linked smart object the issue is not happening. Is there anyway to use callback or event that the file is opened/loaded?

Translate
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 ,
Aug 06, 2022 Aug 06, 2022

I see you posted since and with a Library object it is different here, too. 

But it seems that the opening occurs after the lines have been written (see gif). 

openLibraryObjectViaScript.gif

Translate
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 ,
Aug 07, 2022 Aug 07, 2022

I added the creation of a new document after 

app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));

and even that was created before the SO opened. 

 

I seems like the Script is progressing just fine before the library actually »sends« the image to Photoshop. 

Translate
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 ,
Aug 06, 2022 Aug 06, 2022

I cannot reproduce the issue with a plain SO. 

Screenshot 2022-08-06 at 12.11.55.png

Translate
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
Mentor ,
Aug 11, 2022 Aug 11, 2022

It looks like the object from the library is being opened asynchronously and we can't keep track of it in the normal way.

 

Alternatively, we can abort the script, wait for the library element to open, and then continue (i'm using alert for notification since $.writeln doesn't work fine when fired from an event):

var s2t = stringIDToTypeID,
    evt;

// check run mode: normal or from event
try { evt = arguments[1]; } catch (e) { }

if (!evt) {
    // if no event
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('smartObject'));
    r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
    // check SO Type 
    if (executeActionGet(r).getObjectValue(p).hasKey(s2t('link')) && executeActionGet(r).getObjectValue(p).getType(s2t('link')) == DescValueType.OBJECTTYPE) {
        // if library item, then add eventListener and send open command
        app.notifiersEnabled = true
        clearListener()
        app.notifiers.add('Opn ', File($.fileName));
        executeAction(s2t('placedLayerEditContents'), undefined, DialogModes.NO);
    } else {
        // if not library item, then open it
        executeAction(s2t('placedLayerEditContents'), undefined, DialogModes.NO);
        processSO()
    }
} else {
    // if from event
    clearListener()
    processSO()
}

function clearListener() {
    var f = File($.fileName),
        del;
    for (var i = 0; i < app.notifiers.length; i++) {
        var ntf = app.notifiers[i]
        if (ntf.eventFile.name == f.name) { ntf.remove(); i--; del = true }
    }
}

// process opened smart
function processSO() {
    alert(documents.length)
}

 

Translate
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 ,
Aug 11, 2022 Aug 11, 2022

The direction is great using notifiers, but the script isn't working.

executeActionGet(r).getObjectValue(p)

Is returning "General Photoshop error occurred. This functionality may not be available in this version of Photoshop.
- &lt;no additional information available&gt;" 

Is there another way to assert if a smartobject is from a cloud library?

Translate
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
Mentor ,
Aug 11, 2022 Aug 11, 2022

Did you select the smart object layer before running the script?

 

If so, send output of this script:

#target photoshop
s2t = stringIDToTypeID;
(r = new ActionReference());
r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
(d = new ActionDescriptor()).putObject(s2t('object'), s2t('object'), executeActionGet(r));
$.writeln(executeAction(s2t('convertJSONdescriptor'), d).getString(s2t('json')));

 

Translate
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 ,
Aug 11, 2022 Aug 11, 2022

Sorry I didn't select the SmartObject. After selecting it works.

I will try to incorporate into my own script. Thanks for the help!

Translate
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 ,
Aug 11, 2022 Aug 11, 2022

to jazz-y

После placedLayerEditContents подождать в бесконечном цикле (с учётом таймаута конечно) пока documents.length не изменится.

Не ?  : )

Translate
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
Mentor ,
Aug 11, 2022 Aug 11, 2022

Это было первое, что я попробовал 🙂 Не прокатило. Такое ощущение, что движок фотошопа после запроса на открытие неспеша грузит файл из библиотеки в фоне, затем ждет когда закончится весь скрипт и только после этого открывает его. Уведомление об  'Opn ' после завершения скрипта срабатывает всегда, даже если раздуть скрипт левыми функциями или затормозить его циклом, а в библиотеку запихать файл 1х1 px 

¯\_(ツ)_/¯

Translate
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 ,
Aug 12, 2022 Aug 12, 2022
LATEST

Ясно. Такая же фигня как с генератором, когда пытаешься сделать новомодный экспорт слоя\файла. Фактический экспорт выполнится только по завершению всего скрипта инициировавшего команду.

Translate
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