THTask

Here is where the magnanimous folks that create externals post them (.zip archives only please).

THTask

Postby sctell » Wed Jun 15, 2011 3:47 am

Hi,

I have produced a new external THTask which is based upon NSTask and is an alternative to the shell function built into SuperCard.

Why produce this?

THTask has the ability for the task to be suspended, resumed and interrupted.

THTask does not block the use of SuperCard while the task is running.

Here is part of the script to example 3:
Code: Select all
-- -q 1 = high quality
-- -v 1 = volume from 0 to 1
-- afplay -h for other options

put merge ("usr/bin/afplay -q 1 -v 0.5 `[[tSongPath]]`") into tCommand

get THTask("launch","/bin/sh",tCommand)


You can suspend, resume or interrupt this task.

When the task has completed a card message taskOutputData is sent from the external to SC at which point you can get the data returned from the task. There will be no data with this example as it is just playing music.

Code: Select all
on taskOutputData
put THTask("outputData") into cd fld 1
end taskOutputData


Clearly there are many many options to using a shell script/task and I have not even scratched the surface testing so I would be grateful for any testing that anyone could carryout.

THTask.zip
(29.98 KiB) Downloaded 335 times
Last edited by sctell on Thu Jun 16, 2011 10:51 am, edited 1 time in total.
sctell
 
Posts: 1128
Joined: Sun Jul 06, 2008 10:41 am

Re: THTask

Postby johnjohnston1 » Wed Jun 15, 2011 9:01 am

Hi Terry,
this looks like it could be very handy. I've tried a couple of things briefly and it works well.

I am wondering about multiple commands, I tried:
Code: Select all
put merge ("cd /Users/johnjohn/Desktop;curl pathToOnlinefileA > fileA;echo 'boo'") into tCommand
get THTask("launch","/bin/sh",tCommand)
put merge ("cd /Users/johnjohn/Desktop;curl pathToOnlinefileB > fileB;echo 'boo2'") into tCommand
get THTask("launch","/bin/sh",tCommand)


changing the card script slightly:
Code: Select all
on taskOutputData
put "output:"&THTask("outputData") &CR after cd fld 1
end taskOutputData


both files downloaded as I continued to work in SC.
but I got:
output:boo2

output:boo2

in the field, I was hoping for:
output:boo

output:boo2

similarly:
Code: Select all
on mouseUp
  put "" into  cd fld 1
  put merge ("ls") into tCommand
  get THTask("launch","/bin/sh",tCommand)
  put merge ("cd ~/desktop;ls") into tCommand
  get THTask("launch","/bin/sh",tCommand)
end mouseUp


gives me the list of my Desktop twice.

in the case of the curl it downloads both files but just echos the second command. With LS where the only result is in the data returned the first set doesn't make it

If I redirect the output:
Code: Select all
on mouseUp
  put "" into  cd fld 1
  put merge ("ls > test1.txt;open test1.txt") into tCommand
  get THTask("launch","/bin/sh",tCommand)
  put merge ("cd ~/desktop;ls > test2.txt;open test2.txt") into tCommand
  get THTask("launch","/bin/sh",tCommand)
end mouseUp

I get the expected result, file contain contents of / and ~/Desktop

Interestingly the field gets:
output:
output:
output:

3 lines, where I expected 2.
Not even sure if I should be trying multiple commands, but it is very tempting.

cheers

John
User avatar
johnjohnston1
 
Posts: 196
Joined: Sun Jul 06, 2008 10:42 am
Location: Glasgow, UK

Re: THTask

Postby sctell » Wed Jun 15, 2011 10:00 am

Hi John,

I have myself been experimenting with multiple commands with SQLite3 and when adding records and on inspection of the file some records were missing.

I think what is happening, probably in your case as well, is that because the command is fairly long time wise and it is running in "parallel" to SC, SC is starting the next command before the result has arrived from the first.

Does this make sense to you?

I found by wait 5 ticks after adding a record to the database that SC could not catch up with the external.
That was really a bit of a kludge.

The issue with multiple commands is probably buffering the data and returning it in the correct order.

I will have to give this some thought.

If anyone has any ideas please post them here.

I had not really considered this issue when coding the external, my main goal was to have SC still functional while one task was running.
sctell
 
Posts: 1128
Joined: Sun Jul 06, 2008 10:41 am

Re: THTask

Postby sctell » Wed Jun 15, 2011 10:33 am

Hi,

I have just implemented a isRunning function that returns whether the task is still running and then implemented these lines in a script with multiple scripts.

It basically stops multiple scripts:
Code: Select all
repeat until THTask("isRunning") = false
end repeat


This is not in the version at the top of this thread. I just did it to see if it resolves the issue. It does so I am concluding that my description of the issue in my previous post is correct.
sctell
 
Posts: 1128
Joined: Sun Jul 06, 2008 10:41 am

Re: THTask

Postby johnjohnston1 » Wed Jun 15, 2011 11:55 am

sctell wrote:Hi John,


The issue with multiple commands is probably buffering the data and returning it in the correct order.

I will have to give this some thought.

If anyone has any ideas please post them here.

I had not really considered this issue when coding the external, my main goal was to have SC still functional while one task was running.


Hi,
i wonder if there could be some sort of id parameter that could be passed to the external and passed back with the data?
get THTask("launch","/bin/sh",tCommand,callback)

on taskOutputData pcallback
etc

having multiple calls going on at he same time could be useful.

cheers

John
User avatar
johnjohnston1
 
Posts: 196
Joined: Sun Jul 06, 2008 10:42 am
Location: Glasgow, UK

Re: THTask

Postby johnjohnston1 » Wed Jun 15, 2011 12:20 pm

Hi again,
or perhaps the function could return an id which is also returned by taskOutputData?
Script could stash id in a global?

cheers

John
User avatar
johnjohnston1
 
Posts: 196
Joined: Sun Jul 06, 2008 10:42 am
Location: Glasgow, UK

Re: THTask

Postby mrBean » Wed Jun 15, 2011 2:28 pm

I don't know if this is exactly what your looking for but I have done a lot of work using the shell function and one thing I do to keep track of things is to retrieve the PID or process ID. To get the PID all you have to do is tack on this command at the end of your shell script "& echo $!"
User avatar
mrBean
 
Posts: 89
Joined: Sat Jan 30, 2010 8:03 am
Location: New York

Re: THTask

Postby sctell » Thu Jun 16, 2011 2:48 am

Hi,

johnjohnston1 wrote:Hi again,
or perhaps the function could return an id which is also returned by taskOutputData?
Script could stash id in a global?


Just to let you know I am now experimenting with John's suggestion.

I currently have it working on the above basis but need to arrange for the id to be returned with or without data.

I have also removed the launchPath which I have hardcoded into the external.

This is a couple of example scripts:
Code: Select all
on mouseUp
global gTaskID
put merge ("/bin/ps -ax | grep Finder") into tCommand
put THTask("launch",tCommand) into gTaskID
end mouseUp

on mouseUp
global gSongTaskID
get THTask("resume",gTaskID)
end mouseUp



Another update perhaps by the end of today, all being well.
sctell
 
Posts: 1128
Joined: Sun Jul 06, 2008 10:41 am

Re: THTask

Postby sctell » Thu Jun 16, 2011 10:50 am

Hi,

I have had a bit of a major rethink since this mornings post.

The underlying principal is the same but it is now implemented differently.

What have I done.

1. The commands still run parallel to SC.

2. You cannot run more than one command at a time.

3. I have implemented a key/command pair dictionary within the external.

What does this mean? Read on.

The following functions:
---------------------------

put merge ("/bin/ps -ax | grep Finder") into tCommand

put "grep test" into tKey

get THTask("addCommandForKey",tCommand,tKey)

get THTask("runCommandForKey",tKey)


---------------------------

You can basically store as many commands as memory allows within the external and they can be accessed as follows:

get THTask("runCommandForKey",tKey) -- run one command

get THTask("runCommandsWithKeys","grep test,afplay test,sips test") -- run 3 commands sequentially

If you wish to reuse a key please ensure you remove the key/command first.

get THTask("removeCommandForKey",tKey)

---------------------------

Additional functions available:

get THTask("suspend") -- suspends the command

get THTask("resume") -- resumes the command

get THTask("interrupt") -- stops the command

-----------------------------

Functions to follow:

get THTask("saveCommandsToPath",tPath) -- save the keys/commands to a file
get THTask("readCommandsFromPath",tPath) -- read the keys/commands from a file


The updated external is at the top of the thread.

Please test and let me know.
sctell
 
Posts: 1128
Joined: Sun Jul 06, 2008 10:41 am

Re: THTask

Postby johnjohnston1 » Thu Jun 16, 2011 9:19 pm

Hi Terry,
Thanks again for all the work. I hopeyou are having fun.
Some findings:
Code: Select all
put "cd ~/Desktop; curl 'http://url2-25mbfiledash.m4v' > dash.m4v" into a
put "curl1" into k
get THTask("addCommandForKey",a,k)
 
put "cd ~/Desktop; curl 'http://url2-25mbfiledash.m4v' > post.m4v" into a
put "curl2" into k
get THTask("addCommandForKey",a,k)

get THTask("runCommandsWithKeys","curl1,curl2")

works very nicely. I get the expected:
curl1
curl2
in the field after changing the cd script to
Code: Select all
on taskOutputData
global gTaskOutputData

put CR & gTaskOutputData after cd fld 1

end taskOutputData



If I put one of those in a button in the example project and run it and then click the Example 2 button before the curl has finished. I get

grep test
in the field, without the grep info when the curl job finished.

Is this what you expect. It seems to me what happens is that the key for the active task is being replaced and the second command dosen't run.
i expect this is because I am going against the one command at a time rule?

hope this is of interest?

cheers

John
User avatar
johnjohnston1
 
Posts: 196
Joined: Sun Jul 06, 2008 10:42 am
Location: Glasgow, UK

Re: THTask

Postby sctell » Fri Jun 17, 2011 5:48 am

Hi,

This thread has become a bit of a mess as a result of me trying different things in relation to running multiple tasks.

I think (famous last words) that I have cracked it but it has involved me going back to basics.

Perhaps the board moderator would be so kind as to delete this thread as I intend starting another to save confusion.
sctell
 
Posts: 1128
Joined: Sun Jul 06, 2008 10:41 am


Return to XCmds, XFcns, and XRtns

Who is online

Users browsing this forum: No registered users and 1 guest

cron