replace character in applescript

Hello

I have a basic script with some display dialogs and creates a text file based upon the inputs. The user puts in numbers only. They can contain comma’s. Problem is that the file is used in a workflow that doesn’t allow comma’s in the numbers, but points.
Al the comma’s should be converted to points. It’s something with text delimeters, but I can not figure out to get it to work or i should write a rule for each variable , but it will make the code lengthy and slow.
Anyone can help?

set NameJob to the text returned of (display dialog "Job name" default answer "job")

set WriteFile to "Macintosh HD:Users:" & (do shell script "whoami") & ":Desktop:" & NameJob & ".ppf"

set PrintWidth to the text returned of (display dialog "paper width" default answer "52")
set PrintHeight to the text returned of (display dialog "Paper height" default answer "36")
set ElementWidth to the text returned of (display dialog "Element width" default answer "")
set ElementHeight to the text returned of (display dialog "Element height" default answer "")
set Row to the text returned of (display dialog "columns" default answer "")
set Column to the text returned of (display dialog "rows" default answer "")
set BlockWidth to ElementWidth * Row
set BlockHeight to ElementHeight * Column
set BlockTrfLeft to the text returned of (display dialog "left margin" default answer "")
set BlockTrfTop to the text returned of (display dialog "Top margin" default answer "")


set PPFFile to "%!PS-Adobe-3.0
%%CIP3-File Version 3.0

CIP3BeginSheet
(Sheet structure of CIP3 example) CIP3Comment
/CIP3AdmJobName (" & NameJob & ") def
/CIP3AdmMake (blank for posting) def
/CIP3AdmModel (PC210) def
/CIP3AdmSoftware (CutCreator) def
/CIP3AdmCreationTime (" & (current date) & ") def
/CIP3AdmArtist (blank for posting) def
/CIP3AdmCopyright (Copyright blank for posting) def
/CIP3AdmPSExtent [" & PrintWidth & " cm " & PrintHeight & " cm] def
/CIP3AdmSheetLay /Left def
/CIP3AdmPrintVolume 120000 def
/CIP3AdmPaperGrammage 130 def

CIP3BeginCutData
/CIP3CutModel (Wohlenberg 92) def
CIP3BeginCutBlock
/CIP3BlockTrf         [1 0 0 1 " & BlockTrfLeft & " cm  " & BlockTrfTop & " cm] def
/CIP3BlockSize        [" & BlockWidth & " cm " & BlockHeight & " cm] def
/CIP3BlockElementSize [" & ElementWidth & " cm " & ElementHeight & " cm] def
/CIP3BlockSubdivision [" & Row & " " & Column & "] def
/CIP3BlockType        /CutBlock def
/CIP3BlockElementType /Unknown def
/CIP3BlockName        (Block 1) def
CIP3EndCutBlock
CIP3EndCutData

CIP3EndSheet
%%CIP3EndOfFile
"
set tmpLogdatei to (open for access file WriteFile with write permission)
write PPFFile starting at eof to tmpLogdatei
close access tmpLogdatei

Hi Mira,

It’s easy to replace commas with decimal points If that’s what you’re trying to do. And you’re right in thinking that you could use text item delimiters.

All you need is a simple search and replace subroutine.

Edited: the only problem that might occur is if you don’t wan’t to replace all periods in the text.

gl,
kel

Hi Mira,

Here’s a search and replace subroutine example that I use:

set the_text to "012345"
ReplaceText(the_text, "123", "abc")
--
on ReplaceText(t, s, r)
	set tids to AppleScript's text item delimiters
	set AppleScript's text item delimiters to s
	try
		set temp_list to text items of t
		set AppleScript's text item delimiters to r
		set new_text to temp_list as string
		set AppleScript's text item delimiters to tids
	on error err_msg
		set AppleScript's text item delimiters to tids
		error err_msg
	end try
	return new_text
end ReplaceText

Edited: actually I should rewrite it. Things have changed and more.

gl,
kel

Great

I have it functional now. Thx
Now trying to put this into an applescript xcode project.

Hi,

first of all, this


set WriteFile to "Macintosh HD:Users:" & (do shell script "whoami") & ":Desktop:" & NameJob & ".ppf"

is exactly the same as


set WriteFile to (path to desktop as text) & NameJob & ".ppf"

even for people whose name of the startup disk is not MacIntosh HD.

Here is a alternative solution without text item delimiters


on convertCommaSeparatorToPoint(numberAsText)
	set o to offset of "," in numberAsText
	if o = 0 then return numberAsText -- no comma found return text unchanged
	tell numberAsText to return text 1 thru (o - 1) & "." & text (o + 1) thru -1
end convertCommaSeparatorToPoint

finally it’s strongly recommended to make the write routine safe to avoid leaked open files


try
	set fileDescriptor to open for access file WriteFile with write permission
	write PPFFile starting at eof to fileDescriptor
	close access fileDescriptor
on error
	try
		close access file WriteFile
	end try
end try