Simple script won't work with Leopard

Hi all you experts!
I have been playing at scripting on the Mac for about 9 months with simple projects - the one on here is giving me a HEADACHE!!! It runs once and sends multiples of listlonglonglonglong to the file then, even thought I delete the file, it tells me the file is open.
I have tried every way I can think of but zilch. So any suggestions will be greatly appreciated.

set the_file_path to "Macintosh HD:users:bobsimmons:desktop:work.txt"
open for access file the_file_path with write permission
repeat 5 times
	set A_list to {}
	set A_list to {1, 2, 3, 4, 5, 6}
	
	repeat with i from 1 to 6
		set item i of A_list to random number from 1 to 49 as number
		
	end repeat
	
	write A_list to file the_file_path for 100
end repeat

close access file the_file_path

Regards Bob

You’re trying to save a list to a text file.

Read “The Ins & Outs of File Read/Write in Applescript” by Nigel Garvey

Adam
Thanks for the link - I will read it with great interest. I do have Hanaan Rosenthal’s book but I find it leaves bits out; for a beginner anyway.
I shall experiment this evening and let you know how I get on.

Regards Bob - Worthing East Sussex where it’s getting very cold!

Hi again Adam

I trolled carefully through the very interesting article and tried fixing my script but, I must be thick, 'cos I can’t make it run properly - whatever I do I still get virtually the same result in the file - that’s if it doesn’t suddenly start telling me the file is already open.

I think I’ll sign “mystified in Worthing” or perhaps “Dumbo” would be better.

Regards Bob

Bob:

So, are you trying to write a list of numbers to a file as just the numbers? What is the overall goal of this script?

Hi Bob,

do you mean something like this?

set the_file_path to ((path to desktop as text) & "work.txt")
open for access file the_file_path with write permission
set {TID, text item delimiters} to {text item delimiters, ", "}
repeat 5 times
	set A_list to {}
	repeat 6 times
		set end of A_list to ((random number from 1 to 49) as integer as text)
	end repeat
	set A_list to (A_list as text) & return
	write A_list to file the_file_path starting at eof
end repeat
write return to file the_file_path starting at eof
set text item delimiters to TID
close access file the_file_path

Hi Craig and Stefank

Well!!! Stefank has solved the problem in one for me - that was exactly what I was trying to do Stefank - I was being thick and just could not see why mine would not work. It was just a little exercise to try and get my grandchildren interested in scripting - they all 3 have iMacs. So I said, being 74 and cleverer than them, I will write you a little script to generate 36 lottery random numbers, then you can work on it to stop the same number appearing twice in any group of six and win a prize from me - sucker!
Thanks again gentlemen for your help Regards Bob

As mentioned above, you can’t write a list into a text file,
therefore I coerced the list into a string with text item delimiters

PS: I guessed it was dealing with a lottery :wink:

Here’s my version, done when I was first learning AppleScript (as an exercise). I used AppleScript “some item of a list” rather than the random number generator because it was much faster. Padding the numbers was just to align the display. printOut could just as well have been written to a file - here it’s just a dialog:

property nums : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49}
set games to the text returned of (display dialog "How many games? " default answer "5") as number
set printOut to ""
repeat games times
	set printOut to printOut & rand() & return & return
end repeat

display dialog printOut

to rand()
	set lst to {}
	repeat until (count lst) = 6
		set randomNumber to some item of nums
		if randomNumber is not in lst then set lst's end to pad(randomNumber)
	end repeat
	
	--> format (insert 3 spaces between numbers)
	set AppleScript's text item delimiters to space & space & space
	set lst to UpSort(lst) as text
	set AppleScript's text item delimiters to ""
	return lst
end rand

to UpSort(my_list)
	set the index_list to {}
	set the sorted_list to {}
	repeat (count of my_list) times
		set the low_item to ""
		repeat with i from 1 to (count of my_list)
			if i is not in the index_list then
				set this_item to item i of my_list
				if the low_item is "" then
					set the low_item to this_item
					set the low_item_index to i
				else if this_item comes before the low_item then
					set the low_item to this_item
					set the low_item_index to i
				end if
			end if
		end repeat
		set the end of sorted_list to the low_item
		set the end of the index_list to the low_item_index
	end repeat
	return the sorted_list
end UpSort

to pad(num) -- add leading space to single digits
	set thisNum to num as text
	if length of thisNum = 1 then
		set num to space & space & num
	end if
	return num
end pad

Hi

Yes you guessed correctly and, Adam, thanks for your version. I have written, in the distant past, one or two complex applications in dBase and etc but never had any trouble. Now getting old is part of the problem. I started on AS about two months ago - always been a PC man since 1964 I think I got started on Time Share mainframe stuff. Any no more remeniscence - thanks again for your assistance.

Regards Bob

You’re welcome Bobbleissimmo. There’s hope too – I’m 70 and have a two-year head start on you, but I’ve become reasonably proficient.