Opening Word

When I open Microsoft Word with an AppleScript it takes you to the “Project Gallery” window. Does anyone know how to close that window with an AppleScript command?

Model: 17" PowerBook
Browser: Safari 417.8
Operating System: Mac OS X (10.4)

Try this:
In Word Preferences (available in the Word menu bar when a document is opened), go to General and uncheck Show Project Gallery at startup (I have Word in French; I guess this is it in English). The next time you will launch Word, it will open a new document (the Normal template). So I asked my script to close this window:


tell application “Microsoft Word”
launch
close window 1
end tell

That’s helpful but if I want the Project Gallery to be the default, is there a way to close it with an AppleScript command? Maybe the answer is no.

I tried that and here’s what I got in the event log:

tell application “Microsoft Word”
launch
current application
end tell
tell application “System Events”
set frontmost of process “Microsoft Word” to true
get every window of process “Microsoft Word”
{window “Project Gallery - New” of application process “Microsoft Word”, window 2 of application process “Microsoft Word”, window 3 of application process “Microsoft Word”}
ASCII character 27
“”
keystroke “”
end tell

It did not close the window. It opened Word and the Project Gallery - New window and then it just stayed there. I increased the delay to 12 but no difference. What is ASCII character 27? Is it a return or enter character? Maybe it should be something else. Interesting approach though.

It’s a bit of a tough nut because the window doesn’t understand the close command so I can’t just close the window.

I tried moving the “keystroke (ASCII character 27)” inside the repeat. Still did not close the window.

Hi tupertoller,

try adding this to make sure word is launched before continueing:

tell application “Microsoft Word” to launch
tell application “Finder”
repeat while not ((count (every process whose name is “Microsoft Word”)) as boolean)
delay 1
end repeat
end tell

tell application “System Events” to tell process “Microsoft Word”
set frontmost to true
repeat until windows is not {}
delay 1
end repeat
keystroke (ASCII character 27)
end tell

(for me it worked)

D.

I also knocked up a quick routine a little earlier, but didn’t post when I saw that Jacques had beaten me to it (again). It’s fairly similar:

tell application "Microsoft Word" to activate
tell application "System Events" to tell process "Microsoft Word"
	repeat until (count windows) > 0
		delay 0.2
	end repeat
	tell (first window whose title starts with "Project Gallery - ") to if exists then click button "Cancel"
end tell

Both Jacques and Kai’s methods work. Thank you both and yes I am using Office 2004. Regards, JJ

Glad to hear the suggestions work for you, JJ.

A couple of small points may be worth bearing in mind. If the gallery option chosen previously was not ‘New’, then the name/title of the gallery window could differ slightly. The “starts with” syntax should help to avoid potential problems here.

It’s also a good idea to check for the existence of a window before attempting to perform an action on it, just in case the script is ever run mid-session - or someone changes Word’s preferences so that the window never appears.

You might like to consider something like the following variation, which gives up checking for the window after a certain time (in this case, up to 5 seconds after launch):

launch application "Microsoft Word"
tell application "System Events" to tell process "Microsoft Word"
	set frontmost to true
	tell (first window whose title starts with "Project Gallery - ") to repeat 25 times
		if exists then
			click button "Cancel"
			exit repeat
		end if
		delay 0.2
	end repeat
end tell

Kai yes that’s better. While I’m on this, what is the default unit for the delay command. I seem to remember ticks somewhere along the appescript way, which were very brief. Is the unit of a delay statement seconds? JJ

Yes JJ, the delay command is expressed in seconds. So if the loop that I suggested repeats 25 times with a delay of 0.2 seconds on each iteration, the total checking time would be [25 * 0.2 =] 5 seconds. (That’s assuming, of course, that the window doesn’t materialise and cause the script to exit the repeat loop sooner.)

Ticks (as used, for example, in Jon’s Commands.osax) are units of one sixtieth of a second. :slight_smile: