Another way to upload?

Is there an alternative way to upload without “URL Access Scripting” that is more reliable? I need a way to upload to a FTP server that is able to be done is Applescript Objective-C. Please help. (I know that username and password. Don’t worry)

Hi,

cURL via NSTask or very convenient with Connection Framework

Can you show me an example of uploading through NSTask? I really need help to make my app. :smiley:

I’m using a simple method

[code]- (NSString *)runShellScript:(NSString *)cmd withArguments:(NSArray *)args error:(NSString **)errorString
{
NSPipe *outputPipe, *errorPipe;
NSFileHandle *outputHandle, *errorHandle;
NSTask *task;
NSData *outputData, *errorData;

outputPipe = [NSPipe pipe];
errorPipe = [NSPipe pipe];
task = [[NSTask alloc] init];
[task setLaunchPath:cmd];
[task setArguments:args];
[task setStandardOutput: outputPipe];
[task setStandardError: errorPipe];

outputHandle = [outputPipe fileHandleForReading];
errorHandle = [errorPipe fileHandleForReading];
[task launch];

outputData = [outputHandle readDataToEndOfFile];
errorData = [errorHandle readDataToEndOfFile];
if ([errorData length])
	*errorString = [[[NSString alloc] initWithData:errorData encoding:NSUTF8StringEncoding] autorelease];
else
	*errorString = nil;
NSString *result = [[[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding] autorelease];
[task release];
return result;

}[/code]
the method call looks like

NSArray *args = [NSArray arrayWithObjects:destinationPath, @"-u", userpass, @"-T", tempFile, @"-sS", nil]; [self runShellScript:@"/usr/bin/curl" withArguments:args error:&error];
userpass (NSString) is a string with format “username:password”
[b]destinationPath/b is the full path on the FTP server
[b]tempFile/b is the path to the file to be uploaded
error (NSString) contains the cURL error message, if an error occurs

Oh, so all it is would be "do shell script “curl -T " & quoted form of POSIX path of thefile & space & “ftp://username:password@00.00.00.00/folder/””

So, nothing special?

yes, that’s all

I tried that. :confused: I don’t want to use scripting additions or URL access scripting. Is there any other option? :frowning:

what’s wrong with curl? It’s the easiest way

It doesn’t work. I tried uploading with it and it didn’t upload AND NO ERROR SHOWED UP! :mad:

That’s against the rules! :frowning:

curl works great, may be you did something against the rules.
Have you specified the full path including the proper file name?
ftp://username:password@00.00.00.00/folder/filename.ext"

OK. I fixed it and put

do shell script "curl -T " & quoted form of POSIX path of (thefile as string) & space & quoted form of "ftp://username:password@mysite.com/chat/chat.txt"

AND NOTHING WORKED.
I am probably just being an idiot and it’s my fault. :o

you need the FULL path starting at the root directory on your server
which is often not the same as the URL you use in the browser
Do you have any FTP client (like Transmit or Fetch), there you can see the full path

OK. I have Cyberduck.

There is a folder name “chat”. In that folder is a file named “chat.txt”. Wouldn’t it look like this?

do shell script "curl -T " & quoted form of POSIX path of (thefile as string) & space & quoted form of "ftp://username:password@mysite.com/chat/chat.txt"

NOTE: There is no “www.”. Adding that would make the host go to another server.

of course there is no www, we’re talking about ftp :wink:

For example, one of my domains has the full path ftp://mysite.com/httpdocs/folder/file.ext
the httpdocs folder is specified by the ISP.
To access the file with a browser, the URL ftp://mysite.com/folder/file.ext is sufficient.
But for cURL the full path is required

@dylan - did you understand the chat app I emailed you earlier?

Sorry for an hour of questions, but WHAT?

I have to put an extra folder in my path? I was able to upload before, but just not a reliable. I’m confused. :confused:

No. I thought there was only two changes. This is what I have now (it took that long)

--  Log
--  Created by Dylan Weber on Friday, February 26, 2010.
--  Copyright 2010 Dylan Weber. All rights reserved.

script LogAppDelegate
	property textView : missing value -- a text view
	property textfield : missing value -- multi-line text field
	property mainwin : missing value -- main window
	property namefield : missing value -- name text field
	property namewin : missing value -- name window
	property userName : missing value
	property history : ""
	
	
	on awakeFromNib()
		set history to do shell script "curl ftp://dylanweber:passwordiscensored@mactweaks.info/chat/chat.txt" as string
		textView's setTextContainerInset_({10.0, 10.0})
		textfield's setStringValue_("Type here!")
		mainwin's makeFirstResponder_(textfield)
		textView's setString_(history)
	end awakeFromNib
	
	
	on action_(sender)
		set thetext to textfield's stringValue() as string
		textfield's setStringValue_("")
		
		if userName is in {"", missing value} then
			textView's setString_("Please enter a name in the name field." & return & history)
			myname()
		else
			set history to (userName as string) & " wrote \"" & thetext & "\" at " & ((current date) as string) & return & history
			textView's setString_(history)
			set result_ to upload(history, "Dylan's Laptop:Users:dylanweber:Library:Application Support:Messaging:chat.txt")
			set history to do shell script "curl ftp://dylanweber:passwordiscensored@mactweaks.info/chat/chat.txt" as string
			if result_ is false then
				set result_ to upload(history, "Dylan's Laptop:Users:dylanweber:Library:Application Support:Messaging:chat.txt")
				if result_ is false then
					textView's setString_("An error has occured during your chat. Try again immediately." & return & history)
				else
					if history is ((do shell script "curl ftp://dylanweber:passwordiscensored@mactweaks.info/chat/chat.txt") as string) then
						set history to do shell script "curl ftp://dylanweber:passwordiscensored@mactweaks.info/chat/chat.txt" as string
						textView's setString_(history)
					else
						textView's setString_("An error has occured during your chat. Try again immediately." & return & history)
					end if
				end if
				set history to do shell script "curl ftp://dylanweber:passwordiscensored@mactweaks.info/chat/chat.txt" as string
				textView's setString_(history)
			end if
		end if
	end action_
	
	on updatechat_(sender)
		set history to do shell script "curl ftp://dylanweber:passwordiscensored@mactweaks.info/chat/chat.txt" as string
		textView's setString_(history)
	end updatechat_
	
	on updatename_(sender)
		myname()
	end updatename_
	
	on myname()
		set userName to namefield's stringValue()
		if userName is in {"", missing value} then
			namewin's makeKeyAndOrderFront_(me)
			namewin's makeFirstResponder_(namefield)
		else
			namewin's performClose_(me)
		end if
	end myname
	
	on upload(this_data, target_file)
		tell current application
			try
				set target_file to target_file as Unicode text
				if target_file does not contain ":" then set target_file to POSIX file target_file as Unicode text
				set the open_target_file to open for access file target_file with write permission
				set eof of the open_target_file to 0
				write this_data to the open_target_file starting at eof as string
				close access the open_target_file
				process(alias (target_file as string), "ftp://dylanweber:passwordiscensored@mactweaks.info/chat/chat.txt")
				return true
			on error e
				try
					close access file open_target_file
				end try
				log e
				return false
			end try
		end tell
	end upload
	
	on process(theURL, thefile)
		set thefile to thefile as Unicode text
		try
			do shell script "curl -T " & quoted form of POSIX path of thefile & space & quoted form of theURL
			return true
		on error e
			log e
			return false
		end try
	end process
end script

No, the one I emailed you this morning had a php script to put on your server to handle the reading/writing to the chat.txt file. There is also a timer setup in the app to poll the php script for any new text added by the person you are chatting with.

I tested it with my son and it works fine. Though not a production app. I built it pretty quick just as an example. You can test it the way it is and use the php script on my server or you can upload it to yours and change the paths in the app to match your server.

Craig, I sent you an e-mail. Hope you see it. :smiley: