Skip to main content
Participating Frequently
Mar 14, 2025 7:26 AM UTC
Answered

Set "snap to document grid" via javascript

Hello

Is there any options to set snap to document grid off via javascript?

 

The code from manual doesn't work

with(myDocument.gridPreferences){
documentGridShown = false;
documentGridSnapTo = false;
baselineGridShown = false;
}

 

Thanks

8 replies

Peter Kahrel
Community Expert
Community Expert
Mar 14, 2025 10:31 AM UTC

This one works:

app.documents[0].gridPreferences.documentGridSnapto = true;
Translate
Participating Frequently
Mar 14, 2025 10:39 AM UTC

Thank you very much, it works too

Translate
Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
Mar 14, 2025 2:01 PM UTC
Community Expert
Mar 14, 2025 8:19 AM UTC

It's possible other settings like 'Snap to Guides' or 'Smart Guides' are influencing object placement. 

So something like this maybe work for you

var myDocument = app.activeDocument;
myDocument.guidePreferences.guidesLocked = false;
myDocument.guidePreferences.guidesShown = false;
app.activeWindow.transformReferencePoint = AnchorPoint.TOP_LEFT_ANCHOR;

 

You can invoke Document Grid Snap via the menus which turns it on

// Access the 'Snap to Document Grid' menu action
var snapToGridAction = app.menuActions.itemByName("$ID/Snap to Document Grid");

// Check if the action is valid and enabled
if (snapToGridAction.isValid && snapToGridAction.enabled) {
    // Invoke the action to toggle the snapping
    snapToGridAction.invoke();
} else {
    alert("The 'Snap to Document Grid' action is not available.");
}

 

 

Translate
Participating Frequently
Mar 14, 2025 8:52 AM UTC

Thanks a lot. This option worked.

var snapToGridAction = app.menuActions.itemByName("$ID/Snap to Document Grid");

if (snapToGridAction.enabled) {
snapToGridAction.invoke(); }
Translate