2
set targetFile to (path to temporary items as string) & fileName

I am trying to download a web image to temporary folder by curl in a Apple Script.

According to the above code, the curl downloads it as correct but the targetFile is currently as:

Macintosh HD:private:var:folders:...TemporaryItems:image.png

But after download, I need the downloaded image path as a correct file path like:

/tmp/image.png

How can I set targetFile as a string that have slash containing file path? Or how can I get the file path of the image after it is downloaded by curl?

1

You can work around this with something like

set a to "/tmp/" & fileName
set targetFile to POSIX path of a
--since you are using curl
do shell script "curl http://example.com/images.jpg > " & targetFile

This AppleScript will make a string containing /tmp/, and targetFile sets it to interpret as POSIX (used by shell and curl). Then it downloads images.jpeg and stores it as fileName

| improve this answer | |
0

Try this code:

set targetFile to quoted form of ((POSIX path of (path to temporary items)) & filename)

That will give you a proper POSIX path to the standard temporary folder. On my machine, for instance, this gave me:

'/private/var/folders/rl/zwlvv_hd6ss4c4yhb6nxxfz80000gn/T/TemporaryItems/<somefilename>'

I've used the quoted form of command in case the filename used contains a space or other special shell character.

You could also use System Events.app, like so:

tell application "System Events"
    set targetfolder to quoted form of (get POSIX path of file filename of temporary items folder)
end tell
| improve this answer | |
-2

Other answers are good, but do not address security issues. Other users of the machine can see files in /tmp. Combining the question and accepted answer yields (IMHO) a better answer:

set fileName to "output.txt"
set targetFile to (path to temporary items as string) & fileName
log POSIX path of targetFile

This yields /private/var/folders/m1/ldn6fy6n38d52b1fmztr99br0000gp/T/TemporaryItems/output.txt

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .