You are not logged in.
I am working on this script which I eventually like to turn into a droplet. All I want to do is is have it convert AppleWorks documents to Microsoft Word format.
I have managed to mess it up... I have even managed to get versions of this script to crash AppleWorks -- yeah I know it isn't the most stable application anymore...
Anyway, anyone want to give me a hand? Here's what I have:
property theTranslator : "Word Windows 97, 2000, XP 2002 "
property desktopPath : path to desktop as text
set currentFile to choose file
tell application "Finder"
if kind of currentFile is not folder then
if file type of currentFile is "CWWP" then
set currentPath to currentFile as text -- AW6 wants a path, not an alias
try
tell application "AppleWorks 6"
activate
open file currentPath
save front document in file desktopPath using translator theTranslator
close front document without saving
set theCounter to theCounter + 1
end tell
on error theError
display dialog theError
end try
end if
end if
end tell
-------------------------
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
Offline
Hi,
The main error is the file you're saving into. By AppleWorks dictionary, you want a file to save to and not a folder. You used the path to the desktop as the path to your file.
property theTranslator : "Word Windows 97, 2000, XP 2002 "
property desktopPath : path to desktop as text
set file_spec to (desktopPath & "MyWord.doc") as file specification -- made a file specification
set currentFile to choose file
tell application "Finder"
if kind of currentFile is not folder then
if file type of currentFile is "CWWP" then
-- set currentPath to currentFile as text -- AW6 wants a path, not an alias -- alias ref is ok
try
set theCounter to 0 -- initialized variable theCounter
tell application "AppleWorks 6"
activate
open currentFile -- alias reference is ok
save front document in file_spec using translator theTranslator -- fixed the reference
close front document without saving
set theCounter to theCounter + 1
end tell
on error theError
display dialog theError
end try
end if
end if
end tell
A fiile specification is a reference to a file that may not already exist.
gl,
Offline
Thanks! That did the trick.
Time to go back and re-read the section about file specification and aliases to get them straight in my head.
I will post an updated script for anyone who might be interested, once I clean up the script a bit more...
--Tom
Offline
You're welcome. Glad it worked for you.
gl,
Offline
OK, here's the first version that I am satisfied with...
I would welcome any constructive criticism anyone might have!
--Tom
-- AW6 to MS Word Converter
-- By Tom McKenna
-- May 23, 2004
--
-- This droplet converts AppleWorks word processing documents that are dropped on it
-- into Microsoft Word documents.
property theTranslator : "Word Windows 97, 2000, XP 2002 "
property theExtension : ".doc"
on open droppedFiles
-- Step One: Check and see if the computer has the latest version of AppleWorks
tell application "AppleWorks 6" to set awVersion to its version as string
if awVersion is not "6.2.9" then
display dialog "Go to http://www.apple.com/appleworks and download the latest version of AppleWorks 6 and then re-run this script." buttons {"Cancel"}
end if
-- Step Two: Create a 'Converted Items' folder on the desktop, if it's not already there
try
tell application "Finder"
make new folder in the desktop with properties {name:"Converted Items"}
set targetFolder to it as text
end tell
on error -- 'Converted Items' exists on the desktop
display dialog "There is already a Converted Items folder on the desktop. Do you want to continue?" buttons {"Cancel", "OK"} default button 2
set targetFolder to ((path to desktop as text) & ("Converted Items:" as text))
end try
-- Step Three: Reset the counter
set theCounter to 0
-- Step Four: The main loop
repeat with currentFile in droppedFiles
-- Step Four A: Determine if the file is an AW6 word processing document
tell application "Finder"
if file type of currentFile is "CWWP" then
set currentInfo to info for currentFile
set currentName to the name of currentInfo
-- Step Four B: Check to see if the file has a .cwk extension and if need be, remove it
if currentName ends with ".cwk" then
set nameCount to count of characters in currentName
set currentName to ¬
(characters 1 through (nameCount - 4) of currentName as string)
end if
-- Step Four C: Open and save file using AppleScript 6
set currentPath to currentFile as text -- AW6 wants a file reference to save
try
tell application "AppleWorks 6"
activate
open file currentPath
set saveFile to (targetFolder & currentName & theExtension) as string
save front document in file saveFile using translator theTranslator
close front document without saving
end tell
-- Step Four D: Increment the counter
set theCounter to theCounter + 1
on error theError
display dialog theError
end try
end if
end tell
end repeat
-- Step Five: Quit AppleWorks and provide a dialog notifying the user of how many documents were created
tell application "AppleWorks 6" to quit
display dialog "Congratulations! " & (theCounter as string) & " files have been converted from AppleWorks 6 file format to Microsoft Word file format." buttons {"OK"} default button 1
end open
-------------------------
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
Offline
Heylo Tom,
After spending many days working on a similar project I finally stumbled across your script. I took the liberty of rewriting several sections to fit my needs. If time permits I will go back through and add the wonderful comments/explanations you had in your script. But thought I would share my changes. I didn't set mine up as a droplet, but I think that could be easily modified. I learned a lot while writing this and will definitely spend more time visiting this forum.
Thanks and Have A Great Day.
Rick Davis
Applescript:
-- Original code written by:
-- AW6 to MS Word Converter
-- By Tom McKenna
-- May 23, 2004
property theSprTranslator : "Excel Win 97, 2000, XP 2002 spr"
property theTranslator : "Word Windows 97, 2000, XP 2002 "
on run
tell application "Finder"
activate
set theFiles to choose folder
if exists (folder "Converted Word Docs" of theFiles) then
display dialog "There is already a Converted Word Docs folder. Please move this folder to another location and try again." buttons {"Cancel"} default button 1 with icon note
error number -128
return
else
make new folder at theFiles with properties {name:"Converted Word Docs"}
select (items of theFiles whose kind is not "folder" and file type is "CWWP")
copy selection to folder "Converted Word Docs" of theFiles
end if
end tell
try
tell application "Finder"
activate
set newFiles to folder "Converted Word Docs" of theFiles
repeat with newFile in newFiles
if kind of newFile is not equal to "Folder" then
set FileName to (name of newFile) as text
set Filenamelength to length of FileName
set Suffix to ".doc"
if file type of newFile is "CWWP" then
-- set currentPath to currentFile as text -- AW6 wants a path, not an alias -- alias ref is ok
if FileName ends with ".cwk" then
set nameCount to count of characters in FileName
set FileName to ¬
(characters 1 through (nameCount - 4) of FileName as string)
end if
if Filenamelength > 27 then
set TopLimit to (Filenamelength + (27 - Filenamelength))
copy characters 1 through TopLimit of FileName as string to NewFilename
set NewFilename to NewFilename & Suffix
else
set NewFilename to FileName & Suffix
end if
try
set the name of newFile to NewFilename
on error
set name of newFile to the text returned of (display dialog "Truncating this file name will create a duplicate file name, which will cause an error and halt the process. Please rename this file to a unique name. If the file name you enter here is already in use the operation will end and you will need to check for any files not converted and try again." default answer NewFilename)
end try
end if
try
tell application "AppleWorks 6"
activate
open newFile -- alias reference is ok
save using translator theTranslator with NewFilename and replace
close front document without saving
end tell
on error theError
display dialog theError
end try
else
(display dialog "This is not an Appleworks document and will not be converted")
end if
tell application "Finder"
set the file type of newFile to "W8BN"
set the creator type of newFile to "MSWD"
end tell
end repeat
end tell
end try
tell application "Finder"
if exists (folder "Converted Spreadsheets" of theFiles) then
display dialog "There is already a Converted Spreadsheets folder. Please move this folder to another location and try again." buttons {"Cancel"} default button 1 with icon note
error number -128
return
else
make new folder at theFiles with properties {name:"Converted Spreadsheets"}
select (items of theFiles whose kind is not "folder" and file type is "CWSS")
copy selection to folder "Converted Spreadsheets" of theFiles
end if
end tell
try
tell application "Finder"
activate
set newsprFiles to folder "Converted Spreadsheets" of theFiles
repeat with newsprFile in newsprFiles
if kind of newsprFile is not equal to "Folder" then
set FileName to (name of newsprFile) as text
set Filenamelength to length of FileName
set Suffix to ".xls"
if file type of newsprFile is "CWSS" then
-- set currentPath to currentFile as text -- AW6 wants a path, not an alias -- alias ref is ok
if FileName ends with ".cwk" then
set nameCount to count of characters in FileName
set FileName to ¬
(characters 1 through (nameCount - 4) of FileName as string)
end if
if Filenamelength > 27 then
set TopLimit to (Filenamelength + (27 - Filenamelength))
copy characters 1 through TopLimit of FileName as string to NewFilename
set NewFilename to NewFilename & Suffix
else
set NewFilename to FileName & Suffix
end if
try
set the name of newsprFile to NewFilename
on error
set name of newsprFile to the text returned of (display dialog "Truncating this file name will create a duplicate file name, which will cause an error and halt the process. Please rename this file to a unique name. If the file name you enter here is already in use the operation will end and you will need to check for any files not converted and try again." default answer NewFilename)
tell application "Finder"
activate
end tell
end try
end if
try
tell application "AppleWorks 6"
activate
open newsprFile -- alias reference is ok
save using translator theSprTranslator with NewFilename and replace
close front document without saving
end tell
on error theError
display dialog theError
end try
else
tell current application
activate
(display dialog "This is not an Appleworks document and will not be converted")
end tell
end if
tell application "Finder"
set the file type of newsprFile to "XLS8"
set the creator type of newsprFile to "XCEL"
end tell
end repeat
end tell
end try
tell current application
activate
display dialog "Congratulations! Your files have been converted from AppleWorks 6 format to Microsoft Word and Excel for Windoze format." buttons {"OK"} default button 1
end tell
end run
Offline
Call me anal retentive, but here's the latest and greatest version of my script...
-- AW6 to MS Word Converter
-- By Tom McKenna
-- May 23, 2004
--
-- This droplet converts AppleWorks word processing documents that are dropped on it
-- into Microsoft Word documents.
property theTranslator : "Word Windows 97, 2000, XP 2002 "
property theExtension : ".doc"
-- set droppedFiles to choose file -- for debugging
on open droppedFiles
-- Step One: Check and see if the computer has the latest version of AppleWorks
tell application "AppleWorks 6" to set awVersion to its version as string
if awVersion is not "6.2.9" then
display dialog "Go to http://www.apple.com/appleworks and download the latest version of AppleWorks 6 and then re-run this script." buttons {"Cancel"}
end if
-- Step Two: Check to see if a 'Converted Items' folder exists, if not -- then create it
try
set targetFolder to ((path to desktop folder as text) & "Converted Items:") as text
get targetFolder as alias
display dialog "There is already a Converted Items folder on the desktop. Do you want to continue?" buttons {"Cancel", "OK"} default button 2
on error
tell application "Finder"
make new folder at desktop with properties {name:"Converted Items"}
set targetFolder to ((path to desktop folder as text) & "Converted Items:") as text
end tell
end try
-- Step Three: Reset the counter
set theCounter to 0
-- Step Four: The main loop
repeat with currentFile in droppedFiles
-- Step Four A: Determine if the file is an AW6 word processing document
tell application "Finder"
if file type of currentFile is "CWWP" then
set currentInfo to info for currentFile
set currentName to the name of currentInfo
-- Step Four B: Check to see if the file has a .cwk extension and if need be, remove it
if currentName ends with ".cwk" then
set nameCount to count of characters in currentName
set currentName to ¬
(characters 1 through (nameCount - 4) of currentName as string)
end if
-- Step Four C: Open and save file using AppleScript 6
set currentPath to currentFile as text -- AW6 wants a file reference to save
try
tell application "AppleWorks 6"
activate
open file currentPath
set saveFile to (targetFolder & currentName & theExtension) as string
save front document in file saveFile using translator theTranslator
close front document without saving
end tell
-- Step Four D: Increment the counter
set theCounter to theCounter + 1
on error theError
display dialog theError
end try
end if
end tell
end repeat
-- Step Five: Quit AppleWorks and provide a dialog notifying the user of how many documents were created
tell application "AppleWorks 6" to quit
display dialog "Congratulations! " & (theCounter as string) & " file(s) have been converted from AppleWorks 6 file format to Microsoft Word file format." buttons {"OK"} default button 1
end open
-------------------------
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
Offline
Hi tmckenna,
Your script looks pretty good to me.
Here's a few things you might try:
When you get some text from a string, instead of getting 'characters' use text. For example:
set the_string to "somefilename.ext"
set dot_offset to (offset of "." in the_string) - 1
set the_name to (text 1 thru dot_offset of the_string)
When you use 'charaters of ...', you get a list of characters and then you need to coerce to string. This way you just get a range of text.
Try to avoid nested tell blocks and using scripting addition commands in application tell blocks if possible. Firstly, you might get conflicts with key words. Another, if your wording is not exact for an inner tell block, the script will look for the next target and might use the dictionary of the outer tell block. So, your syntax check might come out ok but it won't work as expected. For instance, after a quick glance at your script, it looks like you can do the 'info for' command outside the Finder tell block and the AppleWorks tell block looks like it doesn't need to be inside the Finder tell block. Doing this may increase speed also, as the Finder is not needed there anyway.
Edit: I just looked through the script again and found that the Finder is not needed in the repeat loop. The 'info for' command returns a record with a lot of information.
Edit: more stuff. 'path to dektop' is used more than once. I like to put this in a variable. It helps to not crunch statements also for debugging especially when starting off. This section:
tell application "Finder"
make new folder at desktop with properties {name:"Converted Items"}
set targetFolder to ((path to desktop folder as text) & "Converted Items:") as text
end tell
In OSX, Finder 'make' command returns a reference to the item. So, the second line is not needed.
tell application "Finder"
set targetFolder to (make new folder at desktop with properties {name:"Converted Items"}) -- as text -- if a reference is not needed later
end tell
If you're using a reference to the made folder later, then coerce to alias (as alias). This is good if you're using the reference out of the Finder as the Finder returns a Finder reference.
Good Job,
Offline
I found this thread an excellent solution to my problem. I have a CD full of Appleworks documents to convert to .doc files. The Script shown (the last one) worked most of the time and saved hours. But - the times it didn't work have me puzzled. (and perhaps this is more of an AppleWorks question). Some of the files have a .CWK instead of .cwk (upper case not lower case) Other than that, I don't see any difference. These .CWKs will not convert using the script - they are totally ignored. If I open them in AppleWorks, I can individually save them as Word docs, but I still have hundreds to go and if anyone knows how to overcome the blockage, I would truly appreciate the help. I'm at a total disadvantage as I don't know either AppleWorks or AppleScript. (I'm translating a client's old files for them - they switched from Mac to Windows and are now using Access and Word) Thanks for any help!
Offline
well, the hackish way i can think of fixing that is doing this:
find the part of the script where it says
-- Step Four B: Check to see if the file has a .cwk extension and if need be, remove it
if currentName ends with ".cwk" then
set nameCount to count of characters in currentName
set currentName to ¬
(characters 1 through (nameCount - 4) of currentName as string)
end if
add this below that line:
-- Step Four B Part Two: Check to see if the file has a .cwk extension and if need be, remove it
if currentName ends with ".CWK" then
set nameCount to count of characters in currentName
set currentName to ¬
(characters 1 through (nameCount - 4) of currentName as string)
end if
i'm pretty sure there is a better way to do that, but i think that will do it just fine. i wish i could test it, but i don't have appleworks on this comp.
Offline
Thanks - I tried your method and previous to posting my question, I had tried to change the existing code by changing the .cwk to .CWK in step 4, and neither works. It seems there is something else different with these files, but I can't figure it out. Again - thanks for your post - do you or anyone else have any other ideas?
Offline
well, maybe it has something to do with the file type then, which would take effect in this line:
Applescript:
if file type of currentFile is "CWWP" then.....
that part of the code is making sure that it is an applescript file. as long as you are SURE that they are all applescript files, you can just get rid of that part of code: delete the " if file type of currentFile is "CWWP" then " and also delete the 'end if' that is here:
on error theError
display dialog theError
end try
end if
end tell
Offline
Because I am totally new to AppleScript it took a couple of tries but I finally figured out what you were telling me to do - and IT WORKED!!!!! Thanks - for saving me hours of tedious work. (Maybe I will get this project completed soon!) Thanks a million.
Offline
Hello--
I am glad that the script is helping people!
As for designbysue's problems, the only thing I can think of (without being able to see the files that were causing the problem) is that the file type in older versions of AppleWorks is different. Until I see one of the problem files, I can't really fix the problem.
Anyway, since everyone is using the script -- let me post the latest cleaned up version of the script.
I hope it helps someone!
--Tom
(* AW6 to MS Word Converter
By Tom McKenna
May 23, 2004, Last revised August 3, 2004
This droplet converts AppleWorks word processing documents that are dropped on it
into Microsoft Word documents.
Revisions:
With the help of others, re-wrote DestinationFolder(). It's much better now.
Condensed code, where possible
Decided to implement handlers as an exercise in better script writing *)
property theTranslator : "Word Windows 97, 2000, XP 2002 "
property theExtension : ".doc"
property desktopPath : (path to desktop as Unicode text)
global targetFolder
global theCounter
on VersionCheck()
tell application "AppleWorks 6"
if (its version as string) is not "6.2.9" then display dialog "Go to http://www.apple.com/appleworks and download the latest version of AppleWorks 6 and then re-run this script." buttons {"Cancel"}
end tell
end VersionCheck
on DestinationFolder()
try
tell application "Finder" to set targetFolder to (make new folder at desktop with properties {name:"Converted Items"}) as text
on error -- The folder already exists...
display dialog "A Converted Items folder already exists on the desktop. Do you want to continue?" buttons {"Cancel", "OK"} default button 2
set targetFolder to (desktopPath & "Converted Items:") as text
end try
end DestinationFolder
on ConvertLoop(droppedFiles)
tell application "Finder" to repeat with currentFile in droppedFiles
if file type of currentFile is "CWWP" then
set currentName to the name of currentFile
if currentName ends with ".cwk" then
set chopExt to (offset of "." in currentName) - 1
set currentName to (text 1 thru chopExt of currentName)
end if
try
tell application "AppleWorks 6"
open file (currentFile as Unicode text)
save front document in file ((targetFolder & currentName & theExtension) as string) using translator theTranslator
close front document without saving
end tell
set theCounter to theCounter + 1
on error theError
display dialog theError
end try
end if
end repeat
end ConvertLoop
on CleanupThings()
tell application "AppleWorks 6" to quit
display dialog "Congratulations! " & (theCounter as string) & " file(s) have been converted from AppleWorks 6 file format to Microsoft Word file format." buttons {"OK"} default button 1
end CleanupThings
on open droppedFiles
VersionCheck()
DestinationFolder()
set theCounter to 0
ConvertLoop(droppedFiles)
CleanupThings()
end open
-------------------------
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
Offline
You have to wonder about a guy who continues to work on a script that works. "I know I can make it better, I know I can make it better..."
Here's what I come up with -- feel free to analyze, criticize or even compliment:
(* AW6 to MS Word Converter, version 1.3
By Tom McKenna
May 23, 2004, Last revised September 10, 2004
This application/droplet converts AppleWorks word processing documents into Microsoft Word documents.
Revisions:
Re-wrote DestinationFolder(). It's much better now :-)
With help, re-wrote the code the trims the ".cwk" extension off files that have it
Condensed code, wherever possible
Decided to implement handlers as an exercise in better script writing
Added a Translator Check() instead of the version check for better compatibility with some older versions of AppleWorks 6
Where appropriate, moved some of the globals and properties to the handlers
Added a run handler for those who won't/don't drag and drop
Miscellaneous small fixes
Condensed still more code
Added more comments *)
property theTranslator : "Word Windows 97, 2000, XP 2002 "
global theCounter
global targetFolder
on TranslatorCheck() -- Makes sure that the necessary translator is installed
tell application "AppleWorks 6" to set translatorList to export translators
repeat with i in translatorList's reverse -- Reverse because the translator is at the end
if (i as string) = theTranslator then return
end repeat
tell application "AppleWorks 6" to quit
display dialog "You need to update to the latest version of AppleWorks 6 to ensure that you have the proper file translators installed." buttons {"Cancel"}
error number -128
end TranslatorCheck
on DestinationFolder() -- Checks, and makes if necessary a "Converted Items" folder on the desktop
set desktopPath to path to desktop as Unicode text
try
tell application "Finder" to set targetFolder to (make new folder at desktop with properties {name:"Converted Items"}) as text
on error -- The folder already exists...
display dialog "A Converted Items folder already exists on the desktop. Do you want to continue?" buttons {"Cancel", "OK"} default button 2
set targetFolder to (desktopPath & "Converted Items:") as Unicode text
end try
end DestinationFolder
on ConvertLoop(droppedFiles) -- This is where the conversion takes place
set theExtension to ".doc"
tell application "Finder" to repeat with currentFile in droppedFiles
if file type of currentFile is "CWWP" then
set currentName to the name of currentFile
if currentName ends with ".cwk" then
set chopExt to (offset of "." in currentName) - 1
set currentName to (text 1 thru chopExt of currentName)
end if
tell application "AppleWorks 6" to try
open file (currentFile as Unicode text)
save front document in file ((targetFolder & currentName & theExtension) as string) using translator theTranslator
close front document without saving
set theCounter to theCounter + 1
on error theError -- Usually from damaged/very old AW documents
display dialog theError
end try
end if
end repeat
end ConvertLoop
on CleanupThings() -- Quit AW; inform user that the conversion is complete
tell application "AppleWorks 6" to quit
display dialog "Congratulations! " & (theCounter as string) & " file(s) have been converted from AppleWorks 6 file format to Microsoft Word file format." buttons {"OK"} default button 1
end CleanupThings
on open chosenFiles -- For files that are dropped on the icon...
TranslatorCheck()
DestinationFolder()
set theCounter to 0
ConvertLoop(chosenFiles)
CleanupThings()
end open
on run -- For when the icon is double-clicked
TranslatorCheck()
set chosenFiles to (choose file with prompt "Pick AppleWorks word processing document(s) to convert:" of type "CWWP" default location (path to "docs" as alias) with multiple selections allowed without invisibles) as list
DestinationFolder()
set theCounter to 0
ConvertLoop(chosenFiles)
CleanupThings()
end run
-------------------------
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
Offline
Hi tmckenna,
If you learn from your mistakes, then persistence is good. It's like you're evolving! :-)
You may write global variables on one line separated by commas. For example:
global theCounter, targetFolder
It's good practice to make subroutines modular. You could make your TranslatorCheck() subroutine modular by passing it the translator. Also, try not to jump with 'return'. This makes scripts harder to read.
on TranslatorCheck(theTranslator) -- Makes sure that the necessary translator is installed
tell application "AppleWorks 6" to set translatorList to export translators
if theTranslator is not in translatorList then
tell application "AppleWorks 6" to quit
display dialog "You need to update to the latest version of AppleWorks 6 to ensure that you have the proper file translators installed." buttons {"Cancel"} default button "Cancel"
-- tell application "AppleWorks 6" to quit -- these lines are not necessary
-- error number -128 -- the Cancel button causes an error
-- place a 'return xxx' at the end of subroutines
end if
return true
end TranslatorCheck
Whew, just this starting section took me about an hour. I love to teach though. Need to do something right now. Will continue.
Later,
Offline
You may write global variables on one line separated by commas. For example:
global theCounter, targetFolder
I had no idea you could even do this. Very cool! I will add it to my repertoire of AppleScript knowledge.
It's good practice to make subroutines modular. You could make your TranslatorCheck() subroutine modular by passing it the translator. Also, try not to jump with 'return'. This makes scripts harder to read.
on TranslatorCheck(theTranslator) -- Makes sure that the necessary translator is installed
tell application "AppleWorks 6" to set translatorList to export translators
if theTranslator is not in translatorList then
tell application "AppleWorks 6" to quit
display dialog "You need to update to the latest version of AppleWorks 6 to ensure that you have the proper file translators installed." buttons {"Cancel"} default button "Cancel"
-- tell application "AppleWorks 6" to quit -- these lines are not necessary
-- error number -128 -- the Cancel button causes an error
-- place a 'return xxx' at the end of subroutines
end if
return true
end TranslatorCheck
Nice scripting. I never thought about passing theTranslator to the handler. (I should have...)
I didn't know you could check directly in a list without looping through it. Nifty!
As for the error number -128, well that is just sloppy on my part. I know the cancel button does the same thing...
Whew, just this starting section took me about an hour. I love to teach though. Need to do something right now. Will continue.
Thanks for spending your time to go through this! It's been educational for me -- and I suspect many others...
--Tom
Offline