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

Set "snap to document grid" via javascript

New Here ,
Mar 14, 2025 Mar 14, 2025

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

TOPICS
How to , Scripting
208
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 3 Correct answers

Community Expert , Mar 14, 2025 Mar 14, 2025

Yeh ok - makes sense because it just toggles it 

 

try this

 

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

// Check if Snap to Grid is currently ON before toggling
if (snapToGridAction.isValid && snapToGridAction.enabled && snapToGridAction.checked) {
    snapToGridAction.invoke();
}
Translate
Community Expert , Mar 14, 2025 Mar 14, 2025

This one works:

app.documents[0].gridPreferences.documentGridSnapto = true;
Translate
Community Expert , Mar 14, 2025 Mar 14, 2025
Translate
Community Expert ,
Mar 14, 2025 Mar 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.");
}

 

 

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
New Here ,
Mar 14, 2025 Mar 14, 2025

Thanks a lot. This option worked.

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

if (snapToGridAction.enabled) {
snapToGridAction.invoke(); }
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
New Here ,
Mar 14, 2025 Mar 14, 2025

There is just one problem, for some reason when the setting is turned off, the opposite happens - snap to grid turned on.

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 ,
Mar 14, 2025 Mar 14, 2025

Yeh ok - makes sense because it just toggles it 

 

try this

 

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

// Check if Snap to Grid is currently ON before toggling
if (snapToGridAction.isValid && snapToGridAction.enabled && snapToGridAction.checked) {
    snapToGridAction.invoke();
}
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
New Here ,
Mar 14, 2025 Mar 14, 2025

Thank you very much, it works

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 ,
Mar 14, 2025 Mar 14, 2025

This one works:

app.documents[0].gridPreferences.documentGridSnapto = true;
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
New Here ,
Mar 14, 2025 Mar 14, 2025

Thank you very much, it works too

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 ,
Mar 14, 2025 Mar 14, 2025
LATEST
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