Skip to main content
Participating Frequently
March 14, 2025
Answered

Set "snap to document grid" via javascript

  • March 14, 2025
  • 2 replies
  • 688 views

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

2 replies

Peter Kahrel
Community Expert
Community Expert
March 14, 2025

This one works:

app.documents[0].gridPreferences.documentGridSnapto = true;
Participating Frequently
March 14, 2025

Thank you very much, it works too

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
March 14, 2025
Community Expert
March 14, 2025

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.");
}

 

 

Participating Frequently
March 14, 2025

Thanks a lot. This option worked.

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

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