Sunday, January 21, 2018

#1 2010-06-20 03:10:47 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Posting with cURL withOUT the command line?

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?"


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#2 2010-06-20 03:15:17 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Posting with cURL withOUT the command line?

Hi,

take a look at NSURLConnection respective URL Loading System Programming Guide


regards

Stefan

Offline

 

#3 2010-06-20 04:38:10 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

*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. sad


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#4 2010-06-21 01:27:45 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Posting with cURL withOUT the command line?

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

[code]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:postData];[/code]


regards

Stefan

Offline

 

#5 2010-06-21 02:27:39 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

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.


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#6 2010-06-23 08:46:43 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

Errors... and the first one I can't understand. hmm

The Log wrote:

2010-06-23 21:43:42.763 iTwitter[1632:a0f] *** +[NSMutableURLRequest requestWithURL:cachePolicy:timeoutInterval:]: unable to set argument 3 because the AppleScript value <NSAppleEventDescriptor: 'obj '{ 'form':'name', 'want':'pcls', 'seld':'utxt'("NSURLRequestUseProtocolCachePolicy"), 'from':'obj '{ 'form':'name', 'want':'pcls', 'seld':'utxt'("NSMutableURLRequest"), 'from':'null'() } }> could not be coerced to type Q.
2010-06-23 21:43:42.861 iTwitter[1632:a0f] *** -[iTwitterAppDelegate receiveaction:]: The variable postdata is not defined. (error -2753)


I have:

Applescript:

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. sad

Last edited by Dylan Weber (2010-06-23 08:57:41 pm)


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#7 2010-06-23 10:23:32 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5231

Re: Posting with cURL withOUT the command line?

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#8 2010-06-23 10:40:29 pm

rdelmar
Member
From:: Sonoma County, CA
Registered: 2010-03-05
Posts: 1008

Re: Posting with cURL withOUT the command line?

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)


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

Offline

 

#9 2010-06-24 01:54:01 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5231

Re: Posting with cURL withOUT the command line?

rdelmar wrote:

Constants like  "NSURLRequestUseProtocolCachePolicy" don't work in ASOC (at least they don't for me).


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#10 2010-06-24 07:10:22 am

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

Error 1 is gone... big_smile Error 2 is still there... sad After testing with it, I can't get anything.

The Log wrote:

2010-06-24 07:58:03.457 iTwitter[2019:a0f] *** -[iTwitterAppDelegate receiveaction:]: The variable postdata is not defined. (error -2753)


I have:

Applescript:

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_


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#11 2010-06-24 07:20:44 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Posting with cURL withOUT the command line?

check the spelling of dataUsingEncoding_allowLossyConversion_


regards

Stefan

Offline

 

#12 2010-06-24 03:25:30 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

Thanks, but it didn't help. I got it right this time, but it's still a pain in my back.


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#13 2010-06-24 07:02:35 pm

rdelmar
Member
From:: Sonoma County, CA
Registered: 2010-03-05
Posts: 1008

Re: Posting with cURL withOUT the command line?

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

Offline

 

#14 2010-06-24 08:33:48 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

Tried... and nothing worked. hmm 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.

The Log wrote:

2010-06-24 21:30:39.912 iTwitter[3058:a0f] 1
2010-06-24 21:30:39.915 iTwitter[3058:a0f] 2
2010-06-24 21:30:39.916 iTwitter[3058:a0f] 3
2010-06-24 21:30:39.917 iTwitter[3058:a0f] 4
2010-06-24 21:30:39.925 iTwitter[3058:a0f] 5
2010-06-24 21:30:39.926 iTwitter[3058:a0f] *** -[iTwitterAppDelegate receiveaction:]: The variable request is not defined. (error -2753)


with

Applescript:

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_


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#15 2010-06-24 09:13:51 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

Oops. I just set the wrong variable. *facepalm*

OK... got this:

2010-06-24 22:13:14.412 iTwitter[3148:a0f] 1
2010-06-24 22:13:14.416 iTwitter[3148:a0f] 2
2010-06-24 22:13:14.416 iTwitter[3148:a0f] 3
2010-06-24 22:13:14.417 iTwitter[3148:a0f] 4
2010-06-24 22:13:14.426 iTwitter[3148:a0f] *** -[iTwitterAppDelegate receiveaction:]: The variable postdata is not defined. (error -2753)


from

Applescript:

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_

Last edited by Dylan Weber (2010-06-24 09:23:37 pm)


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#16 2010-06-24 09:43:32 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5231

Re: Posting with cURL withOUT the command line?

set thetweet to tweetfield's stringValue() as string


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#17 2010-06-25 06:23:41 am

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

Ah! Progress! Messed stuff around. Next, the variable postLength is not defined after "6" is logged. hmm

Applescript:

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_

Last edited by Dylan Weber (2010-06-25 06:24:25 am)


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#18 2010-06-25 08:47:32 am

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

Quick question: What does "NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];" do? Maybe there's another way.


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#19 2010-06-25 10:21:18 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Posting with cURL withOUT the command line?

Dylan Weber wrote:

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
[code]set postLength to (length of postData) as text[/code]

Last edited by StefanK (2010-06-25 10:22:06 am)


regards

Stefan

Offline

 

#20 2010-06-25 11:59:06 am

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

Thanks~ but it can't set the length of an object. It's not the right data.

2010-06-25 12:57:45.018 iTwitter[5344:a0f] *** -[iTwitterAppDelegate receiveaction:]: Can’t make length of «class ocid» id «data kptr00000000C083640002000000» into type text. (error -1700)


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#21 2010-06-25 12:11:16 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Posting with cURL withOUT the command line?

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"

Last edited by StefanK (2010-06-25 12:14:46 pm)


regards

Stefan

Offline

 

#22 2010-06-25 12:40:25 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

I'll look in the documentation.


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#23 2010-06-25 12:48:31 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

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?

Applescript:

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_

Last edited by Dylan Weber (2010-06-25 12:49:03 pm)


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#24 2010-06-25 01:03:05 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Posting with cURL withOUT the command line?

as mentioned a few times, my knowledge of ASOC syntax is almost nil, but try this

Applescript:


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_


regards

Stefan

Offline

 

#25 2010-06-25 01:09:17 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Posting with cURL withOUT the command line?

Stefan, I think that brought back all of the old errors. hmm
I'll try to see what I did. Is there a class method that adds authorization to the variable request?


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)