Excel and applescript

I have tons of files that I open all the time, run a macro on, then print to a PDF. I put a script together that will open the files, and then save them after the macro is run, then i can just print them all.
Problem is that the script is not running, it gives me an error saying that Excel isn’t liking the open command. here is the code:

set sourceFolder to choose folder with prompt "Source folder?" tell application "Finder" to ¬
set fileList to every file of folder sourceFolder as list
repeat with a in fileList
tell application "Microsoft Excel"
Activate
Open a
Activate ChartObject "Chart 1" of ActiveSheet
Evaluate "TOCUtils()"
Save ActiveWorkbook
Close ActiveWorkbook
end tell
end repeat

Any ideas?
Thanks! Kevin

Not really sure but you might try putting a line before the “Open a” similar to this:

display dialog "a is currently set to " & a

Run the script and this dialog should show you the value of a. The value returned may be a clue as to why Excel won’t open it.

Try ‘Open (contents of a)’
Also might want to check that the list generated by the Finder’s ‘every file of folder…’ is a list of aliases, and not a list of Finder file references.
I had a similar problem just surface when scripting Internet Explorer. I used a script to read the source of a page loaded in an IE window, and wanted to display a dialog of how many links it found. It kept giving an error when I tried to combine a number (variable x) with a string of text, ie; ‘set theMessage to x & " links found." as string’. IE couldn’t coerse a list {35, " links found."} into a string, so I had to make a handler do the coersion outside of the IE tell statements. Might be something similar happening in Excel, having trouble coersing data types.

I tried this. I get a window that says that a is currently set to the file I selected, but then when excel tries to open the file i get this message :

K

I tried adding the open contents of a thing… I get the same message. how do I set up the handler to coerce the files outside of excel?

I tried both ways. I get an error from saying the finder cannot find the file in the folder.

This is quite frustrating to say the least :slight_smile:

Kevin

ACK! My bad (maybe). The line is missing a specifier should the variable for sourceFolder not be properly known as a folder

‘set fileList to every file of folder sourceFolder as alias list’

instead of

‘set fileList to every file of sourceFolder as alias list’

Which may be leading to that error. If it still errors, it means there are no files in that folder, perhaps.

:Just had another thought: the ‘every file in folder…’ method of the Finder will also include invisible files like custom icons and wotnot (unless you specify ‘every file in folder sourceFolder whose type is…’), and if those are passed to Excel and it attempts to open them…NOT!

The ‘every file in’ method not only requires the Finder, it is a bit wild to tame and has a reputation of breaking down. The best solution for getting a list of files in a folder is to use ‘list folder’ from Standard Additions. It doesn’t require the Finder tell statements, but only returns a list of file names which then must be recombined with the folder path to make a full file path.
set sourceFolder to (choose folder) as string --“as string” is important when recombining with file names

set  fileNameList  to  (list folder sourceFolder  without  invisibles)

 set  fileList  to  {} --start an empty list

 repeat with  fileName  in  fileNameList
     set  filePath  to  sourceFolder & fileName  as  string
     copy  filePath  to end of  fileList --fill the list with file paths
 end repeat 

Only catch is now the list is not a list of aliases, it is a list of strings. So anywhere you use an item from the list, prefix the file path string with either ‘alias’ or ‘file’ so that it’s understood as an alias/file and not a string of text.

tell application "Excel"
repeat with a in fileList
open alias a (or 'open file a')

how i can learn abour scripting in acrobat?

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.

There are some posts in this bbs regarding Acrobat scripting, the most recent being
http://bbs.applescript.net/cgi-bin/webbbs_config.pl?read=9727

And more can be found in the Read-Only Archives (search “Acrobat”)
http://bbs.applescript.net/cgi-bin/archived_config.pl

Other sources may include info on Adobe’s website, but right now the whole adobe domain is slow as all get out (because the latest Photoshop has hit the street, I imagine).
http://www.adobeforums.com/cgi-bin/webx?14@@.ee6b2ed

but I’ve only seen less than stellar AppleScript enthusiasm out of Adobe.
The way I learn scripting (any application) is the “trial by fire” or “beat on it until it works or breaks” method. Usually by studying an application’s scripting dictionary I can intuit how things are done. I’ve a limited experience with Acrobat Distiller 3.0, which is automated best by exploiting the Hot/Watch Folder feature and avoiding attempts to script the application itself.

From what I can tell Acrobat isn’t too scriptable, but there are folks trying to implement better scripting support
http://www.pdfsages.com