'offset' in Applescript

Discussion in 'Mac Programming' started by Scouze, Mar 3, 2010.

  1. Scouze macrumors newbie

    Joined:
    Mar 3, 2010
    #1
    Hi,

    I've a simple serial number database (really simple!), which stores the serial number itself with the number of times it can be used. Like this:

    thisisatest123456,06,John Smith
    0000ZZZZZZZZZZZZZ,05, Janet Jones
    etc..

    The list is stored in a simple text file (UTF-8 coding). My Applescript gets the serial number to test for using:

    set numberFile to open for access list_path
    set theNumbers to read numberFile


    and

    if theNumbers contains numberToCheck Then.... End If

    to confirm the serial number's in the text file at all. Then, to get the activation times left I use:

    set ActTimes to (offset of numberToCheck in theNumbers) + 18

    My problem seems to be with this last line of code. Sometimes the offset works and sometimes it's out by +/- 1 so with some serial numbers my script tries to use ",0" and others it tries to use "5," as an activation count... I'm guessing that it's something to do with the encoding of the text file but I wonder if my lack of knowledge about Applescript is just at fault.

    Could anyone shed any light? Thanks in advance!
     
  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    Use text item delimiters. They're fast and versatile.

    Code:
    set theText to "thisisatest123456,06,John Smith
    0000ZZZZZZZZZZZZZ,05, Janet Jones"
    
    set TID to AppleScript's text item delimiters
    
    set AppleScript's text item delimiters to ","
    
    set oneItem to (text item 2 of paragraph 1 of theText)
    
    set AppleScript's text item delimiters to TID
    
    oneItem
    The result will be "06" (a string; you might have to coerce it to an integer).

    In general, save off the existing delimiters in a variable (TID), set the delimiters to what you want (in this case a comma), save the text item in a variable, and then restore the delimiters to their original state before the script exits.

    mt
     
  3. Scouze thread starter macrumors newbie

    Joined:
    Mar 3, 2010
    #3
    Thanks mt!

    You wouldnt happen to have a handy tip on counting the number of delimiters would you? I'd like to be able to search for a particular serial number and then grab the number after it.

    I realise I can do a loop to count through the text, but how would I know when I'd reached the last item in the text?
     
  4. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    Something like:

    set n to the number of text items of aVar

    or possibly

    set n to count of text items of aVar

    mt
     
  5. Scouze thread starter macrumors newbie

    Joined:
    Mar 3, 2010
    #5
    Thanks!

    I used 'count'. To be honest I was very unsure about lists but your response prompted me to look them up properly.

    It's a much better way of doing things: I was convinced that reading the text file as a whole was the only way. :)
     
  6. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #6
    To read a UTF-8 encoded text file:

    Code:
    set theNumbers to read numberFile as «class utf8»
     

Share This Page