Page 1 of 1

Text File Manipulation

Posted: Sun Sep 20, 2009 3:02 pm
by wee wullie
Does anyone know of a way to get a certain amount of data from a text file, i.e. a text file that contains a list of recipe's in the format:

recipe title:

recipe notes:

recipe ingredients:

recipe directions:

**********

and how to make each recipe appear on a new card?

Posted: Mon Sep 21, 2009 6:06 am
by Janschenkel
If the text file is relatively small (i.e. < 500K) the easiest way is to read it into a variable as a whole, and go through it line by line from there. Assuming you have setup a background groupo with all the necessary fields (title, notes, ingredients, directions), your import button script could look something like this.

Code: Select all

on mouseUp
  answer file "Readh which recipe file?"
  if the result is "Cancel" then exit mouseUp
  put "file:" & it into tURL
  put URL tURL into tData
  repeat for each line tLine in tData
    if tLine is "recipe title:" then
      create card  -- start a new recipe card
      put "title" into tCurrentField
    else if tLine is "recipe notes:" then
      put "notes" into tCurrentField
    else if tLine is "recipe ingredients:" then
      put "ingredients" into tCurrentField
    else if tLine is "recipe directions:" then
      put "directions" into tCurrentField
    else
      put tLine & return after field tCurrentField
    end if
  end repeat
end mouseUp
HTH,

Jan Schenkel.

nearly

Posted: Wed Sep 23, 2009 12:55 am
by wee wullie
Hi Jan, thanks for your reply, i tried the code you posted and it filled two fields correctly: recipe ingredients is ok, recipe directions is ok but recipe Notes has no text and recipe title filled ok but it also has the text from the recipe notes field,
it nearly worked, it think it may just need tweaking slightly.

Posted: Wed Sep 23, 2009 1:19 pm
by Janschenkel
I'm afraid you'll just have to go and debug it - I can't see a reason why it wouldn't work given your example. Perhaps the import file has an extra space before or after a colon?

Best of luck,

Jan Schenkel.

Sorted

Posted: Fri Sep 25, 2009 12:52 pm
by wee wullie
Thanks Jan,
I managed to get it sorted out in the end,

thanks for your help!