Swapping FileMaker Carriage Returns for Standard Ones?

For whatever reason, FileMaker’s carriage return in calculations (the ¶ symbol) yield a funky character instead of a “return” when the resulting data is brought into an editor like BBEdit. I can’t even copy-n-paste it into this message. BBEdit switches it for “\x0B” in the find/replace dialog.

I need an AppleScript way to substitute that funky character for a “regular” carriage return. I want to have FileMaker kick-off the script internally to fix it’s own export. ;p

EDIT: More research indicates the character is a “vertical tab,” or “ASCII code 11.” Need to turn it into a “return” regardless…

Hello

You can use the ASCII character in the Standard Additons to return a character from a string.

Best Regards

McUsr

Getting the character is not the problem, it’s turning it into the right one. :stuck_out_tongue:

Basically, is there an AppleScript way to do a search-n-replace on a text file?

Or is this where I scratch my head and pull out the TID-swapping trick (which always makes my head hurt for some reason)?

Previously, I also got a headache from using AppleScript’s text item delimiters, but that has changed now. Doing text replacements with the text item delimiters is actually quite easy. Here’s an example:

set myFile to (choose file with prompt "Choose a plain txt file")

-- to replace
set toReplace to text returned of (display dialog "Wath text would you like to replace?" default answer "")

-- replace with
set replaceWith to text returned of (display dialog "With what would you like to replace the text?" default answer "")

set myData to (read myFile)

set newText to replaceTextWithText(myData, toReplace, replaceWith)
writeToFile(myFile, newText)


on writeToFile(aFile, theData)
	try
		set OA to open for access aFile with write permission
		write theData to OA starting at 0
		close access OA
	on error
		try
			close access OA
		end try
		return false
	end try
	return true
end writeToFile

on replaceTextWithText(aText, textToReplace, replaceText)
	set tid to AppleScript's text item delimiters
	
	set AppleScript's text item delimiters to textToReplace
	set allItems to every text item of aText
	set AppleScript's text item delimiters to replaceText
	set newText to allItems as text
	
	set AppleScript's text item delimiters to tid
	return newText
end replaceTextWithText

Hope it helps,
ief2

How do I enter “\x0B” as a find string (that character, not the literal string)?

Or maybe “ASCII code 11.”

I’ve got now idea, but you could do it with a Command Line Utility:

property CLUPosix : "path/to/clu"

set myFile to (choose file with prompt "Choose a plain txt file")

set myData to (read myFile)

set newText to replaceVerticalTabWithCariageReturn(myData)
writeToFile(myFile, newText)


on writeToFile(aFile, theData)
	try
		set OA to open for access aFile with write permission
		write theData to OA starting at 0
		close access OA
	on error
		try
			close access OA
		end try
		return false
	end try
	return true
end writeToFile

on replaceVerticalTabWithCariageReturn(sometext)
	set CLU to stringForTerminal(CLUPosix)
	set v to do shell script CLU & " " & quoted form of sometext
	return v
end replaceVerticalTabWithCariageReturn

on stringForTerminal(filename)
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " "
	set allItems to every text item of filename
	set AppleScript's text item delimiters to "\\ "
	set newPath to allItems as text
	
	set AppleScript's text item delimiters to tid
	return newPath
end stringForTerminal

I didn’t test it, so I hope it works,
ief2

Looks like FileMaker freaks when you have handlers in embedded AppleScript. So I modified yours to be “handlerless”. I just needed to figure out how to load the funky character directly to the variable. Quick search of these forums got me the ASCII syntax.

THANKS!

tell application "Finder"
	set myFile to (path to desktop as string) & "filename.txt"
	
	-- to replace
	set toReplace to ASCII character 11
	
	-- replace with
	set replaceWith to return
	
	--load file data
	set myData to (read file myFile)
	
	--swap TIDs for search-n-replace
	set tid to AppleScript's text item delimiters
	
	set AppleScript's text item delimiters to toReplace
	set allItems to every text item of myData
	set AppleScript's text item delimiters to replaceWith
	set newText to allItems as text
	
	set AppleScript's text item delimiters to tid
	
	--write file
	try
		set OA to open for access myFile with write permission
		write newText to OA starting at 0
		close access OA
	on error
		try
			close access OA
		end try
		return false
	end try
	
end tell

Oeps, so it wirks with ASCII character. Guessed it wasn’t that easy. :stuck_out_tongue:

Yeah, I kept trying “ASCII code” and couldn’t find a Dictionary entry…finally a “duh, search the forums” solved that. LOL.

But thank you for the TID refresher. I understand the “why” of it’s operation, it’s not really that hard mentally, but I always fumble and stumble on the execution, like a coding blind spot. :confused:

Hello

I’m sorry for being so terse in my first post. And not being able to revisit it, as I was busy.

I guess you also could have used the unix “tr” command to translate the text in question by something like:

set yourText to quoted form of yourText

set translated to do shell script “set theText=” & yourText & " ; echo $theText |tr \0xB \0xD"

Best Regards

McUsr