Help with subroutine

I must admit (and I mentioned it before), I’m pretty bad with subroutines but still trying to improve at it (I mostly rely on script objects and global variables). Anyway, I have the following script I believe could use at least 2 subroutines, I can just make one work. What the script does is get the name of various opened .txt files, make a list out of the names, convert every name to a list of ASCII numbers equivalent and reconverting these names back to the corresponding ASCII character list which is then parsed to a string. I use this method with file names such as “Le Journal de Québec.txt” which contains “é” since Database Events is sometimes struggling with such characters. So here’s my script with descriptive comments, any help is welcome:


--This script correct an issue I get with characters such as "è,é,à " and so on when interacting with Database Events
set my_list to {}
tell application "TextEdit"
	activate
	repeat with this_doc in (every document)
		--get name of all opened .txt files
		set publication_name to name of this_doc
		set liste_numero_ascii to {}
		--call of subroutine A
		ascii_conversion(publication_name, liste_numero_ascii) of me
		set caracteres_ascii_apres to ""
		
		--I would like to transform the following loop to subroutine B
		--This loop will convert the list of ASCII number returned by subroutine A into ASCII character list
		repeat with ce_caractere in liste_numero_ascii
			set caracteres_ascii_apres to caracteres_ascii_apres & (ASCII character ce_caractere)
		end repeat
		--if inside subroutine B, it would return caracteres_ascii_apres
		
		set publication_name to caracteres_ascii_apres
		
		--the newly created ASCII character list gets parsed into a single string
		set publication_name2 to (get characters 1 thru -5 of publication_name) as string
		set my_list to my_list & publication_name2
	end repeat
end tell

--final list of string converted names
my_list

--subroutine A to convert every character in opened .txt file to it's ASCII number
on ascii_conversion(publication_name, liste_numero_ascii)
	set liste_caracteres_avant to every character of publication_name
	repeat with ce_caractere in liste_caracteres_avant
		set this_ascii to ASCII number ce_caractere
		
		set end of liste_numero_ascii to this_ascii
	end repeat
	return liste_numero_ascii
end ascii_conversion

At first, I wish to make two comments :

(1) as ASCII character belongs to the OSAX Standard Events, triggering it in a
tell application “TextEdit”
.
end tell

block issue non fatal errors -10004

So it would be good practice to edit the instruction :

set caracteres_ascii_apres to caracteres_ascii_apres & (ASCII character ce_caractere)

into :

tell current application to set caracteres_ascii_apres to caracteres_ascii_apres & (ASCII character ce_caractere)

(2) ASCII character and ASCII number are two deprecated features.

In Standard Additions dictionary, everybody may read :

ASCII character‚v : Convert a number to a character
ASCII character integer : the code point of the specified character
→ text : the character
This command is deprecated; use ˜character id .'.
see also
ASCII number.
ASCII number‚v : Convert a character to a number
ASCII number text : the character
→ integer : the code point of the specified character
This command is deprecated; use ˜id .'.
see also
ASCII character.

Now, back to the question.

Here is a first answer :



--This script correct an issue I get with characters such as "è,é,à " and so on when interacting with Database Events
set my_list to {}
tell application "TextEdit"
	activate
	repeat with this_doc in (every document)
		--get name of all opened .txt files
		set publication_name to name of this_doc
		--call of subroutine A
		set liste_numero_ascii to ascii_conversion(publication_name, {}) of me
		
		
		--I would like to transform the following loop to subroutine B
		--This loop will convert the list of ASCII number returned by subroutine A into ASCII character list
		
		set caracteres_ascii_apres to my concatenate_ascii_characters(liste_numero_ascii)
		set publication_name to caracteres_ascii_apres
		
		--the newly created ASCII character list gets parsed into a single string
		set publication_name2 to (get characters 1 thru -5 of publication_name) as string
		set my_list to my_list & publication_name2
	end repeat
end tell

--final list of string converted names
my_list

--subroutine A to convert every character in opened .txt file to it's ASCII number
on ascii_conversion(the_name, liste_numeros)
	set liste_caracteres_avant to every character of the_name
	repeat with ce_caractere in liste_caracteres_avant
		set this_ascii to ASCII number ce_caractere
		set end of liste_numeros to this_ascii
	end repeat
	return liste_numeros
end ascii_conversion

# subroutine B
on concatenate_ascii_characters(liste_numeros)
	set caracteres_apres to ""
	repeat with ce_caractere in liste_numeros
		set caracteres_apres to caracteres_apres & (ASCII character ce_caractere)
	end repeat
	return caracteres_apres
end concatenate_ascii_characters

Please study it carefully because I made several changes which are too difficult to explain with my poor English.

When you will have understand the first script, you may study the second one :



--This script correct an issue I get with characters such as "è,é,à " and so on when interacting with Database Events
set my_list to {}
tell application "TextEdit"
	activate
	repeat with this_doc in (every document)
		--get name of all opened .txt files
		name of this_doc
		--call of subroutine A
		my ascii_conversion(result, {})
		--I would like to transform the following loop to subroutine B
		--This loop will convert the list of ASCII number returned by subroutine A into ASCII character list
		my concatenate_ascii_characters(result)
		--the newly created ASCII character list gets parsed into a single string
		set publication_name2 to result as string
		set my_list to my_list & publication_name2
	end repeat
end tell

--final list of string converted names
my_list

--subroutine A to convert every character in opened .txt file to it's ASCII number
on ascii_conversion(the_name, liste_numeros)
	set liste_caracteres_avant to every character of the_name
	repeat with ce_caractere in liste_caracteres_avant
		ASCII number ce_caractere
		set end of liste_numeros to result
	end repeat
	return liste_numeros
end ascii_conversion

# subroutine B
on concatenate_ascii_characters(liste_numeros)
	set caracteres_apres to ""
	set liste_numeros to items 1 thru -5 of liste_numeros # drops the last 4 characters
	repeat with ce_caractere in liste_numeros
		set caracteres_apres to caracteres_apres & (ASCII character ce_caractere)
	end repeat
	return caracteres_apres
end concatenate_ascii_characters

No need to ask the script to do useless tasks.

it may be simplified a bit more :



--This script correct an issue I get with characters such as "è,é,à " and so on when interacting with Database Events
set my_list to {}
tell application "TextEdit"
	activate
	repeat with this_doc in (every document)
		--get name of all opened .txt files
		name of this_doc
		--call of subroutine A
		my ascii_conversion(result, {})
		--I would like to transform the following loop to subroutine B
		--This loop will convert the list of ASCII number returned by subroutine A into ASCII character list
		my concatenate_ascii_characters(result)
		set my_list to my_list & (result as string)
	end repeat
end tell

--final list of string converted names
my_list

--subroutine A to convert every character in opened .txt file to it's ASCII number
on ascii_conversion(the_name, liste_numeros)
	set liste_caracteres_avant to every character of the_name
	repeat with ce_caractere in liste_caracteres_avant
		ASCII number ce_caractere
		set end of liste_numeros to result
	end repeat
	return liste_numeros
end ascii_conversion

# subroutine B
on concatenate_ascii_characters(liste_numeros)
	set caracteres_apres to ""
	set liste_numeros to items 1 thru -5 of liste_numeros # drops the last 4 characters
	repeat with ce_caractere in liste_numeros
		set caracteres_apres to caracteres_apres & (ASCII character ce_caractere)
	end repeat
	return caracteres_apres
end concatenate_ascii_characters

Yvan KOENIG (VALLAURIS, France) mercredi 26 septembre 2012 21:44:13

Yvan, thanks for your quick and very helpful reply. Your script does exactly what I needed. Reste plus maintenant qu’à étudier le tout :slight_smile: