Get live output from terminal?

Hi everyone,

This is my first post on MacScripter although i’ve used this site very much.

Im creating a program with with u can synchronize certain folders (LOCALY).
As of this moment this program uses the rsync terminal command.
I want to be able to monitor the progress from terminal as it advances (Like a progress bar).

Ofcourse i know that ASOC only is single-treaded but i found this topic on MS: http://74.218.91.186/viewtopic.php?pid=143965#p143965

I know absolutely Nothing about C or Objc-C (well apart from what I’ve learned with ASOC)
I need this to work under OSX10.7 with xCode 4

Can anyone help me?

P.S. Soz if I’m in the wrong forum…

Model: Macbook4.1
AppleScript: 10.7
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

Hi,

welcome to MacScripter.
No worry, you’re at the right place.

Search the AppleScriptObjC forum for the keyword fileHandleForReading,
there are some examples of using NSTask asynchronously.

I’m using the mentioned ObjC code exactly for rsync,
the switch --progress prints out the state of progress.
You have to parse the paragraphs to get the information.

I have to step in here. That AppleScript is single threaded doesn’t mean that ASOC is single threaded as well. Even if you write almost eveything in ASOC, the application remains an ObjC application. An ASOC application can’t run without the OOP implicit runtime environment (the drawback of every OOP) because it needs it as much as Objective-C code self does.

Thanks for you reply’s i look forward to having my program work!
But I’m not going to search now because It;s 23:00 here

Here another NSTask example. If you pm me your mail address I can send you the xcode project.

property NSNotificationCenter : class "NSNotificationCenter"
property NSPipe : class "NSPipe"
property NSTask : class "NSTask"
property NSString : class "NSString"


script NSTaskAppDelegate
	property parent : class "NSObject"
	property searchField : missing value
	property searchResults : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
	on startSearch_(sender) --IBACTION
		searchResults's setString_("")
		set currentTask to NSTask's alloc's init()
		set outputpipe to NSPipe's pipe()
		currentTask's setStandardOutput_(outputpipe)
		currentTask's setStandardError_(outputpipe)
		currentTask's setLaunchPath_("/usr/bin/mdfind")
		currentTask's setArguments_({searchField's stringValue()})
		
		NSNotificationCenter's defaultCenter()'s addObserver_selector_name_object_(me, "readPipe:", "NSFileHandleReadCompletionNotification", currentTask's standardOutput()'s fileHandleForReading())
		NSNotificationCenter's defaultCenter()'s addObserver_selector_name_object_(me, "endPipe:", "NSTaskDidTerminateNotification", currentTask)
		
		currentTask's standardOutput()'s fileHandleForReading()'s readInBackgroundAndNotify()
		
		currentTask's |launch|()
	end startSearch_
	
	on readPipe_(aNotification)
		set dataString to aNotification's userInfo's objectForKey_("NSFileHandleNotificationDataItem")
		set newstring to ((NSString's alloc()'s initWithData_encoding_(dataString, current application's NSUTF8StringEncoding)))
		searchResults's setString_((searchResults's |string|() as text) & newstring as text)
		aNotification's object()'s readInBackgroundAndNotify()
	end readPipe_
	
	on endPipe_(aNotification)
		NSNotificationCenter's defaultCenter()'s removeObserver_(me)
	end endPipe_
end script

Hi! This was an excellent “tutorial” for me to build my own small tool set of shell commands. It works fine for commands like mdfind and tail. But when I try more simpler ones like “ls -l” all I get is nothing, nothing in stdout, nothing in stderr! Why? Do some commands have a hidden asynchronous attribute or something like that? Or is Applescript to lazy to show results that are already finished???