Need help updating an Applescript to OSX

I have a small applescript that was written for me several years ago. It basically takes a text file (in the finder) and strips out some extraneous information, and writes a new text file with the remaining info. It won’t work in OSX, and I need it to… and the guy who wrote it is no longer available. I am hoping to find someone here who can fix it for me. No idea how much work it might be, but I assume it is fairly simple. He annotated it, so I believe it is pretty obvious how it is supposed to work. If you might be able to help me (and I am willing to pay a modest fee for the help) please PM me or post here, and I will respond to you. Thanks.

Post the script here and someone should be able to help you…

Here is the script:


(* This routine gets executed when a file gets dropped on this application *)
on open theFiles
(* This is the loop that repeats once for each file you drop on application *)
repeat with fileOriginal in theFiles
(* Finder handles file i/o *)
tell application "Finder"
(* Finder Select on the chosen file *)
select file fileOriginal
(* Get folder location so we can write output to same folder *)
set ourLocation to the folder of selection
(* Build appendage to filename for output file *)
set appendage to ".x"
(* Initialize number to append to filename if already exists *)
set i to 1
(* Initialize flag that output file created to false *)
set resultCreated to false
(* This loop keeps trying to create a unique output file *)
repeat until resultCreated
(* Build name (including path) of our output file - folder+filename+"(result)"*)
set thisTry to (ourLocation as text) & the name of selection & appendage
(* Name already exists, append number to end of name and try again *)
if (exists file thisTry) then
set i to (i + 1)
set appendage to "(result-try#" & (i as text) & ")"
(* Found unique name - create our output file and exit this loop *)
else
set fileResult to make file at ourLocation with properties {name:the name of selection & appendage}
set resultCreated to true
end if
end repeat
(* Here we open input file for read access *)
set inputFile to open for access selection
(* Here we open output file for write access *)
set outputFile to open for access fileResult with write permission
end tell
(* Initialize variables to be used in our read through the file loop *)
set readFinished to false
set saveLine to 0
set j to 0
(* Our read through the file loop *)
repeat until readFinished
(* J is a record counter - one record is a line of text ended by a carriage return *)
set j to j + 1
(* Read next record of input file *)
try
set thisRecord to read inputFile until return
(* End of file yet? *)
on error
set readFinished to true
end try
(* We only write to output file if the read of the input file was successful *)
if readFinished is false then
(* Extract out the first character of the current record *)
set firstChar to the first character of thisRecord
(* Call function to determine whether this first character is numeric or not *)
if numericCharacter(firstChar) then
(* Increment count of lines written to output file *)
set saveLine to saveLine + 1
(* Write this numerically initiated record to output file *)
write thisRecord to outputFile
end if
end if
end repeat
(* Clean up *)
close access inputFile
close access outputFile
(* Keep user informed of how many lines written and to what filename *)
display dialog (saveLine as text) & " lines written to " & the name of fileResult
end repeat
end open

on numericCharacter(charToview)
if charToview ≥ "0" and charToview ≤ "9" then
return true
else
return false
end if
end numericCharacter

thanks…

Even before OS X, it was considered bad form to use the Finder selection unnecessarily. But in X itself, the selection is a list, not a reference, so it’s not possible to use it in the same way anyway.

The rewrite below works in Jaguar and Tiger, but may prove to be unsuitable in the filename-extension department. It looks as though your “original” files didn’t have name extensions when the script was written. If they’re now being produced by an OS X version of some software, it’s almost certain that they will have name extensions and that these extensions will need to be preserved at the end of the doctored names. This isn’t hard to do, but until I know more, I’ve left the old naming scheme in place. It’s also possible that the lines in OS X files will end with line feeds (ASCII character 10) instead of returns.

(* This routine gets executed when a file gets dropped on this application *)
on open theFiles
	(* This is the loop that repeats once for each file you drop on application *)
	repeat with fileOriginal in theFiles
		(* Finder gets useful information about the file and its folder *)
		tell application "Finder"
			set originalName to name of fileOriginal
			set ourLocation to folder of fileOriginal
			set takenNames to name of files of ourLocation
		end tell
		
		(* Create a unique output file name *)
		set thisTry to originalName & ".x"
		set i to 0
		repeat until (thisTry is not in takenNames)
			set i to i + 1
			set thisTry to originalName & "(result-try#" & i & ")"
		end repeat
		
		(* Here we open input file for read access *)
		set inputFile to open for access fileOriginal
		(* Here we open output file for write access *)
		set outputFile to open for access file ((ourLocation as Unicode text) & thisTry) with write permission
		
		(* Initialize variables to be used in our read through the file loop *)
		set saveLine to 0
		set j to 0 -- j is incremented below, but never used!?
		(* Our read through the file loop *)
		considering case
			repeat
				(* J is a record counter - one record is a line of text ended by a carriage return *)
				set j to j + 1
				(* Read next record of input file *)
				try
					set thisRecord to read inputFile until return
					(* We only write to output file if the read of the input file was successful
				and the record begins with a numeric character *)
					if (the first character of thisRecord is in "0123456789") then
						(* Increment count of lines written to output file *)
						set saveLine to saveLine + 1
						(* Write this numerically initiated record to output file *)
						write thisRecord to outputFile
					end if
					(* End of file yet? *)
				on error
					exit repeat
				end try
			end repeat
		end considering
		
		(* Clean up *)
		close access inputFile
		close access outputFile
		(* Keep user informed of how many lines written and to what filename *)
		display dialog (saveLine as Unicode text) & " lines written to " & thisTry
	end repeat
end open

Nigel is a gentleman and a scholar! He quite graciously helped me to solve my problem.!