Copy link to clipboard
Copied
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.
.
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;
}
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"));
};
Copy link to clipboard
Copied
$.level = 0; try{alert(activeDocument.selection.bounds)}catch(err){alert('!')}
Copy link to clipboard
Copied
Thank you, this an interesting alternative.
Copy link to clipboard
Copied
Actually the alternative was from c.pfaffenbichler
Copy link to clipboard
Copied
The try/catch-approach you posted should work just as well.
Copy link to clipboard
Copied
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"));
};
Copy link to clipboard
Copied
Thnak you! This function works with the suspendHistory method.
Copy link to clipboard
Copied
Can you post final code (with used function)? I'm sure with mine it should work as well.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
typeof as != 'undfined'
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
What other result you think to get with commented 'suspendHistory', than all history steps? 😛
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now