Strange behavior with cURL

I thought this might be an issue of escaping the quotation marks, but now I’m not so sure. In my script, I have this line:

        set TwitterUpdate to "curl -u " & TwitterUser & ":" & TwitterPassword & " -d status=\"" & Tweet & "\" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url]

When I enter text with quotation marks, it strips out the quotation marks when sending it on to Twitter – this is not ideal.

What’s worse is that when I entered this Tweet:

The folks at MacScripters BBS have moved my script to the "Code Exchange": http://tinyurl.com/67d33a

it winds up posting this on Twitter:

The folks at MacScripters BBS have moved my script to the Code

with everything else truncated.

Using the quoted form of command here doesn’t fix this (and adds single quotes around the Tweet which is also not ideal.)

It seems like I need to escape the double quotes within the text string itself. I found this on the board

http://bbs.macscripter.net/viewtopic.php?pid=43036#p43036

so I wrote this



    set AppleScript's text item delimiters to ASCII character 34
        set theItemList to every text item in Tweet
        set FormattedTweet to every item in theItemList as string
        set AppleScript's text item delimiters to ""

But it didn’t work. Frankly, though, even after reading the MacScripter tutorial about text item delimiters, I’m not sure I totally get it, so I’m probably doing something wrong here.

Can anyone help?

Try something like this:

do shell script "/usr/bin/curl" & ¬
	" --user " & quoted form of (TwitterUser & ":" & TwitterPassword) & ¬
	" --data status=" & quoted form of Tweet & ¬
	" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url]

Hi,

I believe you also need to URL Encode the message text. I did not write the following which encodes the message.

set textToEncode to "The folks at MacScripters BBS have moved my script to the \"Code Exchange\": [url=http://tinyurl.com/67d33a]http://tinyurl.com/67d33a"[/url]
do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of textToEncode

Best wishes

John Maisey
www.nhoj.co.uk

The data isn’t being used in a URL.

Hi,

I believe it still needs URL Encoding. I have quoted below the information for the ‘data’ (-d) option in the cURL man pages. This clearly states that The data is expected to be “url-encoded”.

Best wishes

John Maisey
www.nhoj.co.uk

Try something like this:

do shell script "/usr/bin/curl" & ¬
	" --user " & quoted form of (TwitterUser & ":" & TwitterPassword) & ¬
	" --data status=" & quoted form of encodeURL(Tweet) & ¬
	" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url]

on encodeURL(someURL)
	return do shell script "/usr/bin/python -c '" & ¬
		"from sys import argv; " & ¬
		"from urllib import quote; " & ¬
		"print quote(unicode(argv[1], \"utf8\"))' " & quoted form of someURL
end encodeURL

See also: http://bbs.macscripter.net/viewtopic.php?pid=41930#p41930

Boy, it was fun watching two very smart minds come to the right solution. Thank you both so much for your help. This is the AppleScript I had come up with based on the previous post

-- In order for all kinds of text to work in cUrl, the Tweet must be "URL-encoded"
		
		set FormattedTweet to do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of Tweet
		
		-- Then format the Tweet so we can send it out to Twitter using cURL
		
		set TwitterUpdate to "curl -u " & TwitterUser & ":" & TwitterPassword & " -d status=\"" & FormattedTweet & "\" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url]
		
		-- Now try sending the Tweet to Twitter
		
		try
			set TwitterResponse to do shell script TwitterUpdate

but I think Bruce’s is smarter since it ensures that the quoted form of the Tweet will be used in the shell command.

Thank you again both for your help! Exactly what I needed and educational too! :smiley:

Alex