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

Script, Guides & Snap

Guest
Apr 27, 2024 Apr 27, 2024

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
662
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

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 
...
Translate
Adobe
Community Expert ,
Apr 27, 2024 Apr 27, 2024

Please show your ruler unit.

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 ,
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 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.

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

Thank you @r-bin 

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

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

Thank you @r-bin 

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


By @Deleted User

 

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

@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 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.

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

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;

 

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

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

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!

 

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