Can I post data to a XML file (Twitter’s updating XML, it works on the command line) in AppleScript Objective-C and not having to use “do shell script?”
facepalm
I think I’m a person not fitted to do this stuff, simply because I can’t find what I’m looking for. Stefan, it’s probably right in what you’re showing me, but I don’t see anything about posting. I see downloading information.
I don’t “speak” AppleSriptObjC, in ObjC you can post data with this URL request sample code.
Of course you must adjust the HTTPHeaderField parameter
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSString *post = @"post this string":
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody];
That really helps. I couldn’t find anything about that in Google or that documentation or on MacScripter. I’ll convert it when I have time. That saves time~ lots of it.
Errors… and the first one I can’t understand.
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, class "NSURLRequestUseProtocolCachePolicy", 60.0)
tell current application's class "NSData" to set postdata to thetweet's dataUsingEncodeing_allowLossyConversion_(class "NSASCIIStringEncodeing", 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_
I’ve been working on it for a while; days; and nothing.
When that error says argument 3, it means argument 2 – I suspect someone got confused with zero-based counting. So it probably means that:
class “NSURLRequestUseProtocolCachePolicy”
should be:
curent application’s NSURLRequestUseProtocolCachePolicy
It’s an enumeration, not a class.
Constants like “NSURLRequestUseProtocolCachePolicy” don’t work in ASOC (at least they don’t for me). Try replacing “class “NSURLRequestUseProtocolCachePolicy”” with 0.
Also, I couldn’t find “dataUsingEncodeing_allowLossyConversion” in the NSData reference – it is in NSString ( also there is no “e” after the “d” in Encoding), and once again replace the constant “class “NSASCIIStringEncodeing”” with 1.
Ric
They should if you remove the quotes and precede them by "current application’s ". It makes for longer code, but it also makes it more readable later on.
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.