Waiting for active document

Hi. I’m a complete newbie at Applescript, as a warning.

What I’m trying to do is to take advantage of the fact that tables in Numbers are directly portable to Pages. So I have a directory of simple spreadsheets, which I want to cycle through, copy the first table and then paste into a pages doc (then a page break, rinse and repeat.

I’ve managed to get the table out to the clipboard and then to pages via UI scripting:

	tell document 1
		set selection range of first table of first sheet to cell range of first table of first sheet
	end tell

Then a copy in Numbers, then a paste in Pages.

But I can’t work out how to open a document from a filename in Numbers, and then send it a message. If I “open” a document, it doesn’t open in time to be sent a message by the script (or the script doesn’t wait for the document to open). I’m trying to work out how to do something like:

– set ref to reference returned by opening of document
– then send message to document (ie what can I use instead of document 1 above and believe it will work, knowing the file does exist and is openable)
– do the copy
– close the file without quitting numbers

None of this is self evident from anything I can find, including my copy of Rosenthal’s Applescript book.

Any ideas?

Perhaps something like this

tell application "Finder"
	set mydoc to open "Macintosh HD:Users:merickson:Desktop:Tester.numbers:"
	repeat until frontmost of application "Numbers"
	end repeat
end tell

set PagesDoc to "Macintosh HD:Users:yvan_koenig:Desktop:Restez-y.pages:"

set NumbersDoc to "Macintosh HD:Users:yvan_koenig:Desktop:Notre père qui êtes aux cieux.numbers:"
set sName to "Feuille 2"
set tName to "Tableau 1"
set targetCell to "B5"

my activateGUIscripting()

tell application "Numbers"
	set docName to name of document 1
	tell document docName
		set selection range of first table of first sheet to cell range of first table of first sheet
	end tell
	my shortcut("Numbers", "c", "c")
end tell

tell application "Pages"
	set nbDoc to count of documents
	open file PagesDoc
	repeat
		if (count of documents) > nbDoc then exit repeat
	end repeat
	set docName to name of document 1
	tell document docName
		--
	end tell
	my shortcut("Pages", "v", "c")
end tell

tell application "Numbers"
	set nbDoc to count of documents
	open file NumbersDoc
	repeat
		if (count of documents) > nbDoc then exit repeat
	end repeat
	set docName to name of document 1
	tell document docName to tell sheet sName to tell table tName
		set selection range to range targetCell
	end tell
	my shortcut("Numbers", "v", "c")
end tell

--=====

on activateGUIscripting()
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true (* to be sure than GUI scripting will be active *)
	end tell
end activateGUIscripting

--=====
(*
==== Uses GUIscripting ==== 
*)
on shortcut(a, t, d)
	local k
	tell application a to activate
	tell application "System Events" to tell application process a
		repeat with k in t
			if d = "c" then
				keystroke (k as text) using {command down}
			else if d contains "c" then
				if d contains "s" then
					if d contains "k" then
						keystroke (k as text) using {command down, shift down, control down}
					else
						keystroke (k as text) using {command down, shift down}
					end if
				else if d contains "k" then
					keystroke (k as text) using {command down, control down}
				end if
			end if
		end repeat
	end tell
end shortcut

--=====

Of course, edit the initial values to fit your needs.

Yvan KOENIG (from FRANCE vendredi 26 juin 2009 15:43:41)