Tearing my bloody hair out.


Shane Stanley
 

On 16 Sep 2017, at 9:05 pm, Brian Christmas <ozsanta@gmail.com> wrote:

I’ve Googled, found nothing.
Did you check MacScripter.net? Here's a thread that was active only this week: <http://macscripter.net/viewtopic.php?id=42971>. The topic: "Parsing JSON files". Read *it all*. The answer is in there, both as a one-liner and unfolded and commented by Nigel.

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


Brian Christmas
 

Sorry about all this noise!

I’ve just realized something, but I don’t know the answer to my understanding.

When I posted the below Script 2, I’d added 'as list’ to the end of….. 

set temp to {action:"subscription.getall", result:"success", page:1, limit:10000, nextPage:nulltotal:1, subscriptions:["f-DfdENtRvuhBtKJUIA8QQ"]} as list

THIS ACTUALLY FAILS. Which leads me to understand without the ‘as list', I’ve actually got an applescript Record in Script 2 (please correct me if I’m wrong) The Record WORKS! (bofere I added ‘as list’)

HOWEVER, in Script 1, the list is actually text, NOT A RECORD, and fails. Problem is, even after reading Shanes Explored, I can’t find a way of converting the text to a Record.

I’ve Googled, found nothing. 

How can I change the text to an Applescript Record? I HAVE to change the original to text to apply Delimiters, to get rid of the quotations. The information is there, just in the wrong format.

OR, is there an easier way?

Regards

Santa


 
Further to this matter

I had realized that the fact that the keys were in Quotes in jsonURL meant that it wasn’t really a Dictionary, but thought there might be some simple way to change it, so posted.

But, no fast answers, so after thinking, wrote the following Script 1

However, it’s not recognized still as a Dictionary, and I don’t know why. If I take the EXACT list, and use it in Script 2, it works fine.

There’s obviously something I’m missing, but I thought coercing temp to a list would fix any hassles.

BTW, I’ve tried stripping the square brackets as text item delimiters, but it fails.

Anybody able to set me straight, please?

Regards

Santa


SCRIPT 1

use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions


set jsonURL to do shell script "curl --verbose -A UserAgentString -u 2V57JR9HQNAZNZVOW6OZXG:my-password   https://api.fastspring.com/subscriptions?limit=10000"
set temp to jsonURL as text
set OLD_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "{\""
set temp to text items of temp
set AppleScript's text item delimiters to "{"
set temp to text items of temp as text
set AppleScript's text item delimiters to ",\""
set temp to text items of temp
set AppleScript's text item delimiters to ","
set temp to text items of temp as text
set AppleScript's text item delimiters to "\":"
set temp to text items of temp
set AppleScript's text item delimiters to ":"
set temp to text items of temp as text
set AppleScript's text item delimiters to OLD_delim
set temp to temp as list
display dialog temp --< Returns "{action:"subscription.getall",result:"success",page:1,limit:10000,nextPage:null,total:1,subscriptions:["f-DfdENtRvuhBtKJUIA8QQ"]}"
try
set numbersDict to current application's NSDictionary's dictionaryWithDictionary:temp
on error errmsg
display dialog errmsg --<  returns   "*** -[NSDictionary initWithDictionary:copyItems:]: dictionary argument is not an NSDictionary"
end try
try
set subscriptionsList to temp's subscriptions as list
end try
subscriptionsList
SCRIPT 2

use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions

set temp to {action:"subscription.getall", result:"success", page:1, limit:10000, nextPage:null, total:1, subscriptions:["f-DfdENtRvuhBtKJUIA8QQ"]} as list
try
set numbersDict to current application's NSDictionary's dictionaryWithDictionary:temp
on error errmsg
display dialog errmsg --< no error
end try
try
set subscriptionsList to numbersDict's subscriptions as list
on error errmsg
display dialog errmsg --<  returns   "*** -[NSDictionary initWithDictionary:copyItems:]: dictionary argument is not an NSDictionary"
end try
subscriptionsList --< WORKS! Returns "f-DfdENtRvuhBtKJUIA8QQ"


Brian Christmas
 

Further to this matter

I had realized that the fact that the keys were in Quotes in jsonURL meant that it wasn’t really a Dictionary, but thought there might be some simple way to change it, so posted.

But, no fast answers, so after thinking, wrote the following Script 1

However, it’s not recognized still as a Dictionary, and I don’t know why. If I take the EXACT list, and use it in Script 2, it works fine.

There’s obviously something I’m missing, but I thought coercing temp to a list would fix any hassles.

BTW, I’ve tried stripping the square brackets as text item delimiters, but it fails.

Anybody able to set me straight, please?

Regards

Santa


SCRIPT 1

use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions


set jsonURL to do shell script "curl --verbose -A UserAgentString -u 2V57JR9HQNAZNZVOW6OZXG:my-password   https://api.fastspring.com/subscriptions?limit=10000"
set temp to jsonURL as text
set OLD_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "{\""
set temp to text items of temp
set AppleScript's text item delimiters to "{"
set temp to text items of temp as text
set AppleScript's text item delimiters to ",\""
set temp to text items of temp
set AppleScript's text item delimiters to ","
set temp to text items of temp as text
set AppleScript's text item delimiters to "\":"
set temp to text items of temp
set AppleScript's text item delimiters to ":"
set temp to text items of temp as text
set AppleScript's text item delimiters to OLD_delim
set temp to temp as list
display dialog temp --< Returns "{action:"subscription.getall",result:"success",page:1,limit:10000,nextPage:null,total:1,subscriptions:["f-DfdENtRvuhBtKJUIA8QQ"]}"
try
set numbersDict to current application's NSDictionary's dictionaryWithDictionary:temp
on error errmsg
display dialog errmsg --<  returns   "*** -[NSDictionary initWithDictionary:copyItems:]: dictionary argument is not an NSDictionary"
end try
try
set subscriptionsList to temp's subscriptions as list
end try
subscriptionsList
SCRIPT 2

use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions

set temp to {action:"subscription.getall", result:"success", page:1, limit:10000, nextPage:null, total:1, subscriptions:["f-DfdENtRvuhBtKJUIA8QQ"]} as list
try
set numbersDict to current application's NSDictionary's dictionaryWithDictionary:temp
on error errmsg
display dialog errmsg --< no error
end try
try
set subscriptionsList to numbersDict's subscriptions as list
on error errmsg
display dialog errmsg --<  returns   "*** -[NSDictionary initWithDictionary:copyItems:]: dictionary argument is not an NSDictionary"
end try
subscriptionsList --< WORKS! Returns "f-DfdENtRvuhBtKJUIA8QQ"


Brian Christmas
 

G’day All

A very big thank you to everyone who replied.

Turned out that Jim Skibbie was spot on, (once I put the correct passcode in), possibly only because I tried his suggestion first, the other answers may have worked as well.

set jsonURL to do shell script "curl --verbose -A UserAgentString -u 2V57JR9HQNAZNZVOW6OZXG:my-password https://api.fastspring.com/subscriptions?limit=10000"
-- RETURNS {"action":"subscription.getall","result":"success","page":1,"limit":10000,"nextPage":null,"total":1,"subscriptions":["f-DfdENtRvuhBtKJUIA8QQ"]}

Why I did not need a -G perplexes me, FastSpring insists on a GET. And -A UserAgentString was something I had overlooked when reading the MAN pages for CURL.

You’re two great bunches of people!!!



HOWEVER, the code that Nigel had supplied me with earlier (below), fails to convert the “subscriptions’ key to an applescript list.

Here it is with errors. I’ve played around with it, but really don’t follow it.

Obviously URLWithString:(jsonURL must be incorrect, cause Nigel expected me to supply a URL, but what do replace it with, please?

I tried guessing (Yes Shane, I know, don’t bloody GUESS!),  set subscriptionsList to (current application's jsonURL's objectsForKey:(("subscriptions") as list)) —< jsonURL doesn’t understand the “objectForKey_” message.


How do I  get the list of subscriptions from jsonURL, please?

Again thank you, and regards

Santa

use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions


try
if (count of jsonURL) > 0 then
set p to 3
set jsonURL to current application's class "NSURL"'s URLWithString:(jsonURL)
set p to 4
set jsonData to current application's class "NSData"'s dataWithContentsOfURL:(jsonURL)
set p to 5
set {jsonObject, theError} to current application's class "NSJSONSerialization"'s JSONObjectWithData:(jsonData) options:(0) |error|:(reference) --< returns "data parameter is nil"
set p to 6
if (jsonObject is missing value) then error (theError's localizedDescription() as text)
set p to 7
set subscriptionsList to (jsonObject's objectForKey:("subscriptions")) as list
end if
on error errmsg
display dialog "Json conversion " & errmsg & " p = " & p --< gives "Json conversion The data couldn’t be read because it isn’t in the correct format. p = 6"
end try



On 16 Sep 2017, at 6:51 am, Jim Skibbie <JSkibbie@...> wrote:

I would suggest opening up Terminal and trying this from the command line
using curl’s --verbose flag until you can verify your request is working
correctly.

curl --verbose -A UserAgentString -u
2V57JR9HQNAZNZVOW6OZXG:F7l-9f3SR2We7q9L6mzds
https://api.fastspring.com/subscriptions?page=1&limit=1


When I do this, I get the below result. Note that the response is “401
Unauthorized”. I’m assuming you didn’t publish your real username and
password above.

Once you get it working, you can just wrap the command inside the do shell
script command.

*   Trying 54.88.60.171...
* Connected to api.fastspring.com (54.88.60.171) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.fastspring.com
* Server certificate: Amazon
* Server certificate: Amazon Root CA 1
* Server certificate: Starfield Services Root Certificate Authority - G2
* Server auth using Basic with user '2V57JR9HQNAZNZVOW6OZXG'
GET /subscriptions?page=1 HTTP/1.1
Host: api.fastspring.com
Authorization: Basic
MlY1N0pSOUhRTkFaTlpWT1c2T1pYRzpGN2wtOWYzU1IyV2U3cTlMNm16ZHM=
User-Agent: UserAgentString
Accept: */*

< HTTP/1.1 401 Unauthorized
< Date: Fri, 15 Sep 2017 20:45:42 GMT
< Server: nginx
< Content-Length: 0
< Connection: keep-alive




Date: Fri, 15 Sep 2017 15:51:58 +1000
From: Brian Christmas <ozsanta@...>
To: Applescript Xcode new list <applescript@apple-dev.groups.io>,
Applescript Users <applescript-users@...>
Subject: Re: Tearing my bloody hair out.
Message-ID: <4CA9A161-3D16-4C87-99B7-7CD8F743B38B@...>
Content-Type: text/plain; charset="utf-8"




G’day again scripters

I’ve been trying to study Curl for days, and now how to use Curl in a ‘do
shell script’.

I can’t for the life of me see why this won’t work? BUT, I’m really a
novice at using CURL, completely new to me.

Fast Springs site says I have to use GET. This handler returns “"

Santa

set jsonURL to do shell script "curl    -H  -u
'2V57JR9HQNAZNZVOW6OZXG:F7l-9f3SR2We7q9L6mzds'   -G
\"https://api.fastspring.com/subscriptions?limit=10000\”"


_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (applescript-users@...)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/applescript-users/ozsanta%40gmail.com
Archives: http://lists.apple.com/archives/applescript-users

This email sent to ozsanta@...


EAS EDV-Analyse Sandro Sabatini
 

Hi Brian,

when I type "man curl“ in the terminal on my Mac then it reports:

       -G, --get
              When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in
              an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to  the  URL
              with a '?' separator.

              If used in combination with -I, --head, the POST data will instead be appended to the URL with a HEAD request.

              If  this option is used several times, only the first one is used. This is because undoing a GET doesn't make sense,
              but you should then instead enforce the alternative method you prefer.
so the option for using GET is not -H but -G.

Further, there is the following option for the user agent:

       -A, --user-agent <name>
              (HTTP) Specify the User-Agent string to send to the HTTP server. To encode blanks in the string, surround the string
              with single quote marks. This can also be set with the -H, --header option of course.

              If this option is used several times, the last one will be used.

I never used that, just a hint to use the man pages of curl.

Saba


Am 15.09.2017 um 07:51 schrieb Brian Christmas <ozsanta@...>:




G’day again scripters

I’ve been trying to study Curl for days, and now how to use Curl in a ‘do shell script’.

I can’t for the life of me see why this won’t work? BUT, I’m really a novice at using CURL, completely new to me.

Fast Springs site says I have to use GET. This handler returns “"

Santa

set jsonURL to do shell script "curl    -H  -u '2V57JR9HQNAZNZVOW6OZXG:F7l-9f3SR2We7q9L6mzds'   -G \"https://api.fastspring.com/subscriptions?limit=10000\”"


I’m trying to work out the correct syntax to get a subscription list from FastSpring, who I’m trying to set up to manage my subscritions for my APP.

I’ve contacted FastSprings support numerous times, but the most they’ll do regarding formatting scripts is ‘You’re on the right track’, or, unofficially, tell me to try ‘Postman’.

I’ve entered my credential into Postman, and it tells me there’s one text subscription, but every iteration of my Shell script returns “”, using the same credentials. I know I have to have a ‘User-Agent' header, but support won’t advise me if I’ve got the terminology correct. And my Googling shows there’s lots of ways to format a ‘User-Agent’ header!!! Postman uses it’s own method of addressing the FastSprings site.

I’m at a dead end! I’m told I have to use a ‘GET’, but read that the -H is  the ‘GET’ equivilent.

Any advice from anybody, please. I’m just guessing, and Googling is frustratingly un-informative.

If necessary, is there another company that makes this easier? I’ve battled with FastSpring support for over a week now. They absolutely refuse to supply or post examples. I need a pop-up store on my website, and a storefront for cancelling subscriptions.

Regards

Santa



set jsonURL to do shell script ("curl ' -H User-Agent:2V57JR9HQNAZNZVOW6OZXG/my-password-as-used-in-Postmanhttps://api.fastspring.com/subscriptions/limit=10000")



--- 
Sandro Sabatini, Senior Software Architect
EAS EDV-Analyse Sandro Sabatini, Klosbachstrasse 22, CH-8032 Zurich
Fon: +41 44 450 40 50, Fax: +41 44 450 40 54




Brian Christmas
 




G’day again scripters

I’ve been trying to study Curl for days, and now how to use Curl in a ‘do shell script’.

I can’t for the life of me see why this won’t work? BUT, I’m really a novice at using CURL, completely new to me.

Fast Springs site says I have to use GET. This handler returns “"

Santa

set jsonURL to do shell script "curl    -H  -u '2V57JR9HQNAZNZVOW6OZXG:F7l-9f3SR2We7q9L6mzds'   -G \"https://api.fastspring.com/subscriptions?limit=10000\”"


I’m trying to work out the correct syntax to get a subscription list from FastSpring, who I’m trying to set up to manage my subscritions for my APP.

I’ve contacted FastSprings support numerous times, but the most they’ll do regarding formatting scripts is ‘You’re on the right track’, or, unofficially, tell me to try ‘Postman’.

I’ve entered my credential into Postman, and it tells me there’s one text subscription, but every iteration of my Shell script returns “”, using the same credentials. I know I have to have a ‘User-Agent' header, but support won’t advise me if I’ve got the terminology correct. And my Googling shows there’s lots of ways to format a ‘User-Agent’ header!!! Postman uses it’s own method of addressing the FastSprings site.

I’m at a dead end! I’m told I have to use a ‘GET’, but read that the -H is  the ‘GET’ equivilent.

Any advice from anybody, please. I’m just guessing, and Googling is frustratingly un-informative.

If necessary, is there another company that makes this easier? I’ve battled with FastSpring support for over a week now. They absolutely refuse to supply or post examples. I need a pop-up store on my website, and a storefront for cancelling subscriptions.

Regards

Santa



set jsonURL to do shell script ("curl ' -H User-Agent:2V57JR9HQNAZNZVOW6OZXG/my-password-as-used-in-Postmanhttps://api.fastspring.com/subscriptions/limit=10000")
_._,_._,_


Brian Christmas
 

G’day scripters

I’m trying to work out the correct syntax to get a subscription list from FastSpring, who I’m trying to set up to manage my subscritions for my APP.

I’ve contacted FastSprings support numerous times, but the most they’ll do regarding formatting scripts is ‘You’re on the right track’, or, unofficially, tell me to try ‘Postman’.

I’ve entered my credential into Postman, and it tells me there’s one text subscription, but every iteration of my Shell script returns “”, using the same credentials. I know I have to have a ‘User-Agent' header, but support won’t advise me if I’ve got the terminology correct. And my Googling shows there’s lots of ways to format a ‘User-Agent’ header!!! Postman uses it’s own method of addressing the FastSprings site.

I’m at a dead end! I’m told I have to use a ‘GET’, but read that the -H is  the ‘GET’ equivilent.

Any advice from anybody, please. I’m just guessing, and Googling is frustratingly un-informative.

If necessary, is there another company that makes this easier? I’ve battled with FastSpring support for over a week now. They absolutely refuse to supply or post examples. I need a pop-up store on my website, and a storefront for cancelling subscriptions.

Regards

Santa



set jsonURL to do shell script ("curl ' -H User-Agent:2V57JR9HQNAZNZVOW6OZXG/my-password-as-used-in-Postmanhttps://api.fastspring.com/subscriptions/limit=10000")