Getting and working with a document object in Quark, using Applescript

Firstly I’m new to Applescript so I’m sorry if I ask anything to obvious.

Some background about me:

I’m used to programming in Object Orientated languages like C Sharp, and Java, but no Dot notation seems to exist for Applescript.
I’m trying to write a script for use with Quark, this script will interrogate a Quark document and write values into an XML file.

The problem I’ve got is getting hold of the document object.
If a person has more than one file open then they need to specify which file we should work with. I’ve written code that finds the names of the open files, and displays a list. The user then selects a file from this list and presses OK. Using this text value I again loop through the document. When I find a match I then set a property. Then I call a function. It is at this function things start to go awry. At the moment the error message I get is: Can’t make name of document into type string

i think there are two questions: Am I getting hold of the document correctly, and is this possible?

set docCount to document count   -- find how many documents are open
set docList to {}   -- this list will hold the names of the open documents

if (docCount is greater than 1) then
	--Initialise our array/list
	repeat docCount times
		set end of docList to ""
	end repeat
	display dialog "You have " & docCount & " files open. Which one should we work with?"
	repeat with i from 1 to docCount  -- This repeat adds the names of the documents to our list
		tell document i
			set DocName to name as text
			set item i of docList to DocName
		end tell
	end repeat
       -- These two lines find which document name has been selected
	set chosenFile to (choose from list docList with prompt "Which item?" cancel button name "Cancel" without multiple selections allowed)
	set chosenFile to chosenFile as text
        --- this loop matches the chosen File name with our open documents
	repeat with i from 1 to docCount
		tell document i
			set DocName to name as text
			display dialog ("This is the file we've found " & DocName)
			if chosenFile = DocName then
				display dialog "The names match"
				set theDoc to document  -- set this variable to be the document. Is this assumption correct?
				exit repeat
			end if
		end tell
	end repeat
else if (docCount is 1) then
	display dialog "You have a file open"
	processChosenFile(document 1)
	display dialog "you have no files open"
end if

on processChosenFile(docQuark)
	display dialog "At least we reach the function"
	tell docQuark
		set DocName to name as text
		--set DocName to name of docQuark as string
		display dialog (DocName)
	end tell
end processChosenFile

OK, I just messed around with this for a whle and I think I tightened it up a bit. I also figured out something I didn’t know how to do before so that was a bonus…

property theDoc : ""

tell application "QuarkXPress"
	if not (exists document 1) then
			display dialog "No document open!" buttons "Cancel" default button "Cancel"
		on error
		end try
	end if
	if (document count) is 1 then
		set theDoc to name of front document as text
		set theDocChoice to choose from list (name of documents) as list
		set theDoc to item 1 of theDocChoice as text
	end if
	my processChosenFile()
end tell

on processChosenFile()
	tell application "QuarkXPress"
		tell document theDoc
                     --tell this document to do something
			display dialog (theDoc)
		end tell
	end tell
end processChosenFile

Model: Mac G5 OS 10.3.9
Browser: Safari 312.3.1
Operating System: Mac OS X (10.3.9)

Thank you very much for your response. I had found another way of doing it. Nothing fancy, merely capturing the document number then passing that through to the function rather than the name, or what I hoped was the reference.

on processChosenFileByNumber(num)

	display dialog "At least we reach the function"
	tell document num
		set DocName to name
		display dialog (DocName)
	end tell
end processChosenFileByNumber

However, your way seems a lot cleaner, and neater, so if you don’t mind I’ll use that.

Thanks again,

P.S: Forgive the dumbness of this question, but what does show do? I can guess but i don’t want to guess wrongly. Just so as you know I’m using Applescript with no reference books, and this has been the only online resource that has come up trumps.
P.P.S: i’m hoping the books I’ve ordered turn up tomorrow, now I’ve said this.:slight_smile:

No need to answer with regards to show, I’m guessing from what I’ve seen is that it brings the selected document to the front.

Thanks again for your help.

Funny you ask because “Show” was the new command that I discovered.

I noticed when I was testing that just saying “Tell document whatever” didn’t work if another window was active. Using “Show” in a “Tell Document” block is the same idea as using “Activate” in a “Tell Application” block. It makes that document the “Front Document” (probably Document 1 but I didn’t test this).

I was happy to learn this as it means you should be able to switch back and forth between documents you have open, which I had trouble with before.