Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Scouze

macrumors newbie
Original poster
Mar 3, 2010
7
0
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!
 
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
 
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?
 
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. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.