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

SuspendHistory with active selection returns false

Engaged ,
Jan 18, 2022 Jan 18, 2022

Can some help figureout if the suspendHitory method can be used with an active docuemnt selection?

For example the code below returns false even when the document hss an active slection.

When the suspendHistory method is commented out and the main() function call enabled,

the hasSelcetion function return true with a selection and false without a selction.

 

.Screen Shot 2022-01-18 at 8.33.29 PM.png

 

 

function main() {

  try {

    if(hasSelection(app.activeDocument)) {
      alert(hasSelection(app.activeDocument))
    }
    else {
      alert(hasSelection(app.activeDocument))
    }
  
  } catch(e){
    alert(e + ' ' + e.line);
  }

}

//main();

app.activeDocument.suspendHistory("Test","main()")

function hasSelection(doc) {
  var res = false;
  var as = doc.activeHistoryState;
  doc.selection.deselect();
  if (as != doc.activeHistoryState) {
    res = true;
    doc.activeHistoryState = as;
  }
    return res;
}

 

 

TOPICS
Actions and scripting
1.0K
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

Community Expert , Jan 18, 2022 Jan 18, 2022

Another option: 

alert (hasSelection());
////// check for selection //////
function hasSelection(){
var ref10 = new ActionReference();
ref10.putProperty(stringIDToTypeID("property"), stringIDToTypeID("selection"));
ref10.putEnumerated( charIDToTypeID( "Dcmn" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
var docDesc = executeActionGet(ref10);
return docDesc.hasKey(stringIDToTypeID("selection"));
};
Translate
Adobe
LEGEND ,
Jan 18, 2022 Jan 18, 2022

 

$.level = 0; try{alert(activeDocument.selection.bounds)}catch(err){alert('!')}

 

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
Engaged ,
Jan 19, 2022 Jan 19, 2022

Thank you, this an interesting alternative.

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
LEGEND ,
Jan 19, 2022 Jan 19, 2022

Actually the alternative was from c.pfaffenbichler

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 ,
Jan 19, 2022 Jan 19, 2022
LATEST

The try/catch-approach you posted should work just as well. 

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 ,
Jan 18, 2022 Jan 18, 2022

Another option: 

alert (hasSelection());
////// check for selection //////
function hasSelection(){
var ref10 = new ActionReference();
ref10.putProperty(stringIDToTypeID("property"), stringIDToTypeID("selection"));
ref10.putEnumerated( charIDToTypeID( "Dcmn" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
var docDesc = executeActionGet(ref10);
return docDesc.hasKey(stringIDToTypeID("selection"));
};
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
Engaged ,
Jan 19, 2022 Jan 19, 2022

Thnak you! This function works with the suspendHistory method.

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
LEGEND ,
Jan 19, 2022 Jan 19, 2022

Can you post final code (with used function)? I'm sure with mine it should work as well.

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 ,
Jan 19, 2022 Jan 19, 2022

In simple terms, suspendHistory groups all commands into one history entry. That is, if you remove the selection inside the function that suspendHistory calls, then doc.selection.deselect () is not written as a separate history step and this piece of code simply does not work:

 

  if (as != doc.activeHistoryState) {
    res = true;
    doc.activeHistoryState = as;
  }

 

You can perform any number of operations, but the history entry will appear only after the entire function has been completed. If you just call main(), then the deselect() command creates a history entry and your code works.

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
Engaged ,
Jan 19, 2022 Jan 19, 2022

Thnks for the explenation. When I try ths approach I get the alert 

ReferenceError: as is undefined 6

How do we define as in this case?

 

 

function main() {

  try {

    if (as != doc.activeHistoryState) {
      res = true;
      doc.activeHistoryState = as;
    }

  
  } catch(e){
    alert(e + ' ' + e.line);
  }

}

main();
//app.activeDocument.suspendHistory("Test","main()")

function hasSelection(doc) {
  var res = false;
  var as = doc.activeHistoryState;
  doc.selection.deselect();
  if (as != doc.activeHistoryState) {
    res = true;
    doc.activeHistoryState = as;
  }
    return res;
}

 

 

 

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
LEGEND ,
Jan 19, 2022 Jan 19, 2022
typeof as != 'undfined'
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
Engaged ,
Jan 19, 2022 Jan 19, 2022

I got  it now, I placed the if statement in the wrong place.

The thing is that now the suspedHistory records all the steps instead of just a single step.

 

 

function main() {

  try {

    if(hasSelection(app.activeDocument)) {
      alert(hasSelection(app.activeDocument))
    }
    else {
      alert(hasSelection(app.activeDocument))
    }
  
  } catch(e){
    alert(e + ' ' + e.line);
  }

}

main();

//app.activeDocument.suspendHistory("Test","main()")

function hasSelection(doc) {
  var res = false;
  var as = doc.activeHistoryState;
  doc.selection.deselect();
    if (as != doc.activeHistoryState) {
      res = true;
      doc.activeHistoryState = as;
    }
    return res;
}

 

 

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
LEGEND ,
Jan 19, 2022 Jan 19, 2022

What other result you think to get with commented 'suspendHistory', than all history steps? 😛

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
Engaged ,
Jan 19, 2022 Jan 19, 2022

I was thinking one step for the execution of the main function. I have not used this method before and I am learnign how it works.

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