Script Editor Crashes

I didn’t use applescript since I use Tiger.

I am trying to break a filemaker 8 exported text file into several different files all with the same “.tab” extensions.

This script worked well with Panther and Filemaker tabs but now Apple script editor crashes every time I call the script

In The FileMaker file I have tabs delimiting each field(with or without any text in them) none of the eported fields has any line feeds or carriage returns. Each record ends with a carriage return.

What am’I doing wrong?

Thanks



on run
	open {choose file with prompt "Locate the source file"}
end run

on open the_file
	try
		set the_file to item 1 of the_file as alias
		set target_folder to (choose folder with prompt "Where should I save the files") as string
		set the_paragraphs to paragraphs of (read the_file)
		set old_atid to AppleScript's text item delimiters
		repeat with i from 1 to count of the_paragraphs
			set this_paragraph to (item i of the_paragraphs)
			set AppleScript's text item delimiters to {return}
			set the_base_name to text item 1 of this_paragraph
			set AppleScript's text item delimiters to {""}
			set the_dupe to 0
			set new_name to (target_folder & the_base_name & ".tab")
			repeat
				try
					get new_name as alias
					set the_dupe to the_dupe + 1
					set new_name to (target_folder & the_base_name & "_" & (the_dupe as string) & ".tab")
				on error
					exit repeat
				end try
			end repeat
			my write_to_file(new_name, this_paragraph, false)
		end repeat
		set AppleScript's text item delimiters to old_atid
		beep
		display dialog ((i as string) & " files were saved. (" & ((current date) as string) & ")") buttons {"OK"} default button 1 with icon 1 giving up after 30
	on error the_error
		beep
		try
			my write_to_file((target_folder & the_base_name & "_Error" & "." & the_extension), the_error, false)
		end try
		display dialog the_error buttons {"OK"} default button 1 with icon 0 giving up after 30
		return
	end try
end open

on write_to_file(the_file, the_string, appending)
	set the_file to the_file as string
	try
		set write_file to open for access file the_file with write permission
		if appending = false then set eof of write_file to 0
		write the_string to write_file starting at eof
		close access write_file
	on error
		try
			close access write_file
		end try
	end try
end write_to_file

Model: PowerBook PPC
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

In OS X 10.4.7, the Script Editor is 2.1.1. Is that what you’re running?

I have script Editor 2.1.1

Well, your script opens and runs for me, etnadan, producing an error: that “write_file” is not defined, but it does produce two files on my desktop, one of which for me is empty, but then I’m not feeding it a file it expects to see. I haven’t understood why the file separated into paragraphs, which normally break at returns, needs a further text item delimitation around returns - there shouldn’t be any.

One thing you should definitely change is this bit:


on write_to_file(the_file, the_string, appending)
   set the_file to the_file as string
   try
       set write_file to open for access file the_file with write permission
       if appending = false then set eof of write_file to 0
       write the_string to write_file starting at eof
       close access write_file
   on error
       -- try -- REMOVE THIS
           close access write_file
       -- end try  -- AND THIS
   end try
end write_to_file

Hi.

I agree with Adam about the paragraphs. Why not just set the_base_name to (item i of the_paragraphs)? Do you perhaps mean to set AppleScript’s text item delimiters to {tab} there? That’s what you’d need for tab-delimited fields in a record. If each paragraph is a FileMaker record, you’re trying to use the entire record in the file name. That’s one possible cause of your problem.

Another possibility is that the file you’re reading is Unicode text. (I don’t know anything about FileMaker files.) If it is, it should be read as Unicode text:

set the_paragraphs to paragraphs of (read the_file as Unicode text)

The script does contain one variable that’s used without having previously been set to anything: the_extension. That should be corrected, but I don’t think it would cause Script Editor to “crash”.