current application statement

Hi All,

So I did build this nice script in AppleScript, but now I want to build an interface.
So I moved to XCode 3.2.6. I had to change some file handling using the ‘current application’ statement.
And it worked in most instances. But can’t get it to work here:

original Applescript code:

set theXmlFile to (choose file with prompt "Choose the XML file:" of type {"XML"}) as string
tell application "System Events"
	set theXmlData to XML file theXmlFile
end tell

but after moving to XCode I tried hundred’s of versions of the ‘current application’ statement, but can’t get this thing to work in System Events.

I started with trying:

		tell current application to set theXmlFile to (choose file with prompt "Choose the XML file:" of type {"XML"})
		tell application "System Events"
			set theXmlData to XML file theXmlFile
		end tell

but got the error System Events got an error: Can’t make alias “(file path)” into type integer. (error -1700)

I tried :

tell current application to set theXmlFile to ((choose file with prompt "Choose the XML file:" of type {"XML"}))
		set theXmlFileInt to (open for access current application's file theXmlFile)
		tell application "System Events"
			set my theXmlData to XML file (theXmlFile)
		end tell

But get the error Can’t make file [alias [pathtofile]] into type «class fsrf». (error -1700)

And when I try:

tell current application to set theXmlFile to (choose file with prompt "Choose the XML file:" of type {"XML"})
		tell application "System Events"
			tell current application to set aFileRef to open for access theXmlFile			
			set theXmlData to contents of XML file aFileRef
			tell current application to close access aFileRef
		end tell

The error is a wrong index… (although it is an integer now… but apparently not the integer it was looking for?)

Tried several other options, to no avail.

Obviously the goal is to get XML data from the theXMLData variable. Something like this:

		tell application "System Events"
			tell theXmlData
				tell XML element "name"
					set thisList to the value of (every XML element)
				end tell
			end tell
		end tell

As long as I stay in AppleScript Editor, this all works fine.
The problem came when I went over to Xcode.

Does anybody know how to solve this?
Of course I am aware that I have lots to learn about files, aliases, references, etc. I a, just confuesd. Maybe this example will help me understand better.
thanks a million.

rienq

Hi,

why not the original AppleScript code ?

The only difference to your second example apart from the current application statement is the as string coercion which is crucial.

Stefan !
Brilliant !

I tried so many methods (not in the order I posted them), that I completely overlooked this ‘as string’
That did it. This now works:

       tell current application to set theXmlFile to (choose file with prompt "Choose the XML file:" of type {"XML"}) as string
       tell application "System Events"
           set theXmlData to XML file theXmlFile
       end tell

How stupid of me.
Well, as explained, I find it hard to understand the file handling exactly, so I’m not really knowing what I am doing. By trying and trying, I slowly improve.

thank you
best
Rienk

PS: or you use the native Cocoa methods to parse XML documents


set theXmlFile to POSIX path of (choose file with prompt "Choose the XML file:" of type {"XML"})
set myDocument to current application's NSXMLDocument's alloc()'s initWithContentsOfURL_options_error_(current application's |NSURL|'s fileURLWithPath_(theXmlFile), 0, missing value)
set rootChildren to myDocument's rootElement()'s children() -- returns an array of all children nodes of the root node
log rootChildren's objectAtIndex_(0)'s stringValue()