Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 2, 2013, 01:40 PM   #1
moonman239
macrumors 65816
 
Join Date: Mar 2009
(AppleScript) "Tell"-ing every XML element to do something

I have multiple elements in an XML file. I need to "tell" each of them to do something.

Here's the code:
Code:
on tellEveryXMLElementWithName(elementContainer, elementName, codetorun)
	try
		tell application "System Events"
			set elements to XML elements of elementContainer
			repeat with I from 1 to count of elements
				set e_name to (get name of XML element I of elementContainer) as string
				if e_name is equal to elementName then
					log "The script got this far."
					run script codetorun
				end if
			end repeat
		end tell
		return "success"
	on error errMsg
		return errMsg
	end try
end tellEveryXMLElementWithName

set the_file to (choose file without invisibles) as string
tell application "System Events"
	set file1 to XML file the_file
	tell file1
		set thecode to "display dialog the /'hi'/"
		set successful to my tellEveryXMLElementWithName(file1, "tableViewCell", thecode)
	end tell
end tell
The dialog box never displays. I can set X to 0 and thecode to "set X to 5" and the value of X will remain 0.

You may be thinking, "Here's your answer. Switch to a different programming language." My response to that is "I don't want to."
__________________
The funny thing about this signature is that by the time you get to the end of it, you will have spent between five and ten seconds doing so.
moonman239 is offline   0 Reply With Quote
Old Jan 2, 2013, 06:59 PM   #2
moonman239
Thread Starter
macrumors 65816
 
Join Date: Mar 2009
I think I figured out the problem. System Events is looking in the first level. If the element is not in the first level, then as far as System Events is concerned, the element doesn't exist. Therefore, I need to look in a specific level.
__________________
The funny thing about this signature is that by the time you get to the end of it, you will have spent between five and ten seconds doing so.
moonman239 is offline   0 Reply With Quote
Old Jan 3, 2013, 04:06 PM   #3
moonman239
Thread Starter
macrumors 65816
 
Join Date: Mar 2009
OK, I have a new set of code:

Code:
on tellEveryXMLElementWithName(elementContainer, elementName, codetorun)
	try
		set allElements to {}
		tell application "System Events"
			set elements to every XML element of elementContainer
			set elementCount to count of elements
			repeat with I from 1 to elementCount
				set theElement to XML element I of elementContainer
				set e_name to (get name of theElement) as string
				if e_name is equal to elementName then
					set occurrences to occurrences + 1
					log "The script got this far."
					tell theElement
						run script codetorun
					end tell
				end if
			end repeat
		end tell
		return "success"
	on error errMsg
		return errMsg
	end try
end tellEveryXMLElementWithName

set the_file to (choose file without invisibles) as string
tell application "System Events"
	set file1 to XML file the_file
	tell file1
		my tellEveryXMLElementWithName(file1, "tableViewCell", "display dialog hi")
	end tell
end tell
I just need to make some changes to this code so that it searches everywhere for elements with a given name.
__________________
The funny thing about this signature is that by the time you get to the end of it, you will have spent between five and ten seconds doing so.
moonman239 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Applescript to start iTunes radio "station"? Honza1 Mac Programming 4 Aug 28, 2014 10:04 AM
Problem with "Messages" AppleScript Events jasonash OS X Mavericks (10.9) 2 Mar 20, 2014 06:23 PM
Applescript "Variable hdg is not defined" Dreamspinner Mac Programming 3 Feb 12, 2013 06:33 PM
AppleScript: Set variable to every XML element of a given name moonman239 Mac Programming 13 Jan 2, 2013 02:55 PM
iCal "Run Script" alert opens .scpt file in AppleScript Editor instead of running it drskot Mac Programming 3 Jun 7, 2012 12:47 PM

Forum Jump

All times are GMT -5. The time now is 03:49 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC