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…
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
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
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
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.