Convert Text

I am programming a Cash Register and need to convert Letters and/or characters to the code.

I borrowed some code from this post
http://macscripter.net/viewtopic.php?id=28547

but can’t get it to display a dialog of the converted text.
It seems like a lot of code to do what I am trying to do.



global subject
global theText
set abc to {"¢", " ", "!", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} -- as list
set num to (count of items in abc) as text

set SAM4s to {"(028)", "(032)", "(033)", "(035)", "(036)", "(037)", "(038)", "(039)", "(040)", "(041)", "(042)", "(043)", "(044)", "(045)", "(046)", "(047)", "(048)", "(049)", "(050)", "(051)", "(052)", "(053)", "(054)", "(055)", "(056)", "(057)", "(058)", "(059)", "(060)", "(061)", "(062)", "(063)", "(064)", "(065)", "(066)", "(067)", "(068)", "(069)", "(070)", "(071)", "(072)", "(073)", "(074)", "(075)", "(076)", "(077)", "(078)", "(079)", "(080)", "(081)", "(082)", "(083)", "(084)", "(085)", "(086)", "(087)", "(088)", "(089)", "(090)"}
set sam to (count of items in SAM4s) as text
set theText to text returned of (display dialog "Enter in text to get Code" default answer "GROCERY" buttons {"Cancel", "Convert"} default button 2) as string



tell me to set newText to str_ireplace(abc, SAM4s, theText)
-- or: set newText to my str_ireplace(abc, SAM4s, theText)

--  DIALOG NOT WORKING
display dialog theText & return & subject




on str_ireplace(search, replace, subject)
	considering case
		local search, replace, subject, ASTID, returnList, searchCount, thisSubject, i, n
		
		set ASTID to AppleScript's text item delimiters
		set returnList to true
		
		-- This wouldn't make sense
		if search's class is not list and replace's class is list then return subject
		
		-- Make one-item lists if needed	
		if search's class is not list then set search to {search}
		if subject's class is not list then set {subject, returnList} to {{subject}, false}
		
		set searchCount to count search
		get count subject
		
		try
			repeat with i from 1 to result
				set thisSubject to subject's item i
				
				repeat with n from 1 to searchCount
					set AppleScript's text item delimiters to search's item n
					set thisSubject to thisSubject's text items
					
					if replace's class is list then
						try
							get replace's item n
						on error
							get "" -- `replace` ran out of items
						end try
					else
						get replace
					end if
					
					set AppleScript's text item delimiters to {result}
					set thisSubject to "" & thisSubject
				end repeat
				
				set subject's item i to thisSubject
			end repeat
		end try
		
		set AppleScript's text item delimiters to ASTID
		if not returnList then set subject to subject's first item
		return subject
	end considering
	
	
end str_ireplace



darock

Well put this together and it looks better but still don’t work.


set abc to {"¢", " ", "!", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} as string
--  set num to (count of items in abc) as text

set SAM4s to {"028", "033", "035", "036", "037", "038", "039", "040", "041", "042", "043", "044", "045", "046", "047", "048", "049", "050", "051", "052", "053", "054", "055", "056", "057", "058", "059", "060", "061", "062", "063", "064", "065", "066", "067", "068", "069", "070", "071", "072", "073", "074", "075", "076", "077", "078", "079", "080", "081", "082", "083", "084", "085", "086", "087", "088", "089", "090"} as string
--   set sam to (count of items in SAM4s) as text
--  set theText to text returned of (display dialog "Enter in text to get Code" default answer "GROCERY" buttons {"Cancel", "Convert"} default button 2) as string

set mytext to text returned of (display dialog "Enter your Text" default answer "DOIT")

repeat with i in (every character in mytext)
	
	set myCode to offset of i in abc
	
end repeat

not returning all characters

I know this should be easy but must be to tired to figure it out.

darock

why not


set mytext to text returned of (display dialog "Enter your Text" default answer "DOIT")
set newtext to ""
repeat with i in mytext
	set newtext to newtext & "0" & (ASCII number i)
end repeat
newtext