OS X AppleScript breaks in OS9

Hi, (cross post from Mac OS X)

I’ve just finsished my first Applescript for a project that - in the end - will copy and set up some Word Templates from a CD. (right now I’m using the desktop as the source location) So, all’s fine in OS X 10.3 but it breaks of course in OS 9.2.1

It’s mandatory this works for 9.x through 10.3, But I’m hacking my way though this a s a newbie, so don’t know what’s backwards compatible and what’s not.

If anyone can point out what and how to modify the script for cross compatibility I would be eternally grateful.

When I run the script in 9 get “Finder got errors: Invalid keyform”
and the first duplicate command is highlighted through to the end of the path name.

(*  Creating Oncology Manuscripts Guidance Documents Installation script 
      (c) 2004 Lyle Turner 

 This script installs Microsoft Word Templates for use with the 
 Writing Oncology Manuscripts CD v2.0
 The Script performs the following:
   * copies folders from a CD to the users Documents folder
   * opens Word Templates within the folders and saves them to a MS Word default location
   * opens a Word document within a folder and saves it as oncolCDtemp.doc to the users' Documents folder to reset MS Word's default save location
   * delets oncolCDtemp.doc to the trash
   * Informs the user installation is finished and where the folders are installed
   * Opens the Users Documents Folder and selects the Phase I folder

*)

set docsPath to path to "docs" as string -- set users Documents path
set deskPath to path to desktop as string -- set the source path

set phaseI_Path to (docsPath & "Phase I:") -- set the Phase I folder path
set phaseII_Path to (docsPath & "Phase II:") -- set the Phase II folder path

tell application "Finder"
	-- Check if folders are already there.
	if ((folder phaseI_Path exists) or (folder phaseII_Path exists)) then
		-- tell user folders of same name are there. 
		display dialog "A folder named 'Phase I' or 'Phase II' already exists in your Documents folder. Please rename or move the folders and try again." buttons {"OK"} default button 1
	else
		-- copy the folders to the users Documents folder
		duplicate (deskPath & "Guidance Documents:Phase I") to docsPath
		duplicate (deskPath & "Guidance Documents:Phase II") to docsPath
		
		
		(* Set up guidance document templates by opening them in Word and saving them to Word's default location for templates. Then reset Words user default save location to "Documents"
		*)
		
		tell application "Microsoft Word"
			activate
			-- Set-up Phase I
			open file (docsPath & "Phase I:Guidance Document Help:Phase I Guidance Document.dot")
			do Visual Basic "     ActiveDocument.SaveAs FileName:="Phase I Guidance Document.dot", FileFormat:=wdFormatTemplate"
			close document 1 of window 1
			-- Set-up Phase II
			open file (docsPath & "Phase II:Guidance Document Help:Phase II Guidance Document.dot")
			do Visual Basic "     ActiveDocument.SaveAs FileName:="Phase II Guidance Document.dot", FileFormat:=wdFormatTemplate"
			close document 1 of window 1
			
			-- create and save a temp file in users Documents folder
			do Visual Basic "	Documents.Add DocumentType:=wdNewBlankDocument"
			save window 1 in (docsPath & "oncolCDtemp.doc")
			close window 1
		end tell
		
		-- Clean up temp file and inform the user it's all done and where the folders are installed
		tell application "Finder"
			activate
			-- delete the  temp file file
			delete file (docsPath & "oncolCDtemp.doc")
			display dialog "Your Guidance Documents have been installed in your Documents folder." buttons {"OK"} default button 1
			make new Finder window to docsPath
		end tell
	end if
end tell

I don’t know anything about scripting Microsoft Word, but for the Finder scripting, you need to change these lines:

-- Change:
duplicate (deskPath & "Guidance Documents:Phase I") to docsPath
-- to:
dulplicate folder (deskPath & "Guidance Documents:Phase I") to folder docsPath

-- Change:
duplicate (deskPath & "Guidance Documents:Phase II") to docsPath
-- to:
duplicate folder (deskPath & "Guidance Documents:Phase II") to folder docsPath

-- Change:
make new Finder window to docsPath
-- to:
open folder docsPath

These changes should work with the OS X Finder too.

Since the whole of your ‘if’ block is within a Finder ‘tell’ block, you shouldn’t need another Finder ‘tell’ block inside it as well. In fact, for neatness and (possibly) efficiency, you might consider blocking the code like this:

-- Initial string definitions here

tell application "Finder"
  -- Check if folders are already there.
  if ((folder phaseI_Path exists) or (folder phaseII_Path exists)) then
    -- tell user folders of same name are there.
    display dialog "A folder named 'Phase I' or 'Phase II' already exists in your Documents folder. Please rename or move the folders and try again." buttons {"OK"} default button 1
    -- ... and stop the script here
    error number -128
  end if
  
  -- copy the folders to the users Documents folder
  duplicate folder (deskPath & "Guidance Documents:Phase I") to folder docsPath
  duplicate folder (deskPath & "Guidance Documents:Phase II") to folder docsPath
end tell


tell application "Microsoft Word"

  -- blah blah blah

end tell


tell application "Finder"

  -- blah blah blah

end tell

There’s always something that slips through the proof-reading :frowning: It should be “duplicate” in that last line, of course.

Nigel,

Thanks! That did the trick, for all except … (knew there was a “gotcha” didn’t you? :slight_smile: )

The very last command of the script is supposed to open the users Documents folder in a window. I’d like to select or show the Phase I and II folder that had been duplicated, but not surehow to do that

The ‘Documents’ window opens in X, but throws an error -1,708 in 9.

In X, the window opens ‘scrolled’ to where the Phase I folder is visible, That must be a happy accident, because as far as I can tell I didn’t tell it to do that. (like I said Id like to select or highlight it, but that’s ok, user can see that it’s there. They can decide what to do next)

In 9, I looked at the script and noticed that the code had changed for that command in the OS 9 script editor:

it was:

make new Finder window to docsPath 

in 9 it shows:

make new <<class brow>> to docsPath

I tried adding ‘folder’ in ‘to docPath’ like you did for the duplicate command but no go. Suggestions?

Also a question, I know you said you don’t know Word scripting, but in the process of installing the templates (which contain macros) Word throws it’s obligatory macro warning. Is ther a way to throw a ‘click default’ command on a dialog window? (that woudl disble the macros, but I don’t care - I just want to make this set-up as automated as possible for the user)

Once I get this running smoothly in both I’ll look at reblocking as you suggest.
Thanks agian.

Lyle

You might use something like this to reveal/select the folders.

tell application "Finder"
	set phaseI to (duplicate folder (deskPath & "Guidance Documents:Phase I") to folder docsPath)
	set phaseII to (duplicate folder (deskPath & "Guidance Documents:Phase II") to folder docsPath)
	select {phaseI, phaseII}
end tell

– Rob

That’s why I suggested changing the line to:

open folder docsPath

Before Mac OS X (or, at least, before Jaguar):

  1. The Finder class ‘Finder window’ did not exist.
  2. It was not possible for the Finder to ‘make a new window’ of any kind. It could only ‘open’ them. In the case of a ‘container window’, it would be more usual to open the folder concerned.
  3. Folders had to be correctly specified as folders or aliases.
  4. There was only one ‘Documents’ folder, at the root level of the disk.

It’s probably habit. Every time you open a window on that folder, it should be scrolled to the same place as it was when you last closed it.

Instead of my ‘open folder docsPath’ suggestion, you could put:

tell application "Finder"
  reveal folder "Phase 1" of folder docsPath
end tell

This should open the documents folder window, with the “Phase 1” folder both visible and highlighted.

Opps, overlooked your original suggestion on the final bit in the script.

Thanks again Nigel,
All is playing nice in 9 and X, woohoo!

I modified the last reveal slightly to

tell application "Finder" 
  reveal folder phaseI_Path 
end tell

since I already had that set and all was well.
Well, except for excuciating bug fixing and testing on the Word aspects.

God, I hate Word. :x

Thanks to all for your support!

Lyle