Inserting paragraphs of data in text edit

You cannot set tabs in TextEdit with AppleScript

Hi Stefan on indeed anyone

If I wanted to copy the resulting contents in textedit and paste into word where I could set the tabs for 2.5 left tab and 3.75 right tab any idea how I would do so?

Have you seen the two suggestions in your other thread?

With the visualbasic solution I receive the error ambiguous name detected: TmpDDE

I recommend to update to Office 2004 which is incredibly better scriptable :wink:

I’m wondering why the script works on my machine and not on yours

I have the a common problem!
Sorry I was unable to figure out as your examples were too long and a bit complicated :slight_smile:
And I’m new to applescript!

I’m trying to insert new deftext in new paragraph every time the script is set to run.
(deftext will be an input text written by the user)
here is my code

tell application “Finder” to open file “DictXML.txt” of desktop
tell application “TextEdit”
set deftext to “here comes the deftext input code”
set the paragraph of front document to return & deftext & return
end tell

do you have any ideas how to develop it?
PS. Thanks beforehand :slight_smile:

Model: MacBookPro
AppleScript: 2.2.1
Browser: Safari 528.17
Operating System: Mac OS X (10.5)

AppleScript can do this without any target application


set deftext to "here comes the deftext input code"

set targetFile to ((path to desktop as text) & "DictXML.txt")
try
	set ff to open for access file targetFile with write permission
	write deftext & return to ff starting at eof
	close access ff
on error
	try
		close access file targetFile
	end try
	return false
end try

Stefan

Thank you very much!!!
It finally worked!

Stefan do you have time for little collaborative work? I’m trying to make an input program to write the xml code for mac dictionary!
Got the idea? I already elaborated the part of the code. The input program will serve for inputing words and their explanations in xml.
With that tool one can make a free mac dictionary. And it’s very important for little nations and language groups.

I’ll share with you the whole project and the materials I have.

I use Xcode and applescript.

Again lots of thanks!
:smiley:

Model: MacBookPro
AppleScript: 2.2.1
Browser: Safari 528.17
Operating System: Mac OS X (10.5)

The above-described method of writing does not work with Armenian letter characters.
Instead of writing down what was prompted it writes ??? symbols :confused: :frowning:

Is it possible to solve such a problem?
Armenian is on unicode on my machine! I cannot figure out what’s the problem.

Any ideas?

this depends on the text encoding, try UTF-8


.
write deftext & return to ff starting at eof as «class utf8»
.

I would edit these three instructions:

set item i of sharename to item i of sharename as text
set compdet to (item i of sharename) & tab & (item i of the pricelist) & tab & (item i of the difflist)
set text of front of document to “compdet”

as
–set item i of sharename to item i of sharename as text (* you where just setting the nature of the item in the list )
set compdet to (item i of sharename) as text & tab & (item i of the pricelist) & tab & (item i of the difflist) (
this one doesn’t change the contents of the list itself )
set text of front of document to compdet (
no quotes around the variable name *)

Yvan KOENIG (from FRANCE dimanche 31 mai 2009 16:49:31)

Stefan :slight_smile:
Thanks

However, the problem with encoding still persists. Though it now does not convert the characters to ???, it convert the inputed text to an unrecognizable output with an unknown font.
Perhaps something is missing in input encoding or the font class is wrong.

see my code

display dialog “Write down the word” default answer “” buttons {“Next”}
set MyWord to text returned of the result
set bracword to “'” & MyWord & “'”
set deftext to “<d:entry id=” & bracword & " d:title=" & bracword & “>”

set targetFile to ((path to desktop as text) & “DictXML.txt”)
try
set ff to open for access file targetFile with write permission
write deftext & return to ff starting at eof as «class utf8»
close access ff
on error
try
close access file targetFile
end try
return false
end try

– and here is the result: <d:entry id=‘‘¢’°Ã·Ã„և’ d:title=’‘¢’°Ã·Ã„և’>

By the way do you know how I can use double quotes instead of single quotes in line \set bracword to “'” & MyWord & “'”//

Thanks a lot again :slight_smile:

Besides the writing side, there is also the reading side.

You say the result is “<d:entry id=‘‘¢’°Ã·Ã„և’ d:title=’‘¢’°Ã·Ã„և’>”, but what is showing you that result?

In TextEdit, go through “File > Open” (⌘O) and select “Plain Text Encoding:” “Unicode (UTF-8)” at the bottom of the open panel. The viewer/editor you are using is likely guessing the wrong encoding.

When I save the string you quote in MacRoman encoding and re-open it as UTF-8, I get “<d:entry id=‘Õ¢Õ¡Ö€Ö‡’ d:title=‘Õ¢Õ¡Ö€Ö‡’>” (the Unicode names for these characters, as reported by UnicodeChecker, indicate that they are Armenian, so presumably they are what you intended and your viewer is decoding the UTF-8 encoded characters as MacRoman).

Also, if you intend to write actual XML, you will have to make sure that the encoding declaration at the top of the file (if there is one) matches the encoding you are using via write. If there is no encoding specified, then it is probably OK to write UTF-8. But if the XML header specifies some other encoding then you will need to write your text into the file using that encoding instead.

For double quotes, use this:

set bracword to "\"" & MyWord & "\""

Edit History: Added MacRoman as UTF-8 decoding that shows Armenian characters. Added some paragraph breaks. Reworded the Unicode parenthetical. Added info about double quotes.

Chrys,
Thank you very much! :slight_smile:
It’s what I intended to write.

By the way, can anybody show me how to solve the problem with the brackets?

Also, I need that my script run as many times as the button next is pressed. Like “word” → “Next” → “Definition” → “Next” and so on with same chain until quitted.
Is that possible? Or do I need to set the exact rounds?

here is the draft script


– Here comes the word input!

display dialog “Write down the word.” default answer “” buttons {“Next”}
set MyWord to text returned of the result
set bracword to “'” & MyWord & “'”
set deftext to “<d:entry id=” & bracword & " d:title=" & bracword & “>” & "
<d:index d:value=" & bracword & “/>” & "

" & bracword & "

"

set targetFile to ((path to desktop as text) & “DictXML.txt”)
try
set ff to open for access file targetFile with write permission
write deftext & return to ff starting at eof as «class utf8»
close access ff
on error
try
close access file targetFile
end try
return false
end try

– Here come the translation dialog

display dialog “Write down the definition of the word.” default answer “” buttons {“Next”}
set MyTrans to text returned of the result
set bracTrans to “'” & MyTrans & “'”
set transtext to “

” & bracTrans & “

” & "
</d:entry>"

set targetFile to ((path to desktop as text) & “DictXML.txt”)
try
set ff to open for access file targetFile with write permission
write transtext & return to ff starting at eof as «class utf8»
close access ff
on error
try
close access file targetFile
end try
return false
end try


You are all brilliant in this forum! Thank you all very much :slight_smile:

I appreciate your help.
Again many many and many thanks! :smiley:

Do you mean double quotes? I do not remember any description of a problem involving brackets. The way to use double quotes was at the bottom of my previous post.

Please use the


BBCode tags around your script to make it easier for others to load you script (single click instead of select, copy, activate Script Editor, paste). You can insert them around selected AppleScript code (or insert an empty pair) by clicking the button labeled “Applescript” above the message input text box.

Yes it is possible. One way would be to add another button (“Finished”) to the first dialog to let the user indicate that they are finished adding words. Then put the whole thing in a loop and exit the loop if the user presses the new button.

I added a surrounding loop, a “Finished” button to the first dialog, cancel button for the first dialog (allows user to press Escape key instead of clicking Finished button), and default button for both dialogs (allows user to press Return key instead of having to click or TAB+Space):

-- Here comes the word input!

repeat
	set r to display dialog "Write down the word." default answer "" buttons {"Finished", "Next"} default button "Next" cancel button "Finished"
	if button returned of r is "Finished" then exit repeat
	set MyWord to text returned of the r
	set bracword to "'" & MyWord & "'"
	set deftext to "<d:entry id=" & bracword & " d:title=" & bracword & ">" & "
<d:index d:value=" & bracword & "/>" & "
<h1>" & bracword & "</h1>"
	
	set targetFile to ((path to desktop as text) & "DictXML.txt")
	try
		set ff to open for access file targetFile with write permission
		write deftext & return to ff starting at eof as «class utf8»
		close access ff
	on error
		try
			close access file targetFile
		end try
		return false
	end try
	
	
	-- Here come the translation dialog
	
	display dialog "Write down the definition of the word." default answer "" buttons {"Next"} default button "Next"
	set MyTrans to text returned of the result
	set bracTrans to "'" & MyTrans & "'"
	set transtext to "<p>" & bracTrans & "<p>" & "
</d:entry>"
	
	
	set targetFile to ((path to desktop as text) & "DictXML.txt")
	try
		set ff to open for access file targetFile with write permission
		write transtext & return to ff starting at eof as «class utf8»
		close access ff
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end repeat

Beyond that, the program would probably benefit from pulling some of the common code (the write, some of the formatting) out into handlers. That way, for example, with a single edit you could fix the bug that creates malformed XML when the user’s input contains the quote mark you use in the XML. Right now, if the user types wo’rd, the XML would include id=‘wo’rd’, which is not what you really want in the end. Even if you switch to using double quotes, the problem just shifts to when the user types something that contains a double quote.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4 Public Beta (4528.17)
Operating System: Mac OS X (10.4)

Edit History: Added closing applescript tag. Changed pasted to selected to clarify the operation of the Applescript button. Fixed use/user typo.

Again lots of thanks.

About quotes… I was not attentive, sorry. and thanks. it solved the problem.
Also thanks for the loop.

What about buttons. I see you added the cancel button (Finished). Nice idea. I suppose if someone presses “Finished” the inputed word is not saved in the target file. I’ll use it.

And thanks for


tags. :wink: I was just wondering how these guys putted their scripts in such a form :slight_smile:

You are great.

The script is already ready. Now I’ll use it to create the first dictionary. However, one can say that it can bring problems during the use, like when the user writes the word but forgets to write the definition, or when the word is written and the definition “ not. Originally, I was intended to make this in xcode, but as I was a beginner, it was a bit difficult, so I decided to make it via applescript only. So, perhaps, when I move to Xcode, I’ll try to pay attention to every problem that can occur.

P.S. does anybody have the experience of dictionary creation on apple. The source files come with Development tools. :slight_smile: I failed my one-word dictionary creation test.

If I could offer a suggestion:

Rather than importing values from three Excel columns
and then concatenating the values with AppleScript

you might

have the script put a formula in a helper column in the sheet ( =$A1&CHAR(9)&$D1&CHAR(9)&$E1 ),
import that one column to AppleScript
then delete the helper column.
if AppleScript’s text item delimiters are set to {linefeed}, importedList as string would get it all in one swell foop without looping.

Your proposal is interesting. However, if you look to the code in posts that are above, you’ll see that the script is about manual input of individual words.
Yet, it has nothing with excel or any other data program. The language, I’m trying to make a dictionary for, has no such resources to import. First those resources should be created by manual input.

Perhaps, it will be great to consider this idea when moving to Xcode. :slight_smile:

P.S. Mike, have you worked with apple dictionary? Actually I’m having trouble in converting the one-word test-dictionary to see whether the code inserted via applescript works normally.

Lot’s of thanks. :slight_smile:

No, I’ve no experience with Apple Dictionary.
My background is Excel. Scripting Excel is how I’m learning Apple Script.

Nice. :slight_smile:

I’m a beginner as well. This dictionary is the first work I’m enrolled in.