Changing the Download Folder in Safari

I have a short routine that changes the download folder location in safari. What follows works but seems very clumsy any suggestions?

Thanks


--ChangeDownloadFolder.scpt
--1st Choose the Folder that is to be the destination for Downloads
set Dfolder to (choose folder) as string
set Dfolder to POSIX path of Dfolder
set Dfolder to Dfolder & "\""

tell application "Safari" to quit
delay 0.5
do shell script "defaults write com.apple.safari DownloadsPath -string \"" & Dfolder
--The following sequence is very clumsy but seems at present to be necessary
tell application "Safari" to activate
delay 0.5
tell application "Safari" to quit
delay 0.5
tell application "Safari" to activate
delay 0.5

Model: MacBook Pro
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.13

Why all these quit–>activate–>quit–>activate and delay–>delay–>delay without understanding what they does?
I already showed you the code 4 months ago. That is, how to do it. Okay, I’ll repeat the code with additional explanations.


-- 1st Choose the Folder that is to be the destination for Downloads
set downloadsFolder to quoted form of POSIX path of (choose folder)

-- If do not quit the "Safari", the changes you make in the defaults will be ignored.
-- So, quit Safari. Once only. What need quit start 4 times as you do?...
tell application "Safari" to quit

-- The Safari doesn't quit immediatelly, so check when it quits fully, using repeat loop
-- One delay only here is need. You should understand - this makes the script stable... Nothing more.
tell application "System Events"
	repeat while application process "Safari" exists
		delay 0.1
	end repeat
end tell

-- make the changes in the defauts
do shell script ("defaults write com.apple.Safari DownloadsPath " & downloadsFolder)

-- the change is made. But you can open "Safari" to see in its Preferences that I am not lying
tell application "Safari" to activate

Thank you and my apologies, its 60 years since I was hardwiring IBM 501’s and my memory fails me at times. I have started an AppleScript utility file so hopefully will not make the same mistake again.