Error 1 is gone… Error 2 is still there… After testing with it, I can’t get anything.
I have:
on receiveaction_(sender)
set thetweet to tweetfield's stringValue() as string
if (thetweet's length) ≤ 140 then
set sendpass to thepassword's stringValue()
set senduser to theusername's stringValue()
tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
tell current application's class "NSMutableURLRequest" to set therequest to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
tell current application's class "NSData" to set postdata to thetweet's dataUsingEncodeing_allowLossyConversion_(1, false)
tell current application's class "NSString" to set postLength to (postdata's length)'s stringWithFormat_("%d")
request's setHTTPMethod_("POST")
request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
request's setHTTPBody_(postdata)
end if
end receiveaction_
check the spelling of dataUsingEncoding_allowLossyConversion_
Thanks, but it didn’t help. I got it right this time, but it’s still a pain in my back.
Dylan,
“dataUsingEncoding_allowLossyConversion” is an instance method, not a class method, so try changing the line:
tell current application’s class “NSData” to set postdata to thetweet’s dataUsingEncoding_allowLossyConversion_(1, false) to
set postdata to thetweet’s dataUsingEncoding_allowLossyConversion_(1, false)
Ric
Tried… and nothing worked. I decided to do some research… and found out the variable “request” was not defined, either. :mad: OHZ NOZ!
I need help… I can’t understand this mumbo jumbo of errors and messes.
with
on receiveaction_(sender)
set thetweet to tweetfield's stringValue() as string
display dialog thetweet
if (thetweet's length) ≤ 140 then
set sendpass to thepassword's stringValue()
set senduser to theusername's stringValue()
log 1
tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
log 2
tell current application's class "NSMutableURLRequest" to set therequest to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
log 3
set postdata to thetweet's dataUsingEncoding_allowLossyConversion_(class "NSASCIIStringEncodeing", false)
log 4
try
tell current application's class "NSString" to set postLength to (postdata's length)'s stringWithFormat_("%d")
end try
log 5
request's setHTTPMethod_("POST")
log 6
request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
log 7
request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
log 8
request's setHTTPBody_(postdata)
log 9
end if
end receiveaction_
Oops. I just set the wrong variable. facepalm
OK… got this:
from
on receiveaction_(sender)
set thetweet to tweetfield's stringValue() as string
display dialog thetweet
if (thetweet's length) ≤ 140 then
set sendpass to thepassword's stringValue()
set senduser to theusername's stringValue()
log 1
tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
log 2
tell current application's class "NSMutableURLRequest" to set request to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
log 3
set postdata to thetweet's dataUsingEncoding_allowLossyConversion_(1, false)
log 4
tell current application's class "NSString" to set postLength to (postdata's length)'s stringWithFormat_("%d")
log 5
request's setHTTPMethod_("POST")
log 6
request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
log 7
request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
log 8
request's setHTTPBody_(postdata)
log 9
end if
end receiveaction_
You’ve coerced it to an AS string; you can’t then call a Cocoa method on it. Try moving the “as string” into your display dialog part.
Ah! Progress! Messed stuff around. Next, the variable postLength is not defined after “6” is logged.
on receiveaction_(sender)
set thetweet to tweetfield's stringValue()
display dialog thetweet as string
if ((thetweet as string)'s length) ≤ 140 then
set sendpass to thepassword's stringValue() as string
set senduser to theusername's stringValue() as string
log 1
tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
log 2
tell current application's class "NSMutableURLRequest" to set request to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
log 3
set postdata to thetweet's dataUsingEncoding_allowLossyConversion_(1, false)
log 4
tell current application's class "NSString" to set postLength to (postdata's length)'s stringWithFormat_("%d") -- what does this do
log 5
request's setHTTPMethod_("POST")
log 6
request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
log 7
request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
log 8
request's setHTTPBody_(postdata)
log 9
end if
end receiveaction_
Quick question: What does “NSString *postLength = [NSString stringWithFormat:@”%d", [postData length]];" do? Maybe there’s another way.
It creates a NSString object which contains the amount of bytes of the NSData object.
In AppleScript it would look like
set postLength to (length of postData) as text
Thanks~ but it can’t set the length of an object. It’s not the right data.
The AppleScript code is pseudo code. As the class «data .» is an abstract class,
it doesn’t respond to the length or count command.
You have to perform the task with ObjC methods.
I guess you have to declare postData as a NSData object with tell current application’s class “NSData”
I’ll look in the documentation.
YES! No errors, but I must be missing something because it didn’t post anything. Is my method of authorization (ex “http://dylanweber:password@twitter.com/statues/update.xml”) right?
on receiveaction_(sender)
set thetweet to tweetfield's stringValue()
log thetweet as string
if ((thetweet as string)'s length) ≤ 140 then
set sendpass to thepassword's stringValue()
set senduser to theusername's stringValue()
log 1
tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
log 2
tell current application's class "NSMutableURLRequest" to set request to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
log 3
set postdata to thetweet's dataUsingEncoding_allowLossyConversion_(1, false)
log 4
tell current application's class "NSData" to set postLength to postdata's |length|() as string
log 5
request's setHTTPMethod_("POST")
log 6
request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
log 7
request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
log 8
request's setHTTPBody_(postdata)
log 9
end if
end receiveaction_
as mentioned a few times, my knowledge of ASOC syntax is almost nil, but try this
on receiveaction_(sender)
set thetweet to tweetfield's stringValue() as string
display dialog thetweet
if (thetweet's length) ≤ 140 then
set sendpass to thepassword's stringValue()
set senduser to theusername's stringValue()
log 1
tell current application's class "NSURL" to set theurl to its URLWithString_("http://" & senduser & ":" & sendpass & "@twitter.com/statuses/update.xml")
log 2
tell current application's class "NSMutableURLRequest" to set request to its requestWithURL_cachePolicy_timeoutInterval_(theurl, 0, 60.0)
log 3
tell current application's class "NSData" to set postdata to thetweet's dataUsingEncoding_allowLossyConversion_(1, false)
log 4
tell current application's class "NSString" to set postLength to NSString's stringWithFormat_("%d", postdata's length)
log 5
request's setHTTPMethod_("POST")
log 6
request's setValue_forHTTPHeaderField_(postLength, "Content-Length")
log 7
request's setValue_forHTTPHeaderField_("application/x-www-form-urlencoded", "Content-Type")
log 8
request's setHTTPBody_(postdata)
log 9
end if
end receiveaction_
Stefan, I think that brought back all of the old errors.
I’ll try to see what I did. Is there a class method that adds authorization to the variable request?
On the twitter site http://apiwiki.twitter.com/, it says:
IMPORTANT: Basic authentication removal is going to occur on August 16, 2010
This means you need to switch to OAuth for the authorization.
Curl will stop working on Aug 16.
Why reinventing the wheel anyway? Twitter has got an API for almost every language.
There are two for Cocoa: http://dev.twitter.com/pages/libraries#objectivec
Craig, you just saved me time… instead of wasting it redoing it.
Stefan, I’ve seen those before, I just wanted to make a few lines of code instead of adding 250 class files that I only need 1 or 2 for.
I’ll see what’s to trim off and what’s to love about those.
I added MGTwitterEngine by Matt Gemmell and tweaked a few things. What should I do next?
Project 2.3 MB