Applescript VS Shell Script

I’m trying to make an API call to Google Vision to return the verticies of an image crop as json.
I have successfully done this (entire project) in Python, but if we want to push this out to other teams, I have to forego the Python route unfortunately.

For whatever reason, I can’t get the call to work in Applescript. It only returns an empty string. I have a suspicion it has to do with my muli-line CURL command that is not formatted correctly.

Nevertheless, I am moving forward w/o the Applescipt implementation and just doing the shell call in Xojo.

Here is what I have:

sh= new Shell 
dim s as String
dim command as string
dim nl as string
nl = EndOfLine.UNIX
command = "cd ~/Desktop" + nl + "export GOOGLE_APPLICATION_CREDENTIALS=""</path to json token>""" + nl + "curl -X POST" +nl + "https://vision.googleapis.com/v1/images:annotate?key=KEY”" + nl + "-H ""Content-Type: application/json; charset=utf-8""" + nl + "-d @request.json"""

sh.Execute(command)

s = sh.result
System.DebugLog(s)

But this yields the following error:
curl: no URL specified!

This is the exact way I run it in Terminal and I get the desired json output.

I would try removing the new lines between the curl parameters and replace them with a space.

You seem to have a curly double quote at the end of the URL. Should that be present?

KEY”"

I removed the new lines and removed one of the double quotes as suggested and now there is a syntax error. I’m pretty horrible and escaping quotes, so I’ll have to look it over a bit after lunch.

I ran my command through the debugger and everything looks as it should (with quotes and all)
However, I am STILL getting an error saying there is no url specified. So I threw in the --url flag and still no luck.
I don’t understand why this works in Terminal but not in Xojo.
Any other ideas @kevin_g ?

From the debugger, can you copy and paste the contents of command here?

Hi @Kem_Tekinay,
I ended up modifying it even more. And I’m not getting errors.
But it is also not displaying any of the json that is being returned…
I was assuming it would just spit it out w/o having to parse it? But maybe that’s not the case?

I added line separators at the end of each line AND kept the new lines in between each parameter. (I have edited my original question to reflect the changes)
This was the only combination I found that didn’t produce an error.
Below is the contents of command.

3:47:24 PM : cd ~/Desktop \
export GOOGLE_APPLICATION_CREDENTIALS="/<path to json token>" \
curl -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://vision.googleapis.com/v1/images:annotate?key=****
3:47:46 PM : test Ended

Never mind - I guess I can’t edit it once there is activity on the post.
This is the new code for command:

command = "cd ~/Desktop \" + nl + 
"export GOOGLE_APPLICATION_CREDENTIALS=""/<path to json token>"" \" + nl + 
"curl -X POST \"  + nl + 
"-H ""Content-Type: application/json; charset=utf- 8"" \" + nl + 
"-d @request.json \" + nl +
"https://vision.googleapis.com/v1/images:annotate?key=****"

sh.Execute(command)
s = sh.Result

System.DebugLog(command)
System.DebugLog(s)

Is the Shell reporting an error? You aren’t checking for that in your code.

No error from Shell.

I’m surprised because that command won’t work as written.

In the shell, “” + nl is a line continuation, so you have merged the various commands into one, long, run-on command. Try removing the backslashes after “Desktop” and “token>”"".

1 Like

Holy crap.
Please explain why I had to remove them from those two lines but I could keep the rest?!

You are issuing three commands here, cd, export, and curl. Each takes parameters and must be grouped. If you do this:

cd /path \
export that \
curl ...

it’s the same as writing:

cd /path export that curl ...

and obviously that wouldn’t work.

1 Like

Likewise, within the curl command, you need to group all the parameters, so this wouldn’t work:

curl param1
param2
param3

but this will

curl param1 \
param2 \
param3
1 Like

Got it. That makes sense.
Thanks again @Kem_Tekinay!
Always a life saver.

1 Like