Copying appleworks text

Hi,

I’m new to applescript, and am trying to write a droplet to make textedit files from the contents of appleworks 6 files.

I’m stuck on the FIRST step, so there’s probably something simple i’m missing. (after spending hours in the applescript guide pdf, and searching here)

Problem: I get a “descriptor type mismatch” whenever i try to get the text body of an appleworks document.

on open fileList
	launch "textedit"
	launch "appleworks 6"
			repeat with myFile in fileList
			
			tell application "AppleWorks 6"
				set mytext to text body of document myFile
				activate
				set the clipboard to mytext
-- I get the same error when using "set myText to the text body of document myFile"
			end tell
			
			end repeat

end open

I’ve cut the rest of the script for now, as the problem is in the above! Why would i get a descriptor type mismatch when getting the text body from the appleworks document? Thanks!

Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hello

Using a wrong syntax gives wrong result :wink:

set myFile to "Macintosh HD:Users:yvankoenig:Desktop:myFile.cwk"

tell application "AppleWorks 6"
	activate
	open file myFile
	set the clipboard to (get text body of document 1)
	close document 1
end tell


To be short, you don’t have a document available if you don’t open it.

Yvan KOENIG (from FRANCE lundi 26 février 2007 12:27:22)

Thanks Yvan!

Silly of me to miss the “open” line, and misplace the activate…

For the record - another large problem was that i needed to “open file (myFile as string)” rather than just “open file myFile”.

Hi Julian,

You just need ‘open myFile’ because myFile is a valid reference an alias reference.

Edited: and BTW, if you want to preserve some formatting, you could save as rtf which TextEdit can work with. e.g.


set dp to (path to desktop as string)
tell application "AppleWorks 6"
	activate
	set n to name of front document
	set file_spec to (dp & (text 1 thru -5 of n) & ".rtf") as file specification
	save front document in file_spec using translator "RTF"
end tell

If you don’t like the AppleWorks icon, you could remove the creator type from the file. If you don’t need formatting, you could also save as text. You could also save straight to text after getting the text body without the clipboard and TextEdit.

gl,

Thankyou! Retaining formatting was my next problem. I found some example scripts like yours, and not being overly impressed by appleworks recently, was trying to minimise its use. It does indeed work perfectly though.

With the save command, you can also specify the file type. I’m not sure about this, but I think the file type for rtf should be "RTF ", but AppleWorks set it to “TEXT”. Yvan’s really good at AppleWorks scripting. Maybe He’ll come back and read this. He probably has some scripts already made that converts AppleWorks documents to a more modern format.

I don’t know what you think is wrong with AppleWorks, but it’s a lot better than TextEdit in many areas. I used it a lot before I got Microsoft Office. What are you trying to do?

gl,

I’ve been writing this conversion script primarily for my mother (who has many appleworks files), but also as an excuse to learn a bit of applescript. Hence I’ve been reinventing the wheel rather than just using existing scripts…

My mum has fairly basic requirements of a text editor, and she’s not overly good at computing. Personally, i don’t mind working around appleworks’ quirks, but mum has no chance and gets frustrated easily. The latest gripe was that appleworks was resetting ALL pasted text to a different text style, every time (and not to the default style, either).

She tried out Pages, but it’s fairly slow to use on our computers, and in my opinion will go the way of appleworks, greatworks etc. She didn’t like to use Pages either.

Textedit was the next option, being simple, fast, and having decent open and save type capabilities it seemed appealing. So she’ll be trying that pretty soon…

Hi Julian,

I tried using the ‘as file type’ parameter with the ‘save’ command and it didn’t work.


set f to choose file -- AppleWorks file
set dp to (path to desktop as string)
tell application "AppleWorks 6"
	launch
	activate
	open f
	set n to name of front document
end tell
set file_spec to (dp & n & ".rtf") as file specification
tell application "AppleWorks 6"
	save front document in file_spec as file type "RTF " using translator "RTF"
end tell

The file type of the resulting rtf document is still “TEXT”. The ‘as file type’ in the above script is useless.

The resulting document seems to open fine in TextEdit, but if you want to change file types and creator type, it’s easy to add that to the script.

The AppleWorks pasting bug has been around a long time. It has something to do with the default styles for new documents. I don’t know why, but I never had that bug and all my pasted text is formatted as copied.

I’d go with the convert to rtf to preserve formatting if you’re switching over to TextEdit. You can remove the “.cwk” extension from the file’s name when setting the new name in the file specification. TextEdit just needs the .rtf extension when opening document so file types and creator types is not necessary although, you might want to remove the creator type “BOBO” (AppleWorks) so the file opens in the default editor TextEdit when you double click it.

set f to choose file
tell application “Finder”
set file type of f to "RTF " – or use “???” for default
set creator type of f to “???”
end tell

Instead of using “???” you can use four null characters:

set z to ascii character 0
set null_char to z & z & z & z

and use null_char as your file type and creator instead of “???”.

Note that a file specification is a reference to a file that may or may not exist.

gl,

Hello

Here is the skeleton of what I use to save something in a new AppleWorks 6 file.


set nf to my CreateAWdoc(path to desktop as alias, "essai AW6.cwk", "CWWP")

tell application "AppleWorks 6"
	activate
	save document 1 in nf
 -- close document 1 (* it may be a good idea to close the doc *)
end tell

on CreateAWdoc(F, N, ft)
	(*
f must be an alias to destination folder
N must be the wished name of the file to create
ft is a four digits string defining the file type
"CWWP" -- for Word Processor
"CWSS" -- for SpreadSheet
"CWDR" -- for DRaw
"CWGR" -- for GRaphic
"CWDB" -- for DataBase
*)
	local nf
	tell application "Finder"
		set nf to (make new file at F with properties {name:N, file type:ft, creator type:"BOBO"})
	end tell
	return nf  as alias
end CreateAWdoc

Yvan KOENIG (from FRANCE mercredi 28 février 2007 15:55:44)