Mydestinationfolder and System Events

Hi
I have a script for Safari which saves web pages to the desktop
however I’d like to save them in the folder I choose as the script opens rather than desktop
how to add this to the script?
set mydestinationfolder to (choose folder)
tell application “System Events”
tell process “Safari”
keystroke “S” using command down – Save As.
repeat until sheet 1 of window 1 exists
delay 0.1
end repeat

and so on …

Model: MACPRO 5.1
Browser: Safari 537.36
Operating System: Mac OS X (10.10)

You will get the needed info in this script which I am writing for an asker.

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

# Use a CLI available from : http://www.bluem.net/en/mac/cliclick/


property path2cliclick : "/Users/admin/bin/cliclick"

set OSversion to text 1 thru 5 of system version of (system info)

set sourceFolder to choose folder

tell application "System Events"
	set theFiles to path of files of sourceFolder whose type identifier is "public.html"
end tell

tell application "Safari"
	close every window
end tell

set PosixFolder to POSIX path of sourceFolder

repeat with aFile in theFiles
	set quotedPosixPath to quoted form of POSIX path of aFile
	
	# Open the file in Safari
	do shell script "open -b com.apple.Safari " & quotedPosixPath
	
	tell application "System Events"
		set nomPosixDuFichierSource to name of disk item aFile
		set nomPosixSeul to (my getBareName:nomPosixDuFichierSource)
		# Take care of possible slash in the fileName
		set nomHfsSeul to (my replace:nomPosixSeul existingString:":" newString:"/")
		set nomHfsDuFichierWebArchive to nomHfsSeul & ".webarchive"
		(my deleteItem:nomHfsDuFichierWebArchive inFolder:PosixFolder)
		tell process "Safari"
			set frontmost to true
			# Wait until the document is completely open
			repeat 10 times
				tell me to delay 0.2
				if exists window 1 then exit repeat
			end repeat
			
			tell window 1
				class of UI elements
				set {xLeft, yTop} to its position
				set {itsWidth, itsHeight} to its size
				# Double click in the window to put the focus upon in
				tell me to do shell script path2cliclick & " dc:" & xLeft + itsWidth div 2 & "," & yTop + itsHeight div 2
				# Save As.
				keystroke "S" using {command down}
				repeat
					tell me to delay 0.1
					if exists sheet 1 then exit repeat
				end repeat
				
				tell sheet 1
					# Some instructions used to identifie the structure of the interface
					-- class of UI elements --> {group, button, button, button, group, UI element, text field, static text, static text, text field}
					value of text fields --> {"Coup-de-torchon.webarchive", ""}
					# Define the name of the webarchive file. Required if we want to keep possible slash in the name.
					set value of text field 1 to nomHfsDuFichierWebArchive
					
					-- class of UI elements of group 2 --> {group, radio group, group, pop up button, text field, splitter group}
					-- value of pop up button 1 of group 2 --> "Bureau"
					
					-- class of UI elements of group 1 --> {pop up button, static text, static text}
					-- value of pop up button 1 of group 1 --> "Source de la page"
					set myPopUp to pop up button 1 of group 1
					click myPopUp # select the pop up "Format"
					# Use a loop with try/end try to be sure that the menu items are available
					repeat 5 times
						tell me to delay 0.1
						try
							name of menu items of menu 1 of myPopUp --> {"Source de la page", "Archive web"}
							exit repeat
						end try
					end repeat
					# Select the format WebArchive
					click menu item 2 of menu 1 of myPopUp
					tell me to delay 0.1
					# Open the sheet allowing us to define the path to the destination folder
					keystroke "g" using {command down, shift down}
					repeat
						tell me to delay 0.1
						if exists sheet 1 then exit repeat
					end repeat
					--class of UI elements --> {group, button, button, button, group, UI element, text field, static text, static text, text field, sheet}
					tell sheet 1
						--class of UI elements --> {static text, combo box, button, button}
						if OSversion is "10.12" then
							set theTarget to combo box 1
						else # before 10.12 it was a text field
							set theTarget to text field 1
						end if
						# define the destination folder
						set value of theTarget to PosixFolder
						-- name of buttons --> {"Aller", "Annuler"}
						# Validate the entry
						click button 1 -- keystroke return
					end tell # sheet 1
					-- name of buttons --> {"Enregistrer", "Nouveau dossier", "Annuler"}
					click button 1 -- keystroke return
				end tell # sheet 1
				
			end tell # window 1
		end tell # process Safari
	end tell # System Events.
	tell application "Safari"
		close every window
	end tell
end repeat

#=====#=====#=====#=====#=====#=====

on replace:sourceString existingString:d1 newString:d2
	set sourceString to current application's NSString's stringWithString:sourceString
	return (sourceString's stringByReplacingOccurrencesOfString:d1 withString:d2) as text
end replace:existingString:newString:

#=====#=====#=====#=====#=====#=====

on getBareName:thePath
	set pathNSString to current application's NSString's stringWithString:thePath
	return (pathNSString's stringByDeletingPathExtension) as text
end getBareName:

#=====#=====#=====#=====#=====#=====

on deleteItem:proposedName inFolder:cheminPosixDuFichierSource # appelé par une instruction
	
	log linefeed & "# Entre dans deleteItem,  proposedName = " & proposedName & linefeed & "cheminPosixDuFichierSource = " & cheminPosixDuFichierSource
	
	copy proposedName to propozedName
	set theFolderURL to current application's |NSURL|'s fileURLWithPath:cheminPosixDuFichierSource
	if class of proposedName is text then set proposedName to current application's NSString's stringWithString:proposedName
	set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
	set theSourceURL to theFolderURL's URLByAppendingPathComponent:proposedName
	set theFileManager to current application's NSFileManager's |defaultManager|()
	--set {theResult, theError} to theFileManager's removeItemAtURL:theSourceURL |error|:(specifier)
	theFileManager's removeItemAtURL:theSourceURL |error|:(missing value)
	-- if not (theResult as boolean) then error (theError's |localizedDescription|() as text)
	
end deleteItem:inFolder:

#=====#=====#=====#=====#=====#=====

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) vendredi 28 octobre 2016 20:57:25