downloading a file from FTP - usign curl ftp

I am terrible using shell scripting, but sometimes there appears to be no other option. Can anybody offer a suggestion as to why I might be receiving this error message below? All I wish to do is copy a known file from a known directory on an FTP to a folder on my Desktop.

Thanks in advance,
-Jeff

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
curl: (9) Server denied you to change to the given directory

set user to "diskads"
set thePassword to "diskads"
set theURL to "10.70.70.1"
set theDirectory to "app/afp/diskads"
set theFile to "indexZZZ.html"
set localdirectory to "Desktop/Jeff"
set localfile to "indexZZZ.html"

do shell script "curl ftp://" & user & ":" & thePassword & "@" & theURL & "/" & theDirectory & "/" & theFile & " -o '~/" & localdirectory & "/" & localfile & "'"

Hello.

It is a difference of capturing what is being written directly to screen, and what is sent to standard error.

With curl, at least the curl I have, you can write – stderr - to redirect the standard error to standard output, (see your manual).

Generally, you’d add this redirection after a command in a do shell script so that you can get at the error message, you should then also be sure to end the whole command with exit 0 so that the do shell script command doesn’t fail.

set errtext to do shell script "make fuzz 2>&1 ; exit 0"
log errtext
--> make: *** No rule to make target `fuzz'.  Stop.

You can of course redirect both standard error and standard output to a file, be sure to redirect to the file first then, and add the redirection later.

set errtext to do shell script "make fuzz >~/fuzz.txt  2>&1 ; exit 0"

Otherwise, the error is sent to standard output, while the real standard output is sent to the file, which, is also a useful option in some circumstances.

I hope this helps. :slight_smile:

Hi,

AppleScript does not recognize the tilde in a shell script line, you have to specify the full path


.
set localdirectory to POSIX path of (path to desktop) & "Jeff"
.
do shell script "curl ftp://" & theURL & "/" & theDirectory & "/" & theFile & " -u " & user & ":" & thePassword & " -o " & quoted form of (localdirectory & "/" & localfile)

Hello.

It is always safest to use full paths, but most unix commands, understands the tilde operator, so from that side of things, it should work right more often than not, the problem may come around when you have to play safe, and use the quoted form of the path, since the tilde won’t get expanded inside quotes.

Then you can tildexpand yourself by

set tildeExpanded to do shell script "echo" & theFile

Before you use the quoted form in the “real” do shell script".

It’s almost never the command itself that understands the tilde operator, but it’s the shell that replaces it with the path to home folder before the argument is send to the command. But it’s indeed better to use full paths instead.

At least on my machine, it is sh, and not bash, that is the shell that is invoked, to interpret and execute the script command. :slight_smile:

The other thing, is that the tilde operator has the advantage of letting you write 'user-independent scripts easily.

The safe and sound way to replicate that functionality would be be to use something like:

set pxStem to POSIX path of (path to home folder)

Instead of the tilde, ‘~’ so you don’t have to use (an) extra do shell script(s) to tilde expand, (a) path(s), but can assemble paths, and use the quoted form of directly, so you are sure that nothing breaks because of any spaces in a file name.

I meant shell, not bash or sh, thanks for pointing that out :). sh or not it’s still not the command itself that does the resolving :wink: I’m just saying that, even it feels like nitpicking, because the difference for a reader can be huge. When you say “most unix commands, understands the tilde operator” it feels much more of an uncertain and unreliable feature that depends on the implementation of each command individually. Knowing that the shell itself is actually expanding the tilde feels much more like a reliable feature that will work for every command.

That is not entirely true, I mean AppleScript does indeed not recognize tilde paths, but the shell still does. if Jeff had move the quote behind the tilde and slash the tilde would expand. tildes are not expanded in quoted string, because the tilde loses it’s special meaning.

set user to "diskads"
set thePassword to "diskads"
set theURL to "10.70.70.1"
set theDirectory to "app/afp/diskads"
set theFile to "indexZZZ.html"
set localdirectory to "Desktop/Jeff"
set localfile to "indexZZZ.html"

do shell script "curl ftp://" & user & ":" & thePassword & "@" & theURL & "/" & theDirectory & "/" & theFile & " -o ~/'" & localdirectory & "/" & localfile & "'"

Or:

use framework "Foundation"

set fullPath to (current application's NSString's stringWithString:posixPath)'s stringByExpandingTildeInPath() as text

Thank you all very much for the replies and advice. Shane’s suggestion will most likely work since it appears to be addressing what is taken care of %2f in the snipped below

What worked for me was the following:

do shell script “curl -u” & user & “:” & thePassword & “'ftp://” & theURL & “/%2f/” & theDirectory & theFile & “’ -o ~/” & localdirectory & “/” & localfile

Obviously not my text below but something I found in my searching the web:

In FTP URLs, the path is relative to the starting directory (usually your homedir). You need to specify an absolute path, and that means using %2f to specify /. This is needed because the path in ftp: URLs is treated as a list of slash-separated names, each of which is supposed to be given to a separate CWD command. The %2f is decoded after splitting. See RFC 1738 and FTP URLs. As for the output location, just give a path to -o.

Hello.

@ Jeffkr: I am glad you made it. :slight_smile:

I stand corrected, we were talking about tildexpansion from the commandline here, and the shell takes care of it. Why I differed between sh, and bash, is of course that the bash niceties, like &> and so on, doesn’t work with sh. When using commandline stuff with sh, you better stick to what sh can do, or execute your command inside a bash commandstring like this:

do shell script "/bin/bash -c 'nice stuff here'"

Edit

And just for the record, System Events knows about tilde expansion, even though AppleScript don’t, and so do Finder (which has nothing to do with AppleScript per say), when you enter a path after having hit cmd-shift-G.

tell application id "sevs" to set foundIt to exists folder "~/Desktop"
log foundIt
--> true

I’m always amazed by everybody’s willingness to assist and share knowledge on this site.

Now I actually have two additional questions (prior to scouring the web):

1.) would my script fail if the FTP password contained an @ character, i.e., “passw@rd”???

2.) Does an SFTP site require the “s” preceding the “ftp” in the shell script?

Such as this:
do shell script “curl -u” & user & “:” & thePassword & “s’ftp://” & theURL & “/%2f/” & theDirectory & theFile & “’ -o ~/” & localdirectory & “/” & localfile

Hello.

I am not sure about the ‘@’ character, I know for sure that in some circumstances ‘;’ won’t work, it really depends on the decoding of the characters of the sftp server, the only way to know, is to try it out.

Edited: it all boils down to what kind of encoding you are using, and what locale (language), if this was utf-8 all the way, then you would be safe, but I suspect the encoding to be iso-8859-1, since that is really the standard for the web.

I belive you’ll have to specify the sftp protocol, if you use sftp, but what do I know, try to find a curl example that downloads a file from sftp. :slight_smile: -I believe that, in the same way, that you don’t use the https:// protocol, when specifying http:// as the protocol in use.

Thank you McUsrII,
After some digging around, it appears as if curl does not support sftp, without the use of some additional installed components. but I could be wrong? If that’s the case, then I am sort of stuck here. Unless I can find another method to grab files to and from an sftp using shell script or any other code that can be used within AS. Unfortunately this is unknown territory for me.

btw, I could probably get around the ‘@’ character by encoding it using %40

But I can’t be certain the ‘@’ character is the issue. I think the sftp aspect is the root cause, especially since.

do shell script “curl -V”

outputs:

curl 7.16.4 (i386-apple-darwin9.0) libcurl/7.16.4 OpenSSL/0.9.7l zlib/1.2.3
Protocols: tftp ftp telnet dict ldap http file https ftps
Features: GSS-Negotiate IPv6 Largefile NTLM SSL libz