New to AppleScript - problem debugging

Hello everyone,

First post here, first day with applescript and first month with a mac :smiley:

Whatever, I’ve been trying to learn all day something about applescripts, still I can’t find the error in this simple script.


tell application "TextEdit"
	make new document at beginning with properties {name:"New Report"}
end tell

According to the AppleScript Language Guide, this should open a new document.


tell application "AppleWorks"
	make new document at beginning with properties {name:"New Report"}
end tell

I’m just replacing “AppleWorks” with “TextEdit”. I don’t think this will have any implications, acording to the Script Editor Dictionary, those commands and classes are available for TextEdit.

Any help is truly appreciated. :wink:

Every application accepts its own parameters and adopts its own behaviours. The syntax you use in an application, could not work in the next one (as you see). Running your code, under Panther (10.3.x), I don’t receive errors.
However, seems that the “name” property of documents is associated to its “file path” (the name of the document is the name of the file, or “untitled” document). You can’t create such name from scratch. Other properties can be scripted, though:

tell application "TextEdit"
	make new document at beginning with properties {text:"kaka"}
end tell

Hi,

Also, with TextEdit it’s different than with Carbon apps like AppleWorks. With TextEdit, there is no document in a new document until you save, but you can change the window name:

tell application “TextEdit”
activate
make document at front
set name of front window to “hello”
end tell

This doesn’t help much, but when saving you can get the window name and save using that name.

gl,

Thank you both jj and kel, that was helpful.

OK, more questions. :?:

I thought that what I was trying to do with the code I showed in my first post (this thread), was to create a new document and the window was going to be called “New Document”, but this doesn’t happened. As you said jj, it runs on my system too (10.3.2) but doesn’t do what I though it supposed to do, but I guess I was wrong from the start.

What it actually does is just opens two windows one called “Untitled.txt” and a second one “Untitled 2.txt”, not exactly sure why it overrides the property name and furthermore why is opening a second one. Maybe I have a missconception as well on the “name” property concept. Is not suppose to give the name to the document there, and in consecuence to the window and to the file?

kel, your code basically does what I aimed to do. Still I have a question, what is “activate” doing? it seem to work for me with or without.

Well I’ll will continue posting more questions later.

Cheers, Max.

I’m not sure what you are trying to accomplish but it is a common misconception of folks just learning AppleScript that you need to target TextEdit (or any application) in order to create a file. This isn’t the case. Part of vanilla AppleScript is the Standard Additions Scripting Addition that has file i/o commands that allow you to read & write data to a file without using any other application.

Jon

Well maybe that’s (now) the difficult part, trying to visualize exactly what I want to accomplish. I got stuck into trying to learn AppleScript and I forgot about my main goal. But now that you said that, maybe is a good time to go back to check it, comment it with you guys and take advantage of your wisdom, so you can tell me if what I want to do is convenient to do it with AppleScript or maybe I’m choosing the wrong tool.

What I’m trying to do is to create a file (text), which will be a script for another program (gnuplot) to generate graphics. Basically my problem is:

Data Files → Gnuplot → Graphics → Latex

Lots of data files, to generate graphics and then paste them into latex. So obviously I don’t want to do that “by hand” anymore. I was thinking that first I should generate a script to send to gnuplot to generate the graphics and then to generate the latex code (this last part is not very important, yet).

So that’s why I thought that I should be learning to manipulate a text file using TextEdit, but as you were pointing out Jon, maybe I should not stick to an application but instead manipulate the file using AppleScript commands.

Cheers, Max.

Now that you’ve made it a bit more clear what you want to do, by all means, do not use TextEdit. This (basic) code can be used for writing text to a file (the file can be any Mac formatted path such as “path:to:file”):

set the_file to (((path to desktop) as string) & "test.txt")
set the_contents to "This is some text."
my write_to_file(the_file, the_contents, false)

--to read the string back in:
set read_contents to read file the_file

on write_to_file(the_file, the_contents, append_contents)
	try
		set the_file to open for access file the_file with write permission
		if append_contents = true then set eof of the_file to 0
		write (the_contents) to the_file starting at eof
		close access the_file
	on error
		try
			close access the_file
		end try
	end try
end write_to_file

Also, you can write other data (such as lists, records, picture data, etc.) to a file. Take a look at this post to Code Exchange for more information.

Jon

It’s been a while, but I just wanted to thank you guys.

Jon, your post was really helpful. I decided to work directly with the file, as you suggested, and I reckon this way is much more easy and faster than using an application as an intermediary.

I’m struggling with some other things by now, but I’m progressing :slight_smile:

Thank you.