Hmmmmm… It’s just very odd that Excel would do this in the first place, and I’m not certain a handler would solve it. To run the gamut of possible fixes:
tell application "Finder" to ¬
set fileList to every file of folder sourceFolder as list
This would return a list of Finder file specifications if there is more than one file in sourceFolder, and a single alias in a one item list if there is only one file. Excel may require an alias or a file spec of some type - the entry for the open command in Excel’s scripting dictionary should indicate what type of object the open command expects. Standard for the open command is an alias object, so to get the Finder to create a list of aliases instead of a list of Finder file specification you would say “as alias list” instead of “as list” when getting all the files. This will error if there is only one file, so the routine to handle that would be:
tell application "Finder"
try
set fileList to every file of sourceFolder as alias list
on error
set fileList to every file of sourceFolder as alias as list
end try
end tell
With this, the variable ‘fileList’ will always be a list of aliases (unless there’s no files). Kinda wierd, but while
alias “Mac HD:Some Folder:A File”
and
file “A File” of folder “Some Folder” of disk “Mac HD”
are essentially the same thing, only the Finder understands the later. The next thing to look at is what you then do with the list:
repeat with a in fileList
tell application "Microsoft Excel"
Activate
Open a
Activate ChartObject "Chart 1" of ActiveSheet
This part may cause trouble with the contents of the list since it is repeatedly telling Excel things, when it could be easier on it if it was
tell application "Microsoft Excel"
repeat with a in fileList
Activate
Open a
Activate ChartObject "Chart 1" of ActiveSheet
That way Excel is repeating thru the list of aliases opening them instead of the script repeating thru the list aliases telling Excel to open them.
Sorry to be long winded and taking so long to reply - I’ve never scripted Excel before so I’m taking all the chances I can think of.
If that doesn’t coerse Excel into opening the files, then you might want to tell the Finder to open them using Excel, and only script the actions on the ActiveSheet while leaving out the ‘open’ command.
ell application "Finder"
try
set fileList to every file of sourceFolder as alias list
on error
set fileList to every file of sourceFolder as alias as list
end try
repeat with a in fileList
open a using application "Calvin:Desktop Folder:Excel"
my processExcelCommands()
end repeat
end tell
on processExcelCommands()
tell application "Excel"
activate ChartObject "Chart 1" of ActiveSheet
--and the rest of the Excel commands
end tell
end processExcelCommands
the handler ‘processExcelCommands()’ is run as a subroutine, activated by ‘my processExcelCommands()’ because the Finder doesn’t understand custom handlers (“my” is refering to the script application, which does understand the handler). If the script starts to open the second alias in the list before the Excel handler has completed you can put “return true” at the end of the Excel script, forcing the main script to wait for a response from the handler before continuing (tho, it should wait for it to finish anyway).
Just taking stabs at it. Sorry I don’t even have a copy of Excel for Mac to test things out myself. A lot of previous posts about scripting Word and Excel usually lead to learning VisualBasic over AppleScript, so I suspect the AS implementation in Excel is a bit leaky.