Re: basic scripting with Excel
Re: basic scripting with Excel
- Subject: Re: basic scripting with Excel
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 03 Apr 2007 12:24:46 -0700
- Thread-topic: basic scripting with Excel
On 4/3/07 11:24 AM,
"applescript-users-bounces+berkowit=email@hidden"
<applescript-users-bounces+berkowit=email@hidden> wrote:
> If you are using Office 2004, you should use the "open workbook" command.
>> From the Excel 2004 Applescript Reference:
>
> Opening a workbook
> You can open a workbook by using the open workbook command. The following
> procedure opens a workbook named MyBook.xls.
>
> open workbook workbook file name "Macintosh HD:Users:Shared:MyBook.xls"
You haven't looked carefully at Mike's full script. His 'on open
these_items' droplet handler feeds a series of this_item variables to the
'on process_item(this_item)' handler. As with any 'on open' droplet handler
the automatic parameter ('these_items') is a list of aliases.
Excel 2004's 'open workbook' command does not take aliases as direct or
indirect objects. It takes a (text) file path as indirect object to the
'workbook file name' parameter, as you quote above. So if Mike were merely
to use that command, as you suggest, he'll get an error. To use 'open
workbook' command, he would first have to coerce the alias this_item to its
textual file path, which is easy enough:
on process_item(this_item)
tell application "Microsoft Excel"
activate
open workbook workbook file name (this_item as Unicode text)
end tell
end process_item
In this case Mike says he wants to open a text file (he refers to it as a
".txt" - it needs to be tab- or comma- or otherwise- delimited, as I'm sure
it is), not a workbook (an ".xls"). 'open workbook' will work fine on a .txt
file, however, if it's a standard delimited file (tab text).
If there's anything more complicated, he should really use the 'open text
file' command, which appears in the dictionary just above 'open workbook'.
But here the delimiter (tab or comma or semicolon or whatever) needs to be
specified with the appropriate parameter:
tell application "Microsoft Excel"
activate
open text file filename (this_item as Unicode text) with tab
end tell
However, the generic 'open' command from the Standard Suite would still work
(albeit less usefully, exactly as your quote from the Reference says, as it
does not return any object reference) as long as he doesn't stick in that
strange 'item' reference. His original version was:
> tell application "Microsoft Excel"
>
> Activate
>
> Open item this_item
>
That looks as if it was really meant to work in the Finder (which it should,
by the way), but perhaps Excel X and earlier allowed the same terminology
somehow (I've forgotten). For Excel 2004
tell application "Microsoft Excel"
open this_item
end tell
should work fine, as long as there's nothing else you want to do with the
opened file. this_item is in fact an alias, and Excel 2004's 'open' command
works on aliases (or alternately on just the file path, which is coerced to
the alias). So there's no need for any coercion to Unicode text here. Again,
'open' understands the standard delimiters, such as tab, it will not bring
up a dialog asking you to choose one. It just works. Since the script he had
for the earlier version of Excel was not trying to return a result, this
version will also work fine in Excel 2004 for him. It's the simplest
solution in this case. Just remove that 'item' word which is causing the
error.
However, in principle, it does indeed make good sense to get the 2004
AppleScript Reference and learn all the new scripting, including 'open
workbook'. The fact that you can now get a result from that command, makes
scripting other than simply opening a file much much more viable than in
earlier Excel versions.
--
Paul Berkowitz
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden