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

Script, Guides & Snap

Community Beginner ,
Apr 27, 2024 Apr 27, 2024

Copy link to clipboard

Copied

Hello,
I am experiencing an issue of inaccurate magnetism with guides added in JavaScript. Here is an example:
1. I add two guides using the code below.
2. With Snap enabled, I draw a selection.
3. This selection should be 200px, but I often end up with 201px.
4. If I manually add the guides, everything is fine.
Does anyone know where this problem comes from?

app.activeDocument.guides.add (Direction.HORIZONTAL, 600);
app.activeDocument.guides.add (Direction.HORIZONTAL, 800);

 Guides.jpg

TOPICS
Actions and scripting

Views

171

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

correct answers 1 Correct answer

People's Champ , Apr 27, 2024 Apr 27, 2024

Yes, a really interesting bug.

Try using this code to add guides.

 

var d = new ActionDescriptor();
var d1 = new ActionDescriptor();
d1.putUnitDouble(stringIDToTypeID("position"), stringIDToTypeID("pixelsUnit"), 600);
d1.putEnumerated(stringIDToTypeID("orientation"), stringIDToTypeID("orientation"), stringIDToTypeID("horizontal"));
d.putObject(stringIDToTypeID("new"), stringIDToTypeID("guide"), d1);
executeAction(stringIDToTypeID("make"), d, DialogModes.NO);


var d = new ActionDescriptor();
var 
...

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 27, 2024 Apr 27, 2024

Copy link to clipboard

Copied

Please show your ruler unit.

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 ,
Apr 27, 2024 Apr 27, 2024

Copy link to clipboard

Copied

Yes, a really interesting bug.

Try using this code to add guides.

 

var d = new ActionDescriptor();
var d1 = new ActionDescriptor();
d1.putUnitDouble(stringIDToTypeID("position"), stringIDToTypeID("pixelsUnit"), 600);
d1.putEnumerated(stringIDToTypeID("orientation"), stringIDToTypeID("orientation"), stringIDToTypeID("horizontal"));
d.putObject(stringIDToTypeID("new"), stringIDToTypeID("guide"), d1);
executeAction(stringIDToTypeID("make"), d, DialogModes.NO);


var d = new ActionDescriptor();
var d1 = new ActionDescriptor();
d1.putUnitDouble(stringIDToTypeID("position"), stringIDToTypeID("pixelsUnit"), 800);
d1.putEnumerated(stringIDToTypeID("orientation"), stringIDToTypeID("orientation"), stringIDToTypeID("horizontal"));
d.putObject(stringIDToTypeID("new"), stringIDToTypeID("guide"), d1);
executeAction(stringIDToTypeID("make"), d, DialogModes.NO);

 

The snap seems to be working as it should.

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 Beginner ,
Apr 27, 2024 Apr 27, 2024

Copy link to clipboard

Copied

Thank you @r-bin 

Should I conclude that ActionDescriptor is the way to go when possible?

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 ,
Apr 27, 2024 Apr 27, 2024

Copy link to clipboard

Copied

quote

Thank you @r-bin 

Should I conclude that ActionDescriptor is the way to go when possible?


By @Ralph S.

 

You didn’t specify whether, when creating guides using this code, everything is fine with snapping the selection to the guides?
 
Classic DOM methods, such as guides.add(), are based (according to unverified rumors) on AM code and the use of ActionDescriptor and executeAction. So if it is more convenient in the code, then it is not forbidden to use it. However, as you yourself noticed in your case, there may be some oddities and bugs when using only DOM methods.

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 Beginner ,
Apr 28, 2024 Apr 28, 2024

Copy link to clipboard

Copied

@r-bin

Yes, with the code above, everything is fine.

Thank you for the help and the explanation, I will keep this in mind in the future.

 

@Stephen_A_Marsh

I had tried what you recommend before posting, without success. In fact, guides seem well positioned, the problem is the magnetism on these guides.

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 ,
Apr 27, 2024 Apr 27, 2024

Copy link to clipboard

Copied

What happens when you explicitly set the ruler units via the DOM (which is always a good practice)?

 

var origRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
app.activeDocument.guides.add (Direction.HORIZONTAL, 600);
app.activeDocument.guides.add(Direction.HORIZONTAL, 800);
app.preferences.rulerUnits = origRulerUnits;

 

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 ,
Apr 27, 2024 Apr 27, 2024

Copy link to clipboard

Copied


@Stephen_A_Marsh wrote:

What happens when you explicitly set the ruler units via the DOM (which is always a good practice)?

 

 

That's not the point.
 
P.S. A good practice would be to use UnitValue(800, "px") instead of just 800. But that doesn't help in this case.

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 ,
Apr 28, 2024 Apr 28, 2024

Copy link to clipboard

Copied

As this appears to be an ExtendScript DOM issue, I was curious to see what happens with the UXP DOM as a .psjs in v2024:

 

https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/guides/#add

 

It's a shame that I couldn't get a result!

 

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 ,
Apr 28, 2024 Apr 28, 2024

Copy link to clipboard

Copied

LATEST
It looks like I found the cause of this "bug".
Guides created through DOM and AM code are no different.
This is confirmed by a bit-by-bit comparison of psd files with such guides.
When using DOM guides.add(), some internal information is not updated and it appears that the SnapTo function does not see or know about the new guides.
If you save the file and then Revert or re-open it, then SnapTo starts working as it should for such guides.
 
This is definitely a bug.

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