Editing the Task Script

KosmicTask Help

Editing the Task Script

To edit the script for our task click the Edit button in the toolbar. A list of script templates will be displayed.

Edit window edit view

KosmicTask includes templates for a wide range of scripting languages. These templates serve as starting points for basic task activities. Because we previously selected Bash shell as our script type we are presented with a list of suitable Bash templates. The simplest of these is called Hello Kosmos. Select it and click OK. The selected template contains some comments at the top and a single command which says:

# send result to stdout
echo "Hello, kosmos!"

Every line that starts with a # is a comment and will not be interpreted as a command. Delete these two lines and replace them with our command like so:

# convert file to html
textutil -convert html "/Users/Muggins/Documents/My Pages document.rtf"

Now, your username will not be Muggins so edit this to replace Muggins with your username. It will normally be displayed in the top right hand corner of your screen. Alternatively you can open up the finder and navigate to your Documents folder. The Path Bar at the bottom will show the full path to your Documents folder.

So now our script should look like:

# convert file to html
textutil -convert html "/Users/< MY USERNAME HERE ! >/Documents/My Pages document.rtf"

That's it. Our script is ready to go!

Testing Our Task

To display the run view click the square Run button.

Edit window run view

Now our task expects to find a file named "My Pages document.rtf" in our Documents folder. So let's create one (if you don't have Pages installed you can download the iWork trial).

  1. Open Pages and create a new document.
  2. Type "I once was a Pages document!".
  3. Select the text and change the font size to 24 and the colour to green (I chose the Fern crayon).
  4. Save the document to your Documents folder as "My Pages Document".
  5. Select menu File - Export and export as RTF to your Documents folder.

To run the task click the Play button in the toolbar or the large blue Play icon. 

If you see something like the following then the task cannot find the required file:

STDERR output:
Error reading /Users/Muggins/Documents/My Pages Document.rtf. The file doesn’t exist.

If the above occurs use the Finder to verify that the file is named correctly and is in the Documents folder. Then try and run the task again. If you don't get an error message but simply a blank result in the right hand panel then the task succeeded. Open the Finder and navigate to you Documents folder. Double click on "My Pages document.html" to open the file in your browser.

Okay, our task works so we better save it. Select menu File - Save.

Improving Our Task 

So we have achieved our basic goal but things are not as good as they could be. For instance it would be a lot better if a successful task execution produced a better output than a blank screen. So we will edit our task script to display a message when the task succeeds. Click the Edit button to go back to the edit view and change our script to read:

# convert file to html
textutil -convert html "/Users/Muggins/Documents/My Pages Document.rtf"
echo "HTML file saved to Documents folder"

Remember, change Muggins to your username in the above example!

Our improvement is simply to print a helpful message. To do this we use the Bash "echo" command. By Default KosmicTask returns all a task's printed output as part of the task results. So to return text to the user, in any of the supported scripting languages, all we do is print the text to the screen (or to be precise, send the text to stdout).

Now run the task again. Instead of a blank screen we now get a message informing us that the converted file is available in our Documents folder. This is definitely an improvement. However there are still some problems. Notable amongst these are that we are only able to convert a single named file within our Documents folder. It would be much better if we could specify which file we wanted to convert when the file was run. That way we can use our task to convert any RTF file to HTML. This will be our next improvement.