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);
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
...
Copy link to clipboard
Copied
Please show your ruler unit.
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.
Copy link to clipboard
Copied
Thank you @r-bin
Should I conclude that ActionDescriptor is the way to go when possible?
Copy link to clipboard
Copied
Thank you @r-bin
Should I conclude that ActionDescriptor is the way to go when possible?
By @Deleted User
Copy link to clipboard
Copied
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.
I had tried what you recommend before posting, without success. In fact, guides seem well positioned, the problem is the magnetism on these guides.
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;
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)?
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!
Copy link to clipboard
Copied