Jump to content

Export Field Contents in utf-8


fed

This topic is 2991 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Thanks for the suggestion. I tried it. It looks much better, only the line feeds seem to be hex 0B, rather than the required hex 0A

Any idea how to fix this problem?

Link to comment
Share on other sites

Here is a brute force vanilla AppleScript which writes a UTF-8 file, with Mac carriage returns:


set the_text to cell "Text_" of current record



set my_path to (path to desktop folder from user domain) as text

set the_file to (my_path & "test.txt")



tell application "Finder"

	try

		close access file the_file

	end try

	set my_file to (open for access file the_file with write permission)

	set eof my_file to 0

	set the_text to my replace_chars(the_text, ASCII character 11, ASCII character 13)

	write ((ASCII character 239) & (ASCII character 187) & (ASCII character 191)) to my_file --> not as «class utf8»

	write the_text to my_file as «class utf8»

	close access my_file

end tell



on replace_chars(this_text, search_string, replacement_string)

	set AppleScript's text item delimiters to the search_string

	set the item_list to every text item of this_text

	set AppleScript's text item delimiters to the replacement_string

	set this_text to the item_list as text

	set AppleScript's text item delimiters to ""

	return this_text

end replace_chars

Link to comment
Share on other sites

Thank you for the code.

I do not know how to use applescript, actually. Is this code for using within FMP, or after the export is done?

Fed

Link to comment
Share on other sites

No, if you mean my AppleScript, it does not change the file after export,* it explicitly writes the file as UTF-8, first adding the 2 invisible characters that identify it as UTF-8.

The "find/replace" subroutine is converting the internal FileMaker "returns" (ASCII 11) to regular Mac returns. AppleScript itself is "line ending agnostic" (so it would keep the ASCII 11 unless you change them).

You run AppleScript like this by putting it in a Perform AppleScript FileMaker script step.

The AppleScript hard-codes the field (cell) name as "Text_", and puts a file named "test.txt" on your Desktop. But these things can be changed.

FileMaker runs AppleScript very well (with a few quirks (like the fact that I had to enclose the "write" command within a Finder tell block). But FileMaker no longer includes much documentation (the Help page is so old it's funny, "AppleWorks 6"?). Some of the documentation (without the examples) is here:

http://fmdl.filemaker.com/MISC/fmp10/fp/apple_events_reference_wwe.zip

*Well, it writes to the file twice, once for the 2 invisible characters, again for the real text. So I guess you could say it changes it after "export". But the writes are almost simultaneous. The word "export" does not really apply, as there is no such command in AppleScript itself, so it's hard to say.

Edited by Guest
export
Link to comment
Share on other sites

I've tried exporting (see http://fmforums.com/forum/showtopic.php?tid/211846/fromactivity/myposts/), but it doesn't seem to work for what I need. I need utf-8 format with hex 0A as linefeeds, and I cannot seem to get it by exporting. If I copy, then paste into BBedit, I can save the file just the way I need it.

Link to comment
Share on other sites

I cannot seem to get it to work. Does the field have to have a special name?

I get the error: "Object not found." followed by "Unknown Error: -1728."

Thanks again,

Fed

Edited by Guest
Link to comment
Share on other sites

I changed "Text_" to the name of the field I want to export, and it worked!

The problem, is that the linefeeds are still hex 0D, and I need them to be 0A. How can I achieve this.

Also, The first three bytes (EF BB BF) should be excluded.

Thanks again,

Fed

Link to comment
Share on other sites

Do not double post your questions. If you need to try a different approach, just make it part of the original request. No wonder Barbara and I couldn't figure out what you were trying to do.

I have merged your two topics.

Lee

Link to comment
Share on other sites

I didn't double post. I would like to keep the two topics separate, at least until I get the answer I need for this topic. It is VERY important to me. I am making a billing program, and the final part (exporting to a file in the required format for use on a mainframe) is where I am stuck.

I am almost there, thanks to Søren Dyhr  for all his help.

Please do not merge the two topics until I have my export working properly. I really need it to work.

The other question was about putting information into the clipboard in FMP for use outside FMP. It is for a quite different use.

Link to comment
Share on other sites

While Fenton should take the credit here and not me! Would I like to lead your attention to this:

http://www.macosxhints.com/article.php?story=20081231012753422

...and in particular to this snippet:


set theFileReference to open for access fileToCopy

set theFileContents to read theFileReference as «class utf8»

close access theFileReference

set the clipboard to theFileContents

Which is the oppsite way to handle filemakers eksport ...

--sd

Link to comment
Share on other sites

That's great! Thanks again!

About the file I need to make,

The problem, is that the linefeeds are still hex 0D, and I need them to be 0A. How can I achieve this.

Also, The first three bytes (EF BB BF) should be excluded.

Link to comment
Share on other sites

Have you read up upon both pbpaste and sed here?... when it comes to it would they perhaps be better bets than applescript?

http://www.cs.hmc.edu/tech_docs/qref/sed.html

And the way to use it inside filemaker is brewed over this recipee:

http://www.newcenturydata.com/downloads/text_write.zip

...the piping allows several things to happen simultaneously in every branch of the piping, which is much faster than applescript even when it's compiled.

--sd

Edited by Guest
Link to comment
Share on other sites

Too complicated for me. Thanks anyway.

I almost have it!! I changed some things and now have what I want:

set the_text to cell "Batch" of current record

set my_path to (path to desktop folder from user domain) as text

set the_file to (my_path & "test.txt")

tell application "Finder"

try

close access file the_file

end try

set my_file to (open for access file the_file with write permission)

set eof my_file to 0

set the_text to my replace_chars(the_text, ASCII character 13, ASCII character 10)

write the_text to my_file as «class utf8»

close access my_file

end tell

on replace_chars(this_text, search_string, replacement_string)

set AppleScript's text item delimiters to the search_string

set the item_list to every text item of this_text

set AppleScript's text item delimiters to the replacement_string

set this_text to the item_list as text

set AppleScript's text item delimiters to ""

return this_text

end replace_chars

Link to comment
Share on other sites

Now I need to be able to change the file name. I tried a 'calculated' applescript rather than 'native' (ie using Perform Applescript)

I cannot seem to get it working. I am having problems with the quotation marks.

Any suggestions?

Link to comment
Share on other sites

I think I did it!:)

"set the_text to cell " & Char ( 34 ) & "Batch" & Char ( 34 ) & " of current record" & ¶ &

"" & ¶ &

"set my_path to (path to desktop folder from user domain) as text" & ¶ &

"set the_file to (my_path & " & Char ( 34 ) & $Batch Name & Char ( 34 ) & ")" & ¶ &

"" & ¶ &

"tell application " & Char ( 34 ) & "Finder" & Char ( 34 ) & ¶ &

" try" & ¶ &

" close access file the_file" & ¶ &

" end try" & ¶ &

" set my_file to (open for access file the_file with write permission)" & ¶ &

" set eof my_file to 0" & ¶ &

" set the_text to my replace_chars(the_text, ASCII character 13, ASCII character 10)" & ¶ &

" write the_text to my_file as «class utf8»" & ¶ &

" close access my_file" & ¶ &

"end tell" & ¶ &

"" & ¶ &

"on replace_chars(this_text, search_string, replacement_string)" & ¶ &

" set AppleScript's text item delimiters to the search_string" & ¶ &

" set the item_list to every text item of this_text" & ¶ &

" set AppleScript's text item delimiters to the replacement_string" & ¶ &

" set this_text to the item_list as text" & ¶ &

" set AppleScript's text item delimiters to " & Char ( 34 ) & Char ( 34 ) & ¶ &

" return this_text" & ¶ &

"end replace_chars "

I'm now sending in the file it generated to see if it passes the tech specs.

Link to comment
Share on other sites

Yes, that calculation produces the AppleScript needed. Just a tip. You can use the FileMaker Quote ("text") function instead of Char (34) & "text" & Char(34). Char(34) is fine, just kind of long (and requires FileMaker 10, for you pre-10 readers).

P.S. You can also use """, but that can be awkward.

Link to comment
Share on other sites

  • 6 years later...

This topic is 2991 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.