Do Shell Script for lftp failing

Well, I’m back a couple of days later with another “do shell script” that fails. Of course, last time it actually worked, it was just my stupid code making it appear to partially fail. At least this actually fails with an error.

do shell script "/usr/local/bin/lftp -u ftpremote01,[password] -p 21 [IP Address] << \"EOF\"
pget -n 10 'Test/main.scpt'

So, if I paste the first line into the terminal and run it, then paste the second line into the terminal and run it, each works fine.

For the Applescript, I get:

It seems very odd to me the phrasing of the error, with “Test/main.scpt: /main.scpt” as if the second part of the path was passed again? Or maybe that’s just the format for the error from lftp, it shows the whole path, and then the name of the file?

I’m suspect something wrong with my heredoc, I’m not very experienced with shell. Just trying to blunder through.

I added the “-d” argument for more feedback, here’s what’s returned in case that’s helpful. (With the password and IP address redacted).

I don’t know if I’ve got a syntax error? That seems a lot more likely to me than that I somehow really don’t have permissions for this inside a “do shell script” when I do have them on the terminal.

Ah, one more thought. Maybe the lack of permission being reported isn’t server side, maybe it’s local side - running these commands in Terminal, the file comes down to my User folder. Maybe the script is trying to download it to whatever the “home folder” is for “do shell script,” which isn’t my user folder. I tried adding an lcd command to change the path lftp would download to, but with no luck. Whatever path I try to LCD to errors:

returns the error:

returns the error:

Where does a “do shell script” think it’s default directory path starts?

Any suggestions greatly appreciated.



Well, nevermind. I was about to post when I had the thought about the default local directory for “do shell script” and that that could be where the permissions problem was. So I tried “lcd,” it didn’t fix my problem, so I went ahead and posted.

I didn’t realize I forgot to quote my attempts to fix that with “lcd.”

So that’s it, it’s working now:

do shell script "/usr/local/bin/lftp -u ftpremote01,[password] -p 21 [IP address] << \"EOF\"
lcd '/Volumes/Hackintosh HD/Users/work/Downloads'
pget -n 10 Test/main.scpt

Working handler, in case anyone finds this thread and is interested.

I’ll try to come back through and finish out the “upload” case and add commenting.

on lftp_file_transfer(transferDirection, localPath, remotePath, userName, thePassword, serverAddress, thePort, theSegments, useQueue, replace, extraVariable)
	returns true if transfer is successful, or an error message if it is not
	transferDirection: "up" or "down" - which way you're sending a file
	localPath: If downloading, the local directory to download to. If uploading, the local path for the file or directory to upload.
	remotePath: If downloading, the remote path for the file or directory to dowload. If uploading, the remote path for the directory to upload to.
	userName: For the remote server
	thePassword: For the remote server
	serverAddress: For the remote server
	thePort: For the remote server, null if default
	theSegments: For downloads, how many simultaneous connections to use. if null, then segmentation is not used.
	useQueue: boolean. If a queue is used, the download(s) are added a queue and occur in the backround. The handler will continue the Applescript immediately without waiting for the transfer to complete. Note some errors might not be returned by this handler when using a queue.
	replace: boolean.  If a file already exists at the path it's being transferred to, should it be ovewritten?
	extraVariable: In case we need another argument sometime. Pass anything for now.
	if transferDirection is "down" then
		if thePort is null then
			set portText to ""
			set portText to "-p " & thePort & " "
		end if
		if useQueue is true then
			set queueText to "queue "
			set queueText to ""
		end if
		if theSegments is null then
			set segmentsText to ""
			set segmentsText to "-n " & theSegments & " "
		end if
		set replaceText to "set xfer:clobber " & replace & linefeed
		set shellLine to "/usr/local/bin/lftp -u " & userName & "," & thePassword & " " & portText & serverAddress & " << \"EOF\"
		lcd " & quoted form of localPath & linefeed & replaceText & queueText & "pget " & segmentsText & quoted form of remotePath & linefeed & "bye
			do shell script shellLine
			set theResponse to true
		on error theError
			set theResponse to theError
		end try
		display dialog "Upload transfers are not supported yet by this handler."
	end if
	return theResponse
end lftp_file_transfer

EDIT: Added some improvements and comments.