Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Opening a curl downloaded file in Applescript?

Hi, kind of new to Applescript and hoping someone could help me. I am using a script to download an image file with curl to my Desktop (with the file named after the title of my active Google Chrome tab). This works fine and as expected.

The problem is the line afterwards where I want to open that file in another application. It's not working and I'm not sure why. I assume it's probably trying to open the URL path instead of the local file? I am trying to save the results of this (DownloadedImage.jpg) as a variable but not sure if I'm doing it right. This is the portion of the script:


set downloadedFile to (do shell script "curl -o " & quoted form of (savePath & tabTitle & ".jpg" as text) & space & quoted form of theURL)

tell application "Preview" to open "" & downloadedFile & ""


Any assistance would be much appreciated.

iMac, OS X El Capitan (10.11.4), 2012 iMac, 32GB RAM

Posted on May 3, 2016 11:31 AM

Reply
Question marked as Best reply

Posted on May 3, 2016 12:32 PM

Your problem is actually quite simple.


In your script:


set downloadedFile to (do shell script "curl -o " & quoted form of (savePath & tabTitle & ".jpg" as text) & space & quoted form oftheURL)

the variable downloadedFile will be the text output of the do shell script command. It has nothing to do with the resulting file that was downloaded. Instead, it's the actual text that you would have seen if you'd run the same curl command in a Terminal window. There's no way for preview to coerce that into a file reference to open.


The good thing, though, is that you know where the file is, since you have to pass that data into curl in the first place. For ease of maintenance, I'd simply create one new variable that tracks the path of the file and use that in both statements.


set filePath to quoted form of (savePath & tabTitle & ".jpg" as text)


set downloadedFile to (do shell script "curl -o " & filePath & space & quoted form of theURL)

tell application "Preview" to open file filePath


So now you open the filePath, not the output of the shell command.

2 replies
Question marked as Best reply

May 3, 2016 12:32 PM in response to Sean Mooney

Your problem is actually quite simple.


In your script:


set downloadedFile to (do shell script "curl -o " & quoted form of (savePath & tabTitle & ".jpg" as text) & space & quoted form oftheURL)

the variable downloadedFile will be the text output of the do shell script command. It has nothing to do with the resulting file that was downloaded. Instead, it's the actual text that you would have seen if you'd run the same curl command in a Terminal window. There's no way for preview to coerce that into a file reference to open.


The good thing, though, is that you know where the file is, since you have to pass that data into curl in the first place. For ease of maintenance, I'd simply create one new variable that tracks the path of the file and use that in both statements.


set filePath to quoted form of (savePath & tabTitle & ".jpg" as text)


set downloadedFile to (do shell script "curl -o " & filePath & space & quoted form of theURL)

tell application "Preview" to open file filePath


So now you open the filePath, not the output of the shell command.

Opening a curl downloaded file in Applescript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.