Saturday, September 23, 2017

#1 2017-06-25 05:03:06 am

DPaquin
Member
Registered: 2012-01-23
Posts: 69

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

MASTER - Keynote for video mixer.key


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.

        Couldn't auto-save document.
        Your most recent changes might be lost.

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

THANKS in advance!

Applescript:

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

Offline

 

#2 2017-06-25 08:42:21 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3194

Re: Applescript - Keynote - Save document to another name

I found no way to achieve that without GUI Scripting.

Applescript:


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

Offline

 

#3 2017-06-25 09:40:49 am

DPaquin
Member
Registered: 2012-01-23
Posts: 69

Re: Applescript - Keynote - Save document to another name

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

Repeat until exists sheet 1

-- error "System Events got an error: Script Editor is not allowed assistive access." number -25211


Applescript:

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

Applescript:

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

Offline

 

#4 2017-06-25 10:26:36 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3194

Re: Applescript - Keynote - Save document to another name

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

Offline

 

#5 2017-06-25 11:46:27 am

DPaquin
Member
Registered: 2012-01-23
Posts: 69

Re: Applescript - Keynote - Save document to another name

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

Offline

 

#6 2017-06-25 12:20:07 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3194

Re: Applescript - Keynote - Save document to another name

DPaquin wrote:

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.

Applescript:

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

Last edited by Yvan Koenig (2017-06-25 12:53:50 pm)

Offline

 

#7 2017-06-25 03:17:38 pm

DPaquin
Member
Registered: 2012-01-23
Posts: 69

Re: Applescript - Keynote - Save document to another name

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

Error ystem Events got an error: Can’t get combo box 1 of sheet 1 of sheet 1 of window 1 of process "Keynote". Invalid index.


and the script stopped at the command line
                               

Applescript:

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!




Applescript:

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

Offline

 

#8 2017-06-26 04:19:26 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3194

Re: Applescript - Keynote - Save document to another name

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.

Applescript:

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)