Inserting paragraphs of data in text edit

Hi

I am trying to place paragraphs of data into a new document in textedit.

The script is as follows

tell application "TextEdit"
set text of front document to "blah blah blah" & return & return
repeat with i from 1 to 82
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"
end repeat
end tell

This script replaces the first line again and again with the last item in the compdet list what I want to happen is to have a new paragraph for each new item.

I would really appreciate some help.

Thanks in advance!

Hi sherlock,

It’s a bit difficult for me to help you, as you did not post your complete code, but maybe the following AppleScript code can give you some insights:


set sharenames to {"iCookie", "iTomato", "iBread"}
set curprices to {"1.98", "0.57", "2.23"}
set oldprices to {"1.99", "0.56", "2.33"}

set countsharenames to length of sharenames
set doctext to ""
set doctext to doctext & "blah blah blah" & return & return

repeat with i from 1 to countsharenames
	set sharename to (item i of sharenames) as Unicode text
	set curprice to (item i of curprices) as Unicode text
	set oldprice to (item i of oldprices) as Unicode text
	set compdet to sharename & tab & curprice & tab & oldprice
	if i is not equal to countsharenames then
		set doctext to doctext & compdet & return
	else
		set doctext to doctext & compdet
	end if
end repeat

tell application "TextEdit"
	activate
	make new document
	set text of front document to doctext
end tell

Hi Martin

Here is my complete script

try
	tell application "Microsoft Excel"
		Quit
	end tell
end try
tell application "Microsoft Excel"
	set CompanyBackup to "Macintosh HD:Users:editorial_user:Desktop:New Company Backup"
	Open CompanyBackup
end tell
tell application "Microsoft Excel"
	set sharename to Value of Range "r1c2:r82c2"
	-- Close Workbook 1
end tell
tell application "Finder"
	set thefile to choose file with prompt "Please select todays shares file"
	tell application "Microsoft Excel"
		set thefile to thefile as string
		tell Application "Finder"
			select file thefile
			open selection
		end tell
		Delete Row 1
		Delete Row 1
		Delete Row 1
		set thepricelist to Value of Range "r1c4:r82c4"
		set thedifflist to Value of Range "r1c3:r82c3"
		Close Workbook 1 saving No
		Quit
	end tell
	repeat with i from 1 to 82
		set item i of thepricelist to item i of thepricelist as number
		set item i of thepricelist to (round ((item i of thepricelist) * 10)) / 10
		set item i of thedifflist to item i of thedifflist as number
		if item i of thedifflist > 0 then
			set item i of thedifflist to "+" & item i of thedifflist
		end if
	end repeat
end tell
tell application "TextEdit"
set text of front document to "blah blah blah" & return & return
repeat with i from 1 to 82
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"
end repeat
end tell
tell application "Finder"
	display dialog "Share Text Completed - save document as word 4 format" buttons "OK" default button 1
end tell

The problem arises from the fact that the variables I have set as sharename, pricelist and the thedifflist are lists of data that I have stripped out of excel. I really need for the repeat to add each line one at a time. Any help would be appreciated.

try this


.
	if item i of thedifflist > 0 then
		set item i of thedifflist to "+" & item i of thedifflist
	end if
end repeat

set theText to "blah blah blah" & return & return
repeat with i from 1 to 82
	set compDet to (item i of sharename as text) & tab & (item i of the thepricelist) & tab & (item i of the thedifflist)
	set theText to theText & compDet & return & return
end repeat


tell application "TextEdit"
	activate
	if not (exists document 1) then make new document
	set text of document 1 to theText
end tell

tell application "Finder"
.

You are a scripting genius Stefan my script is now working my very last request is to ask how to set tabs in textedit?

Can anyone help in setting tabs in textedit?

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: