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?

4 Answers 4

0

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

2

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
0

I think Ted Wingly's answer is less likely to throw errors than the accepted answer ! It's always a good idea to use "quoted form of" in a path, in case names have spaces in them. The System Events version would actually throw up an error as it's written, if the file filename doesn't exist. to avoid the error, you'd need to write it like this :

tell application "System Events" to set targetfolder to quoted form of (get POSIX path of temporary items folder & filename)
-1

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

You must log in to answer this question.

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