Script To Set Desktop View Option To Snap To Grid

Mac Studio
macOS v13.3.1

I use the Snap To Grid setting for all folders that display in icon view. As well, I do this for the Desktop. From time to time, this Desktop setting becomes turned off, with neither my knowledge nor my consent. I select Finder → View → Show View Options → Snap To Grid to select it, again. This is cumbersome.

Each day, my Mac restarts at 6:00 AM. After I log in, I run an AppleScript that launches several applications, sets the position and dimensions of the applications, opens two websites in Safari, hides the applications, opens four folders, sets the position and dimensions of the folders, and then, copies file X from folder Y to folder Z.

I want to add one more process to this script, which sets the Desktop view option to Snap To Grid.

I haven’t determined the code to accomplish this. I haven’t found anything online.

Does AppleScript support this?

Thank you.

Try this

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

local flag
tell application "Finder"
	if (arrangement of icon view options of window of desktop) ≠ snap to grid then
		set flag to true
	else
		set flag to false
	end if
	select desktop's window
	activate
end tell

if flag then
	tell application "System Events" to tell application process "Finder"
		if not (exists window "Desktop") then
			click menu item "Show View Options" of menu "View" of menu bar 1
			repeat until exists window "Desktop"
				delay 0.1
			end repeat
		end if
		tell window "Desktop"
			click pop up button 2
			repeat until exists menu 1 of pop up button 2
				delay 0.1
			end repeat
			click menu item "Snap to Grid" of menu 1 of pop up button 2
			click (button 1 whose description is "close button")
		end tell
	end tell
end if

The Finder arrangement is settable:

tell application "Finder" to tell container window of desktop
	if arrangement of its icon view options is not snap to grid then ¬
		set arrangement of its icon view options to snap to grid
end tell

Robert Fern,

Thank you for your prompt response, and, for a fine piece of work. My gratitude.

Privacy & Security / Assistive Access have reared its head.

I copied your work, and, I pasted it into my Launch script. I exported the Launch script to a script app, overwriting the existing script app.

If I run my modified script from within Script Editor, it works properly, regardless of conditions.

If I run the exported script app, and, if the desktop Snap to Grid setting is already set to true, then, the app runs and quits, properly.

If I run the exported script app, and, if the desktop Snap to Grid setting is false, then, the app runs and stops at the new code, producing a Finder error message: Launch Applications M1 Mac - APP is not allowed assistive access. System Events got an error: Launch Applications M1 Mac - APP is not allowed assistive access. (-25211)

System Settings → Privacy & Security → Accessibility shows the script app and Script Editor in the list, and, enabled. Yet, the problem, persists. I even removed both of these from the list, quit System Settings, deleted the script app, recreated the script app, and then re-added it and Script Editor to the list. The problem exists.

I have searched System Settings, but, am unable to locate any other avenues to grant full access to the script app.

Do you have any ideas?

Kurt

Kurt

I know @robertfern is one of the experienced programmers. I’m not sure, but I think he was in a hurry in this case. The fact is that his solution uses GUI scripting, which is best used only as a last resort.

The error says that the launcd utility must be given the authority to control your computer. Better not to do this in this case. I came up with a solution that logically should work. Have you tried it?

There was a reason I did it this way.
If you do the direct tell Application “Finder” way, although it says it changed, when you go into the gui manually afterwards, the result hasn’t changed.
It won’t stick.
That is why I used GUI scripting

1 Like

Strange, requires Finder restarting to stick.

tell application "Finder"
	tell container window of desktop
		if arrangement of its icon view options is not snap to grid then ¬
			set arrangement of its icon view options to snap to grid
	end tell
	quit
	repeat while running
		delay 0.2
	end repeat
	activate
end tell

I will do timing tests to see if there might be a time limit for the finder to reload settings after a change

I put in an automated delay just in case, to work around the “error: Connection is invalid.”, which appears when restarting many applications this way (quit-activate).

Still, my previous script has a drawback - it restarts the Finder when not needed too. This one would be more correct:

tell application "Finder"
	if arrangement of icon view options of container window of desktop is not snap to grid then
		set arrangement of icon view options of container window of desktop to snap to grid
		quit
		repeat while running
			delay 0.2
		end repeat
		activate
	end if
end tell

Robert Kniazidis,

Thank you for your prompt responses. Nice piece of simple code. My gratitude.

I copied your work, and, I pasted it into my Launch script. I exported the Launch script to a script app, overwriting the existing script app.

If I run my modified script from within Script Editor, or, if I run the exported script app, and, if the desktop Snap to Grid setting is already set to true, then, the process runs to completion, as I would expect it.

If I run my modified script from within Script Editor, or, if I run the exported script app, and, if the desktop Snap to Grid setting is false, then, the process runs to completion. Then, it closes the folders, that it opened, and, it displays Safari.

I look forward to hearing from both of you gentlemen, again.

Kurt

This should fix the issue:

tell application "Finder"
	if arrangement of icon view options of container window of desktop is not snap to grid then
		set arrangement of icon view options of container window of desktop to snap to grid
		tell scripting additions to do shell script "killall 'Finder'"
	end if
end tell

Robert Kniazidis,

This code no longer closes the folders. It does display Safari, at the end of run. This occurs by both methods of running the script or by running the exported script app. It only occurs if the desktop Snap to Grid setting is set to false.

Kurt

With the erroneous closing of open Finder windows, everything was clear and I easily fixed it. But now it’s getting hard for me. Where does Safari come from? It depends on which scripts are run before and after my script.

Robert Kniazidis,

(**************** DETERMINE SCREEN RESOLUTION ****************)
(**************************************************************)

tell application "Finder"
	set screen_resolution to bounds of window of desktop
	set screen_width to item 3 of screen_resolution
	set screen_height to item 4 of screen_resolution
end tell


(**************************************************************)
(****** LAUNCH APPLICATIONS / SET WINDOW POSITION AND SIZE ******)
(**************************************************************)

try
	tell application "Safari"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.6) / 2), 0, (screen_width * (1 + 0.6) / 2), screen_height}
	end tell
end try

try
	tell application "Mail"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.6) / 2), 0, (screen_width * (1 + 0.6) / 2), screen_height}
	end tell
end try

try
	tell application "Messages"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.2) / 2), (screen_height * (1 - 0.8) / 2), (screen_width * (1 + 0.2) / 2), (screen_height * (1 + 0.8) / 2)}
	end tell
end try

(*try
	tell application "FaceTime"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.2) / 2), (screen_height * (1 - 0.8) / 2), (screen_width * (1 + 0.2) / 2), (screen_height * (1 + 0.8) / 2)}
	end tell
end try*)

try
	tell application "Contacts"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.6) / 2), 0, (screen_width * (1 + 0.6) / 2), screen_height}
	end tell
end try

try
	tell application "Calendar"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.9) / 2), 0, (screen_width * (1 + 0.9) / 2), screen_height}
	end tell
end try

try
	tell application "Notes"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.4) / 2), (screen_height * (1 - 0.9) / 2), (screen_width * (1 + 0.4) / 2), (screen_height * (1 + 0.8) / 2)}
	end tell
end try

try
	tell application "Maps"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.8) / 2), 0, (screen_width * (1 + 0.8) / 2), screen_height}
	end tell
end try

try
	tell application "Music"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.6) / 2), 0, (screen_width * (1 + 0.6) / 2), screen_height}
	end tell
end try

try
	tell application "TV"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.6) / 2), 0, (screen_width * (1 + 0.6) / 2), screen_height}
	end tell
end try

tell application "TextEdit" to activate
tell application "Preview" to activate
tell application "FindMy" to activate
tell application "Microsoft Word" to activate
tell application "Microsoft Excel" to activate
(*tell application "Alpha" to activate*)

try
	tell application "Weather"
		activate
		set the bounds of the first window to {(screen_width * (1 - 0.6) / 2), 0, (screen_width * (1 + 0.6) / 2), screen_height}
	end tell
end try

tell application "TeX Live Utility" to activate


(**************************************************************)
(******************** OPEN SAFARI WEBSITES ********************)
(**************************************************************)

tell application "Safari"
	open location "https://forecast.weather.gov/MapClick.php?CityName=Jackson&state=MI&site=GRR&textField1=42.2432&textField2=-84.4048#.ZD_BIy_MIQ9"
	activate
	open location "https://dvd.netflix.com/Queue?qtype=DD&lnkctr=mhbque"
	activate
end tell


(**************************************************************)
(******************** HIDE ALL APPLICATIONS ********************)
(**************************************************************)

delay 1

tell application "Finder"
	activate
end tell

tell application "System Events"
	set visibleApps to every process whose visible is true and name is not "Finder"
	repeat with theApp in visibleApps
		set visible of theApp to false
	end repeat
end tell


(**************************************************************)
(********* OPEN FOLDERS / SET WINDOW POSITION AND SIZE *********)
(**************************************************************)

tell application "Finder" to open "Macintosh HD:Users:kurttodoroff:Z Everything Else:Kurt:LaTeX Documents:F-111T Flight Manual"
set F_111T_Flight_Manual_folder_X to 140
set F_111T_Flight_Manual_folder_Y to 230
set F_111T_Flight_Manual_folder_width to 800
set F_111T_Flight_Manual_folder_height to 960
tell application "Finder" to set the bounds of the front Finder window to {F_111T_Flight_Manual_folder_X, F_111T_Flight_Manual_folder_Y, F_111T_Flight_Manual_folder_X + F_111T_Flight_Manual_folder_width, F_111T_Flight_Manual_folder_Y + F_111T_Flight_Manual_folder_height}
tell application "Finder" to open "Macintosh HD:Users:kurttodoroff:Z Everything Else:Kurt:LaTeX Documents:F-111T"
tell application "Finder" to open "Macintosh HD:Users:kurttodoroff:Z Everything Else:Kurt:LaTeX Documents:F-111 Compendium"
tell application "Finder" to open "Macintosh HD:Users:kurttodoroff:Downloads"
tell application "Finder" to open "Macintosh HD:Users:kurttodoroff:Z Everything Else:Kurt:LaTeX Documents:F-111T Flight Manual"


(**************************************************************)
(*********** DUPLICATE AND MOVE F-111T LATEX CBY FILE ***********)
(**************************************************************)

tell application "Finder"
	try
		duplicate file "Macintosh HD:Users:kurttodoroff:Z Everything Else:Kurt:LaTeX Documents:F-111T Flight Manual:F-111T-Flight-Manual.cpy" to "Macintosh HD:Users:kurttodoroff:Z Everything Else:Kurt:LaTeX Documents:F-111T" with replacing
	end try
end tell


(**************************************************************)
(*********** SET DESKTOP VIEW OPTIONS TO SNAP TO GRID ***********)
(**************************************************************)

tell application "Finder"
	if arrangement of icon view options of container window of desktop is not snap to grid then
		set arrangement of icon view options of container window of desktop to snap to grid
		tell scripting additions to do shell script "killall 'Finder'"
	end if
end tell```

I am unable to locate a control to properly format the code, in this posting.

I have reviewed your script. The only thing I can guess is that you meant to say that “Finder” loses focus and that focus is transferred to “Safari”.

Well, then try instructing the Finder to grab focus again (in other words, become the front app again):
.

tell application "Finder"
	if arrangement of icon view options of container window of desktop is not snap to grid then
		set arrangement of icon view options of container window of desktop to snap to grid
		tell scripting additions to do shell script "killall 'Finder'"
	end if
end tell

repeat
	try
		tell application "Finder" to activate
		exit repeat
	end try
end repeat

.
or,
.

tell application "Finder"
	if arrangement of icon view options of container window of desktop is not snap to grid then
		set arrangement of icon view options of container window of desktop to snap to grid
		tell scripting additions to do shell script "killall 'Finder'"
	end if
	repeat
		try
			tell it to activate
			exit repeat
		end try
	end repeat
end tell

Both of these code sets remove all content from the desktop, and, still display Safari.

The only method that I have discovered, to redisplay the desktop content, is to rerun the same script. This restores desktop content, seeing how the first run set Snap to Grid to true.

Bringing other applications to the foreground, does not restore desktop content. Right-clicking on the desktop, does not reveal a contextual menu.

Hi.

Three backticks on lines above and below the code:

```
Script code here
```

There’s a Markdown reference here.

Thank you, Nigel.

Good to hear from you, again. It’s been a while.

Kurt

Another reason may be that you have separate scripts/applications running almost simultaneously in your launchd properties list that create synchronization problems. This will be more difficult to deal with.