Using XML fetched from Twitter

Hi. I’ve been searching for awhile, and can’t quite figure this out. I’ve figured out how to post to twitter, but now I’m trying to figure out how to get info from Twitter’s API, and then post to twitter based on the results (the goal is to send a follow command for each of the people following). I’ve gotten to the point where I can do steps 1,2, 4, and 5, but I’m having problems with step 3.

Here’s how I get the XML-

set TwitterUpdate to "/usr/bin/curl" & ¬
	" --user " & quoted form of (theUname & ":" & thePswd) & ¬
	" [url=http://twitter.com/statuses/followers.xml]http://twitter.com/statuses/followers.xml"[/url]
tell application "System Events"
	set TwitterResponse to do shell script TwitterUpdate as string
end tell

I think my problem is this- when I get the XML, it returns bad XML, caused by a \ escaping an apostrophe. So I get XML that looks like this:

When I save the file as XMLreturned.xml and use this AppleScript:

set TwitterResponse to "/Users/jamesweber/Desktop/TwitterFollow/XMLreturned.xml" as Unicode text



tell application "System Events"
	tell contents of XML file TwitterResponse
		set theFollowersCount to count of XML elements of XML element "users"
		repeat with i from 1 to theFollowersCount
			set theName to value of XML element 3 of XML element i of XML element "users"
		end repeat
	end tell
end tell
return theName

I get the error that it can’t get XML element users.

I can do a find and replace in another program so it looks like this:

Then I can run the same AppleScript and it will return the names. (I’ll be calling a subroutine from the loop)

Any ideas how I can make the XML readable in AppleScript?

Is that in the text of the actual file or just in Script Editor?

Nitpick: You don’t need System Events to use do shell script, and the string coercion is pointless.

The actual XML file (when viewed on the web at http://twitter.com/statuses/followers.xml) does not have the " in it. That seems to be introduced in Script Editor.

Thanks- I’ve been just trying anything I can find, so my code is not the cleanest right now. I’ll be cleaning this up more.

If it’s not in the file, then it’s fine; Script Editor is just showing the escaped strings that it is using.

Okay. One potential problem ruled out.
Why, then, am I getting a “Can’t make "users" into type integer.” error?

Thanks a lot for your help, by the way.

This should get follower ids (though each request might be limited to 100 followers):

set theUsername to ""
set thePassword to ""

-- Create a temporary file
do shell script "/usr/bin/mktemp -t twitter-followers"
set outputPath to result

do shell script "/usr/bin/curl --silent --show-error" & ¬
	" --user " & quoted form of (theUsername & ":" & thePassword) & ¬
	" http://twitter.com/statuses/followers.xml > " & quoted form of outputPath

tell application "System Events"
	tell first XML element of XML file outputPath
		if its name is not "users" then error "Unexpected data." number 1
		
		set idList to {}
		repeat with thisElement in XML elements
			set end of idList to value of XML element "id" of thisElement
		end repeat
	end tell
end tell

return idList

Bruce-
That works.
Thanks a lot! I’ll post my final script here once I’m done.

Here’s what I have-


set theUname to ""
repeat
	display dialog "Twitter name:" default answer theUname
	copy the result as list to {theUname, the button_pressed}
	if theUname is not "" then exit repeat
end repeat
set thePswd to ""
repeat
	display dialog "Twitter password:" default answer thePswd
	copy the result as list to {thePswd, the button_pressed}
	if thePswd is not "" then exit repeat
end repeat

-- Create a temporary file
do shell script "/usr/bin/mktemp -t twitter-followers"
set outputPath to result

do shell script "/usr/bin/curl --silent --show-error" & ¬
	" --user " & quoted form of (theUname & ":" & thePswd) & ¬
	" http://twitter.com/statuses/followers.xml > " & quoted form of outputPath

tell application "System Events"
	tell first XML element of XML file outputPath
		if its name is not "users" then error "Unexpected data." number 1
		
		set idList to {}
		repeat with thisElement in XML elements
			set end of idList to value of XML element "screen_name" of thisElement
		end repeat
	end tell
end tell

repeat with twittername in idList
	set Tweet to "follow " & twittername
	do shell script "/usr/bin/curl" & ¬
		" --user " & quoted form of (theUname & ":" & thePswd) & ¬
		" --data status=" & quoted form of Tweet & ¬
		" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url]
end repeat

Note- this will send follow commands for all the followers. It currently doesn’t check if you’re following on not.

I’ve updated the script-

set theUname to ""
repeat
	display dialog "Twitter name:" default answer theUname
	copy the result as list to {theUname, the button_pressed}
	if theUname is not "" then exit repeat
end repeat
set thePswd to ""
repeat
	display dialog "Twitter password:" default answer thePswd
	copy the result as list to {thePswd, the button_pressed}
	if thePswd is not "" then exit repeat
end repeat

-- Create a temporary file
do shell script "/usr/bin/mktemp -t twitter-followers"
set outputPath to result

set idList to {}
set pageNumber to 1
repeat
	do shell script "/usr/bin/curl --silent --show-error" & ¬
		" --user " & quoted form of (theUname & ":" & thePswd) & ¬
		" [url=http://twitter.com/statuses/followers.xml?page=]http://twitter.com/statuses/followers.xml?page="[/url] & pageNumber & "> " & quoted form of outputPath
	
	tell application "System Events"
		tell first XML element of XML file outputPath
			if the (count of XML elements) is 0 then exit repeat
			if its name is not "users" then error "Unexpected data." number 1
			
			
			repeat with thisElement in XML elements
				set end of idList to value of XML element "screen_name" of thisElement
			end repeat
		end tell
	end tell
	
	set pageNumber to pageNumber + 1
end repeat

-- Create a temporary file
do shell script "/usr/bin/mktemp -t twitter-friends"
set outputPath to result

set friendList to {}
set pageNumber to 1
repeat
	do shell script "/usr/bin/curl --silent --show-error" & ¬
		" --user " & quoted form of (theUname & ":" & thePswd) & ¬
		" [url=http://twitter.com/statuses/friends.xml?page=]http://twitter.com/statuses/friends.xml?page="[/url] & pageNumber & "> " & quoted form of outputPath
	
	tell application "System Events"
		tell first XML element of XML file outputPath
			if the (count of XML elements) is 0 then exit repeat
			if its name is not "users" then error "Unexpected data." number 1
			
			
			repeat with thisElement in XML elements
				set end of friendList to value of XML element "screen_name" of thisElement
			end repeat
		end tell
	end tell
	
	set pageNumber to pageNumber + 1
end repeat

set listtoFollow to {}
repeat with x from 1 to count of items of idList
	set n to item x of idList
	if n is not in friendList and n is not in listtoFollow then set end of listtoFollow to n
end repeat

repeat with twittername in listtoFollow
	set Tweet to "follow " & twittername
	do shell script "/usr/bin/curl" & ¬
		" --user " & quoted form of (theUname & ":" & thePswd) & ¬
		" --data status=" & quoted form of Tweet & ¬
		" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url]
end repeat