Applescript - Keynote - Save document to another name

I am trying save the keynote file to another name onto a specific folder stored on Desktop. The current filename of the keynote presentation is

I do have another part of the script which generates slides reading an excel document and I do not have any problem. However, when trying to save the document under a new name I do get the following error message.

	[b]Couldn't auto-save document.[/b]
	[i]Your most recent changes might be lost.[/i]

Would someone know why I cannot make a save as in Keynote

THANKS in advance!

set SheetName to "EVENT23"
set theTargetFolder to ((path to desktop folder)) & "MINTO2017:" & SheetName as string
		tell application "Keynote"
			activate
			display dialog theTargetFolder
			save document 1 in theTargetFolder as Keynote
		end tell

I found no way to achieve that without GUI Scripting.


use framework "Foundation"
use scripting additions

set destFolder to ( path to desktop as text) & "4 keynote:" # Edit to fit your needs
set posixDest to POSIX path of destFolder
set newName to "new file.key"# Edit to fit your needs (don't forget the extension name)
set newFile to destFolder & newName

# Check that we may use the running OS
set OSversion to (current application's NSProcessInfo's processInfo()'s operatingSystemVersion())
--> {majorVersion:10, minorVersion:12, patchVersion:5}
tell OSversion to set OSversion to (its majorversion as text) & "." & its minorversion

considering numeric strings
	if (OSversion < "10.10") then error "The script requires  MacOS X 10.10 or higher"
	set useTextField to OSversion < "10.11" # before 10.11 the field storing the dest path was a text field, after it's a combo box
end considering


tell application id "com.apple.iWork.Keynote"
	activate
end tell
tell application "System Events" to tell process "Keynote"
	set frontmost to true
	keystroke "s" using {command down, shift down} # Duplicate
	delay 0.2
	keystroke "s" using {command down} # Save
	delay 0.2
	tell window 1
		repeat until exists sheet 1
			delay 0.02
		end repeat
		--> {radio group, checkbox, static text, scroll area, scroll area, scroll area, button, button, button, menu button, toolbar, static text, sheet}
		tell sheet 1
			class of UI elements --> {button, button, button, group, UI element, text field, static text, static text, text field}
			set value of text field 1 to newName # Set the name of the file to save
			
			keystroke "g" using {command down, shift down} # Go to folder
			repeat until exists sheet 1
				delay 0.02
			end repeat
			tell sheet 1
				class of UI elements --> {static text, combo box, button, button}
				if useTextField then
					set value of text field 1 to posixDest # if the OS is older than 10.11, the UI element is a text field
				else
					set value of combo box 1 to posixDest # if the OS is 10.11 or higher, the UI element is a combo box
				end if
				name of buttons --> {"Aller", "Annuler"}
				--keystroke return
				click button 1
			end tell -- sheet 1 (Go to)
			name of buttons --> {"Enregistrer", "Nouveau dossier", "Annuler"}
			--delay 0.1 # Required if we used keystroke return
			--keystroke return
			click button 1
		end tell -- sheet 1 (Save)
	end tell # Window 1
end tell # System Events
tell application id "com.apple.iWork.Keynote" to close document 1 # Close the duplicate

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) dimanche 25 juin 2017 15:37:12

Evan, many thanks for helping!

We are progressing, to provide some context this time Ive included the entire script including the part you gave me.

Unfortunately, there seems to be an issue with the following statement

tell application "System Events" to tell process "Keynote"
				set frontmost to true
				keystroke "s" using {command down, shift down} # Duplicate
				delay 0.2
				keystroke "s" using {command down} # Save
				delay 0.2
				tell window 1
					repeat until exists sheet 1 
use framework "Foundation"
use scripting additions

set iCellCount to ""
set SheetName to ""

ExcelFileReadingData(iCellCount, SheetName)

on ExcelFileReadingData(iCellCount, SheetName)
	
	set VidExtension to {"xlsx", "xls"}
	set selectedFile to choose file with prompt "Select a MS-EXCEL file" default location (path to desktop) of type VidExtension # Here, selectedFile is an alias
	tell application "Microsoft Excel" to open selectedFile
	delay 2
	
	-- hide excel
	tell application "System Events"
		set excel to first process whose name is "Microsoft Excel"
		set visible of excel to false
	end tell
	
	--Select EVENT to be read from EXCEL when creating KEYNOTE slides
	set issueList to {"22 - Dance ", "23 - Dance", "24 - Dance", "25 -"}
	set selectedIssue to {choose from list issueList with prompt "Please select which EVENT you want to broadcast on the web" default items "None" OK button name {"Select"} cancel button name {"Cancel"}}
	if selectedIssue is false then error number -128 -- user canceled
	set SheetName to "EVENT" & leftString(selectedIssue, " -")
	
	-- get the values
	tell application "Microsoft Excel"
		
		
		if exists sheet SheetName then
			
			tell sheet SheetName to set RowCount to ((first row index of (get end cell 101 of column 1 direction toward the top)))
			
			set iEventNameEN to (value of range ("B1") of worksheet SheetName of workbook 1) as text
			set iEventNameFR to (value of range ("C1") of worksheet SheetName of workbook 1) as text
			
			repeat with iCellCount from 2 to RowCount
				
				set iOrdrePassage to (value of range ("A" & iCellCount) of worksheet SheetName of workbook 1) as text
				set iPerformer to (value of range ("B" & iCellCount) of worksheet SheetName of workbook 1) as text
				set iRegion to (value of range ("C" & iCellCount) of worksheet SheetName of workbook 1) as text
				set iLanguage to (value of range ("D" & iCellCount) of worksheet SheetName of workbook 1) as text
				if iLanguage = "FR" then
					set iEventName to (value of range ("C1") of worksheet SheetName of workbook 1) as text
				else
					set iEventName to (value of range ("B1") of worksheet SheetName of workbook 1) as text
				end if
				
				-- Substitute text in keynote slide using information found in excel
				tell application "Keynote"
					tell front document
						if iOrdrePassage = "Warm-up" then
							duplicate (slide 1)
							tell text item 2 of current slide
								set tt to its object text
								set tt to my replaceTxt(tt, "EventName$$", "WARM-UP " & iEventName)
								set its object text to tt
							end tell
						else
							duplicate (slide 2)
							tell text item 2 of current slide
								set tt to its object text
								set tt to my replaceTxt(tt, "Performer$$", iPerformer & ", " & iRegion)
								set its object text to tt
							end tell
							tell text item 3 of current slide
								set tt to its object text
								set tt to my replaceTxt(tt, "EventName$$", iEventName)
								set its object text to tt
							end tell
						end if
					end tell
				end tell
			end repeat
			
                      --set destFolder to (path to desktop as text) & "4 keynote:" # Edit to fit your needs
                      --set posixDest to POSIX path of destFolder
                      --set newName to "new file.key" # Edit to fit your needs (don't forget the extension name)
                      --set newFile to destFolder & newName

			set destFolder to (path to desktop as text) & "keynotefolder:" # Edit to fit your needs
			set posixDest to POSIX path of destFolder
			set newName to SheetName & ".key" # Edit to fit your needs (don't forget the extension name)
			set newFile to destFolder & newName
			
			# Check that we may use the running OS
			set OSversion to (current application's NSProcessInfo's processInfo()'s operatingSystemVersion())
			--> {majorVersion:10, minorVersion:12, patchVersion:5}
			tell OSversion to set OSversion to (its majorversion as text) & "." & its minorversion
			
			considering numeric strings
				if (OSversion < "10.10") then error "The script requires  MacOS X 10.10 or higher"
				set useTextField to OSversion < "10.11" # before 10.11 the field storing the dest path was a text field, after it's a combo box
			end considering
			
			
			tell application id "com.apple.iWork.Keynote"
				activate
			end tell
			tell application "System Events" to tell process "Keynote"
				set frontmost to true
				keystroke "s" using {command down, shift down} # Duplicate
				delay 0.2
				keystroke "s" using {command down} # Save
				delay 0.2
				tell window 1
					repeat until exists sheet 1
						delay 0.02
					end repeat
					--> {radio group, checkbox, static text, scroll area, scroll area, scroll area, button, button, button, menu button, toolbar, static text, sheet}
					tell sheet 1
						class of UI elements --> {button, button, button, group, UI element, text field, static text, static text, text field}
						set value of text field 1 to newName # Set the name of the file to save
						
						keystroke "g" using {command down, shift down} # Go to folder
						repeat until exists sheet 1
							delay 0.02
						end repeat
						tell sheet 1
							class of UI elements --> {static text, combo box, button, button}
							if useTextField then
								set value of text field 1 to posixDest # if the OS is older than 10.11, the UI element is a text field
							else
								set value of combo box 1 to posixDest # if the OS is 10.11 or higher, the UI element is a combo box
							end if
							name of buttons --> {"Aller", "Annuler"}
							--keystroke return
							click button 1
						end tell -- sheet 1 (Go to)
						name of buttons --> {"Enregistrer", "Nouveau dossier", "Annuler"}
						--delay 0.1 # Required if we used keystroke return
						--keystroke return
						click button 1
					end tell -- sheet 1 (Save)
				end tell # Window 1
			end tell # System Events
			tell application id "com.apple.iWork.Keynote" to close document 1 # Close the duplicate
			
			--close active workbook
			--quit
		else
			display dialog "The sheet " & SheetName & " does not exist in " & selectedFile & ". Ensure to select the right sheet before starting this process"
		end if
	end tell
	
end ExcelFileReadingData

-- Substituing text in a keynote slide
to replaceTxt(tt, f, r)
	set AppleScript's text item delimiters to f
	set tt to tt's text items
	set AppleScript's text item delimiters to r
	set tt to tt's text items as text
	set AppleScript's text item delimiters to ""
	return tt
end replaceTxt

-- returns character found before any custom delimiters
on leftString(str, del)
	local str, del, oldTIDs
	set oldTIDs to AppleScript's text item delimiters
	try
		set str to str as string
		if str does not contain del then return str
		set AppleScript's text item delimiters to del
		set str to str's first text item
		set AppleScript's text item delimiters to oldTIDs
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't leftString: " & eMsg number eNum
	end try
end leftString

Which system are you running ?
Merdosoft products aren’t allowed on my machine so I can’t help about Excel.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) dimanche 25 juin 2017 17:25:46

Hello Evan,

I am using OSX EI Captain with Keynote 6.6.1

I am only gathering (read only) information from excel to generate the keynote slides.

Once the keynotes slides are generated I want to save them to a specific name. This is where your code is very valuable.

Best regards!

Daniel

If my memory is right, under El Capitan we must allow GUIScripting thru the Security & Privacy Preference Pane.
If you save your code as a script (.scpt) you must add Script Editor in the list of applications displayed in the pane “Privacy”.
If you save your code as an application (.app) you must add this application in the list of applications displayed in the pane “Privacy”.

Before working on your complete script, it would be good to test it as it was posted with a fake Keynote document open.

It would be neat to move the code saving the Keynote document in a dedicated handler as I did below.

use framework "Foundation"
use scripting additions

ExcelFileReadingData()

on ExcelFileReadingData()
	local iCellCount, SheetName, VidExtension, selectedFile, excel, issueList, selectedIssue, RowCount
	local iEventNameEN, iEventNameFR, iOrdrePassage, iPerformer, iRegion, iLanguage, iEventName, tt
	
	--set iCellCount to "" # seems to be useless
	--set SheetName to "" # seems to be useless
	
	set VidExtension to {"xlsx", "xls"}
	set selectedFile to choose file with prompt "Select a MS-EXCEL file" default location (path to desktop) of type VidExtension # Here, selectedFile is an alias
	tell application "Microsoft Excel" to open selectedFile
	delay 2
	
	-- hide excel
	tell application "System Events"
		set excel to first process whose name is "Microsoft Excel"
		set visible of excel to false
	end tell
	
	--Select EVENT to be read from EXCEL when creating KEYNOTE slides
	set issueList to {"22 - Dance ", "23 - Dance", "24 - Dance", "25 -"}
	set selectedIssue to {choose from list issueList with prompt "Please select which EVENT you want to broadcast on the web" default items "None" OK button name {"Select"} cancel button name {"Cancel"}}
	if selectedIssue is false then error number -128 -- user canceled
	
	# SheetName is defined here8
	set SheetName to "EVENT" & leftString(selectedIssue, " -")
	
	-- get the values
	tell application "Microsoft Excel"
		
		if exists sheet SheetName then
			
			tell sheet SheetName to set RowCount to ((first row index of (get end cell 101 of column 1 direction toward the top)))
			
			set iEventNameEN to (value of range ("B1") of worksheet SheetName of workbook 1) as text
			set iEventNameFR to (value of range ("C1") of worksheet SheetName of workbook 1) as text
			
			# iCellCount is defined here !
			repeat with iCellCount from 2 to RowCount
				
				set iOrdrePassage to (value of range ("A" & iCellCount) of worksheet SheetName of workbook 1) as text
				set iPerformer to (value of range ("B" & iCellCount) of worksheet SheetName of workbook 1) as text
				set iRegion to (value of range ("C" & iCellCount) of worksheet SheetName of workbook 1) as text
				set iLanguage to (value of range ("D" & iCellCount) of worksheet SheetName of workbook 1) as text
				if iLanguage = "FR" then
					set iEventName to (value of range ("C1") of worksheet SheetName of workbook 1) as text
				else
					set iEventName to (value of range ("B1") of worksheet SheetName of workbook 1) as text
				end if
				
				my fillKeynoteSlides(iOrdrePassage, iEventName, iPerformer, iRegion)
				
			end repeat
			my saveAsKeynote(SheetName)
		else
			display dialog "The sheet " & SheetName & " does not exist in " & selectedFile & ". Ensure to select the right sheet before starting this process"
		end if
	end tell # Excel
	
end ExcelFileReadingData

-- Substituing text in a keynote slide
to replaceTxt(tt, f, r)
	set AppleScript's text item delimiters to f
	set tt to tt's text items
	set AppleScript's text item delimiters to r
	set tt to tt's text items as text
	set AppleScript's text item delimiters to ""
	return tt
end replaceTxt

-- returns character found before any custom delimiters
on leftString(str, del)
	local str, del, oldTIDs
	set oldTIDs to AppleScript's text item delimiters
	try
		set str to str as string
		if str does not contain del then return str
		set AppleScript's text item delimiters to del
		set str to str's first text item
		set AppleScript's text item delimiters to oldTIDs
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't leftString: " & eMsg number eNum
	end try
end leftString

on fillKeynoteSlides(iOrdrePassage, iEventName, iPerformer, iRegion)
	local tt
	-- Substitute text in keynote slide using information found in excel
	tell application id "com.apple.iWork.Keynote"
		tell front document
			if iOrdrePassage = "Warm-up" then
				duplicate (slide 1)
				tell text item 2 of current slide
					set tt to its object text
					set tt to my replaceTxt(tt, "EventName$$", "WARM-UP " & iEventName)
					set its object text to tt
				end tell
			else
				duplicate (slide 2)
				tell text item 2 of current slide
					set tt to its object text
					set tt to my replaceTxt(tt, "Performer$$", iPerformer & ", " & iRegion)
					set its object text to tt
				end tell
				tell text item 3 of current slide
					set tt to its object text
					set tt to my replaceTxt(tt, "EventName$$", iEventName)
					set its object text to tt
				end tell
			end if
		end tell
	end tell # Keynote
end fillKeynoteSlides

on saveAsKeynote(SheetName)
	local destFolder, posixDest, newName, newFile, OSversion, useTextField
	--set destFolder to (path to desktop as text) & "4 keynote:" # Edit to fit your needs
	--set posixDest to POSIX path of destFolder
	--set newName to "new file.key" # Edit to fit your needs (don't forget the extension name)
	--set newFile to destFolder & newName
	
	set destFolder to (path to desktop as text) & "keynotefolder:" # Edit to fit your needs
	set posixDest to POSIX path of destFolder
	set newName to SheetName & ".key" # Edit to fit your needs (don't forget the extension name)
	set newFile to destFolder & newName
	
	# Check that we may use the running OS
	set OSversion to (current application's NSProcessInfo's processInfo()'s operatingSystemVersion())
	--> {majorVersion:10, minorVersion:12, patchVersion:5}
	tell OSversion to set OSversion to (its majorversion as text) & "." & its minorversion
	
	considering numeric strings
		if (OSversion < "10.10") then error "The script requires  MacOS X 10.10 or higher"
		set useTextField to OSversion < "10.11" # before 10.11 the field storing the dest path was a text field, after it's a combo box
	end considering
	
	tell application id "com.apple.iWork.Keynote"
		activate
	end tell
	tell application "System Events" to tell process "Keynote"
		set frontmost to true
		keystroke "s" using {command down, shift down} # Duplicate
		delay 0.2
		keystroke "s" using {command down} # Save
		delay 0.2
		tell window 1
			repeat until exists sheet 1
				delay 0.02
			end repeat
			--> {radio group, checkbox, static text, scroll area, scroll area, scroll area, button, button, button, menu button, toolbar, static text, sheet}
			tell sheet 1
				class of UI elements --> {button, button, button, group, UI element, text field, static text, static text, text field}
				set value of text field 1 to newName # Set the name of the file to save
				
				keystroke "g" using {command down, shift down} # Go to folder
				repeat until exists sheet 1
					delay 0.02
				end repeat
				tell sheet 1
					class of UI elements --> {static text, combo box, button, button}
					if useTextField then
						set value of text field 1 to posixDest # if the OS is older than 10.11, the UI element is a text field
					else
						set value of combo box 1 to posixDest # if the OS is 10.11 or higher, the UI element is a combo box
					end if
					name of buttons --> {"Aller", "Annuler"}
					--keystroke return
					click button 1
				end tell -- sheet 1 (Go to)
				name of buttons --> {"Enregistrer", "Nouveau dossier", "Annuler"}
				--delay 0.1 # Required if we used keystroke return
				--keystroke return
				click button 1
			end tell -- sheet 1 (Save)
		end tell # Window 1
	end tell # System Events
	tell application id "com.apple.iWork.Keynote" to close document 1 # Close the duplicate
	
	--close active workbook
	--quit
	
end saveAsKeynote

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) dimanche 25 juin 2017 19:19:54

Ive added Script Editor in the list of applications displayed in the pane “Privacy” and it now goes further.

The display dialog Tell sheet 1-2 get displayed and the useTextField is equal to false

As expected, the keynote document has slides being generated (reading excel) and Keynote is now the active window.

Two dialog box are displayed on top of each other:
Non-active box shows : Save is equal to EVENT22.key
Tags is empty and
Where is equal to Documents Folder

Active box is showing : …/Library/Mail/V2. and is waiting for me to click on Cancel or Go.

If I click on Cancel

  • the active dialog box disappear and the non-active one is now active.
  • I have to either click Cancel or Save. If I click on Save the file EVENT22.key gets created in the Document Folder and the process terminated with keynote still opened and now have EVENT22 as its file name.

else If I click on Go the following error message get displayed

and the script stopped at the command line

set value of combo box 1 to posixDest # if the OS is 10.11 or higher, the UI element is a combo box


MANY THANKS!

on saveAsKeynote(SheetName)
	local destFolder, posixDest, newName, newFile, OSversion, useTextField
	--set destFolder to (path to desktop as text) & "4 keynote:" # Edit to fit your needs
	--set posixDest to POSIX path of destFolder
	--set newName to "new file.key" # Edit to fit your needs (don't forget the extension name)
	--set newFile to destFolder & newName
	
	set destFolder to (path to desktop as text) & "keynotefolder:" # Edit to fit your needs
	set posixDest to POSIX path of destFolder
	set newName to SheetName & ".key" # Edit to fit your needs (don't forget the extension name)
	set newFile to destFolder & newName
	
	# Check that we may use the running OS
	set OSversion to (current application's NSProcessInfo's processInfo()'s operatingSystemVersion())
	--> {majorVersion:10, minorVersion:12, patchVersion:5}
	tell OSversion to set OSversion to (its majorversion as text) & "." & its minorversion
	
	considering numeric strings
		if (OSversion < "10.10") then error "The script requires  MacOS X 10.10 or higher"
		set useTextField to OSversion < "10.11" # before 10.11 the field storing the dest path was a text field, after it's a combo box
	end considering
	
	tell application id "com.apple.iWork.Keynote"
		activate
	end tell
	tell application "System Events" to tell process "Keynote"
		set frontmost to true
		keystroke "s" using {command down, shift down} # Duplicate
		delay 0.2
		keystroke "s" using {command down} # Save
		delay 0.2
		tell window 1
			repeat until exists sheet 1
				delay 0.02
			end repeat
			--> {radio group, checkbox, static text, scroll area, scroll area, scroll area, button, button, button, menu button, toolbar, static text, sheet}
			tell sheet 1
				class of UI elements --> {button, button, button, group, UI element, text field, static text, static text, text field}
				set value of text field 1 to newName # Set the name of the file to save
				
				keystroke "g" using {command down, shift down} # Go to folder
				repeat until exists sheet 1
					delay 0.02
				end repeat
				tell sheet 1
					display dialog "tell sheet 1-2 useTextField: " & useTextField
					class of UI elements --> {static text, combo box, button, button}
					if useTextField then
						set value of text field 1 to posixDest # if the OS is older than 10.11, the UI element is a text field
					else
						set value of combo box 1 to posixDest # if the OS is 10.11 or higher, the UI element is a combo box
					end if
					name of buttons --> {"Aller", "Annuler"}
					--keystroke return
					click button 1
				end tell -- sheet 1 (Go to)
				name of buttons --> {"Enregistrer", "Nouveau dossier", "Annuler"}
				--delay 0.1 # Required if we used keystroke return
				--keystroke return
				click button 1
			end tell -- sheet 1 (Save)
		end tell # Window 1
	end tell # System Events
	tell application id "com.apple.iWork.Keynote" to close document 1 # Close the duplicate
	
	--close active workbook
	--quit
	
end saveAsKeynote

As you are running El Capitan (10.11.x) it’s normal that useTextField is false.
As I carefully wrote twice, since 10.11, the sheet Go to no longer use a text field but uses a combo box.

set useTextField to OSversion < “10.11” # before 10.11 the field storing the dest path was a text field, after it’s a combo box
set value of combo box 1 to posixDest # if the OS is 10.11 or higher, the UI element is a combo box

When I test the handler, I got the described dialog: “tell sheet 1-2 useTextField: false
I logically clicked the button “OK” and the script did it’s job.

use framework "Foundation" # REQUIRED
use scripting additions # REQUIRED

my saveAsKeynote("badaboum")# REQUIRED. The passed name is a fake one.

on saveAsKeynote(SheetName)
	local destFolder, posixDest, newName, newFile, OSversion, useTextField
	
	set destFolder to (path to desktop as text) & "keynotefolder:"
	set posixDest to POSIX path of destFolder
	set newName to SheetName & ".key" # Edit to fit your needs (don't forget the extension name)
	set newFile to destFolder & newName
	
	# Check that we may use the running OS
	set OSversion to (current application's NSProcessInfo's processInfo()'s operatingSystemVersion())
	--> {majorVersion:10, minorVersion:12, patchVersion:5}
	tell OSversion to set OSversion to (its majorversion as text) & "." & its minorversion
	
	considering numeric strings
		if (OSversion < "10.10") then error "The script requires  MacOS X 10.10 or higher"
		set useTextField to OSversion < "10.11" # before 10.11 the field storing the dest path was a text field, after it's a combo box
	end considering
	
	tell application id "com.apple.iWork.Keynote"
		activate
	end tell
	tell application "System Events" to tell process "Keynote"
		set frontmost to true
		keystroke "s" using {command down, shift down} # Duplicate
		delay 0.2
		keystroke "s" using {command down} # Save
		delay 0.2
		tell window 1
			repeat until exists sheet 1
				delay 0.02
			end repeat
			--> {radio group, checkbox, static text, scroll area, scroll area, scroll area, button, button, button, menu button, toolbar, static text, sheet}
			tell sheet 1
				class of UI elements --> {button, button, button, group, UI element, text field, static text, static text, text field}
				set value of text field 1 to newName # Set the name of the file to save
				
				keystroke "g" using {command down, shift down} # Go to folder
				repeat until exists sheet 1
					delay 0.02
				end repeat
				tell sheet 1
					display dialog "tell sheet 1-2 useTextField: " & useTextField
					class of UI elements --> {static text, combo box, button, button}
					if useTextField then
						set value of text field 1 to posixDest # if the OS is older than 10.11, the UI element is a text field
					else
						set value of combo box 1 to posixDest # if the OS is 10.11 or higher, the UI element is a combo box
					end if
					name of buttons --> {"Aller", "Annuler"}
					--keystroke return
					click button 1
				end tell -- sheet 1 (Go to)
				name of buttons --> {"Enregistrer", "Nouveau dossier", "Annuler"}
				--delay 0.1 # Required if we used keystroke return
				--keystroke return
				click button 1
			end tell -- sheet 1 (Save)
		end tell # Window 1
	end tell # System Events
	tell application id "com.apple.iWork.Keynote" to close document 1 # Close the duplicate
	
	--close active workbook
	--quit
	
end saveAsKeynote

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) lundi 26 juin 2017 11:15:01

No need GUI scripting here.

Let’s see the 1st case: the OP has some Keynote file opened in the KeyNote (as here) and they want save it with other name at other folder, that is, deleting the old file:


set SheetName to "EVENT23"
set theTargetFolder to ((path to desktop folder) as text) & "MINTO2017:"

tell application "Keynote"
	activate
	set theHFSPath to (file of document 1) as text -- remember HFS path to use later
	save document 1 -- save changes at old location
end tell

tell application "Finder"
	if not (folder theTargetFolder exists) then make new folder at (path to desktop folder) with properties {name:"MINTO2017"}
	try
		set theKeyNoteFile to (move file theHFSPath to folder theTargetFolder)
		set name of the theKeyNoteFile to SheetName & ".key"
	end try
end tell

tell application "Keynote" to activate -- Let's see what we have now (see name of the document)

Let’s see the 2nd case: the OP has some Keynote file opened in the KeyNote (as here) and they want export it with other name at other folder, that is, keeping the old file:

  1. Grant Full Disk Access to Keynote.app.
  2. Run script:

set SheetName to "EVENT23"
set theTargetFolder to ((path to desktop folder) as text) & "MINTO2017:"
set theTargetFile to ((path to desktop folder) as text) & "MINTO2017:" & SheetName & ".key"

tell application "Finder"
	if not (folder theTargetFolder exists) then make new folder at (path to desktop folder) with properties {name:"MINTO2017"}
	if not (file theTargetFile exists) then make new file at folder theTargetFolder with properties {name:(SheetName & ".key")}
end tell

tell application "Keynote"
	activate
	export document 1 to file theTargetFile as Keynote 09
end tell