I am using the following in my script to browse for and open an excel file in Numbers (below)
Is there a way to ensure that the rest of my script only runs on the file I have opened?
My issue is that currently, if the user cancels the file browse UI or if there are other spreadsheets open in the background, the rest of the script that runs after this section runs on whatever is then the active document.
maybe use “tell chosenDocumentFile to activate” or something like that?
Thanks in advance for any help!
tell application "Numbers"
activate
try
set the chosenDocumentFile to ¬
(choose file of type ¬
{"com.apple.iwork.numbers.numbers", ¬
"com.apple.iwork.numbers.sffnumbers", ¬
"com.microsoft.excel.xls", ¬
"org.openxmlformats.spreadsheetml.sheet"} ¬
default location (path to documents folder) ¬
with prompt "Choose the Numbers document or Excel workbook to open:")
open the chosenDocumentFile
on error errorMessage number errorNumber
if errorNumber is not -128 then
display alert errorNumber message errorMessage
end if
end try
end tell
try
set the chosenDocumentFile to ¬
(choose file of type ¬
{"com.apple.iwork.numbers.numbers", ¬
"com.apple.iwork.numbers.sffnumbers", ¬
"com.microsoft.excel.xls", ¬
"org.openxmlformats.spreadsheetml.sheet"} ¬
default location (path to documents folder) ¬
with prompt "Choose the Numbers document or Excel workbook to open:")
on error errorMessage number errorNumber
if errorNumber is not -128 then
display alert errorNumber message errorMessage
end if
end try
do shell script "open -b com.apple.iwork.numbers " & quoted form of POSIX path of chosenDocumentFile
tell application id "com.apple.iWork.Numbers"
activate
repeat 10 times
try
set myDoc to name of document 1
exit repeat
end try
tell me to delay 0.2
end repeat
tell document myDoc
name of sheets
end tell
end tell
Sometimes I wish to use a specific application : Numbers '09 or Numbers v3 or 4.
In such case I replace the single instruction using Shell script by :
set pathToApp to POSIX path of ((path to applications folder from local domain as text) & "iWork '09:Numbers 09.app")
do shell script "open -a " & quoted form of pathToApp & space & quoted form of POSIX path of chosenDocumentFile
With that I’m sure of the running app and of the triggered document
There is an alternate syntax using the Finder:
set pathToApp to (path to applications folder from local domain as text) & "iWork '09:Numbers 09.app"
tell application "Finder" to open chosenDocumentFile using alias pathToApp
but as I often wrote, I hate the Finder.
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) jeudi 27 octobre 2016 15:54:15