Free AppleScripts for AppleWorks 6

AppleScript
AppleScript | Source

A Collection of AppleWorks and AppleWorks-Related Scripts

A site for those who use AppleWorks on a routine basis. More and more people are using Microsoft Word these days, myself included, but I continue to use AppleWorks as well, while there are so many useful scripts that can be written for AppleWorks.

Where AppleWorks cannot be scripted by the usual means, it can be scripted through the means of GUI. To see some of those, go to : AppleScripts for Graphical User Interface

AppleWorks Replace Selected Text

Select some text in an AppleWorks 6 document and run this AppleScript.

A display dialog prompts you to enter the replacement text for the highlighted text. The entered text is saved in the varieble replaceText and a copy of the selected text is saved to the variable targetText. A repeat block compares all text to targetText and replaces all instances found.

Finally, at the end, you have the option of reverting the document back to its original saved state using a bit of GUI scripting through System Events.

tell application "AppleWorks 6"
	try
		tell document 1
			set targetText to the selection
			set replaceText to text returned of (display dialog "Enter replacement text for the text ' " & targetText & "'." default answer "" with title "Replace text '" & targetText & "'" buttons {"Cancel", "Replace All"} default button 2)
			set textCount to (count words)
			set y to 0
			repeat with x from 1 to textCount
				if word x = targetText then
					set word x to replaceText
					set y to y + 1
				end if
			end repeat
		end tell
		set revertAction to button returned of (display alert y & " Instances Found" message y & " instances of the text " & targetText & " were replaced with " & replaceText & ". Choose 'Revert' from the 'File' menu to revert back to previous version of text." buttons {"No", "Revert"} default button 2 as warning)
		if revertAction = "Revert" then
			tell application "System Events"
				tell process "AppleWorks 6"
					click menu item "Revert" of menu "File" of menu bar item "File" of menu bar 1
				end tell
			end tell
		end if
	on error theError number theNumber
		if theNumber = -1700 then display alert "Oops !" message "Please select the text to be replaced." buttons {"Abort"} default button 1 giving up after 15 as warning
		return
	end try
end tell

AppleWorks 6 and Property Keyword

Example Using a Property Variable

This script shows how to make use of a property variable in creating a document in which you want to add multiple data from multiple sources to the same document:

property docCreated : false
tell application "AppleWorks 6"
activate
if docCreated = false then
display dialog "Enter a name for new document:" default answer "" buttons {"Cancel", "OK"} default button 2 with icon 1
set userEntry to text returned of the result
make new document at front with properties {document kind :text document, name:userEntry}
set docCreated to true
save
end if
tell front document
display alert "Text Insertion" message "Are you sure you want to insert the text from the clipboard into the front document?" buttons {"Cancel", "OK"} default button 2 as warning
set executePaste to button returned of the result
if executePaste = "OK" then
tell application "System Events"
key code 125 using command down --arrow key down
key code 36 --return
key code 36 --return
end tell
paste text
set properties of every text to {font:"Verdana", size:14}
end if
end tell
end tell
Source

Improving Performance of AppleWorks 6

Empty AppleWorks Recent Items Folder

Using AppleWorks, you may find at times that it seems to be running particularly slow. Sometimes, it is because there are too many items in the Recent Items folder associated with AppleWorks. The Recent Items folder actuallly keeps track of more than just the recently opened documents; it keeps track of all of the documents that you open. As a result, it can become a huge file over time This is a handy little script that can be used to reduce this clutter from time to time.

Copy this into the Script Editor and save as application. You can place it in the Scripts folder, so that you have access to it from the Scripts menu of AppleWorks:

tell application "Finder"
try
activate
set targetPath to (path to documents folder) as string
-->"Macintosh HD:Users:macuser:Documents:"
set targetPath to (targetPath & "AppleWorks User Data:Starting Points:Recent Items:")
-->"Macintosh HD:Users:macuser:Documents:AppleWorks User Data:Starting Points:Recent Items:"
open targetPath
select every item of Finder window 1
set selectedCount to (count items of selection) as string
move selection to trash
display alert selectedCount & " items" message "These items have been moved to the trash. Are you sure you want to delete them permanently ?" buttons {"OK", "Cancel"} default button 2 as critical
set executeDelete to button returned of the result
if executeDelete = "OK" then empty trash
on error errorMsg
display alert "Abort Delete" message "Deleting of items has been cancelled ." buttons "OK" default button 1 giving up after 15 as informational
end try
end tell

If you like this site, please share with others on Twitter

Mac Cube
Mac Cube | Source

AppleScript for Creating a New Document with AppleWorks 6

A Simple script to create a new AppleWorks document followed by a more detailed script with text entry fields

If you use AppleWorks 6, you may find this script useful in creating new documents. it should be fairly easy to see how this can be adapted to your specific needs:

set theDocName to "Simple Text Document"
set theData to "Basic text for new document"
tell application "AppleWorks 6"
activate
make new document with data theData with properties {name:theDocName}
tell front document
select paragraph 1
set the properties of the selection to {font:"Helvetica", size:24}
--Here, for 'Helvetica', you could substitute any font that you have available on your system (Arial,Monaco,Geneva etc), and size, in this case '24', any size that you have available.
end tell
end tell

You could set 'theDocName' to the result of a query dialog where the user enters a specific name for the new document and 'theData' to the result of a dialog where the user enters the initial text for the new document.

Here's an idea of how this enhancement of the previous script would work:

set theDocName to text returned of (display dialog "Enter new document name:" default answer "" buttons {"Set"} default button {"Set"})
set theData to text returned of (display dialog "Enter initial text for document '" & theDocName & "':" default answer "" buttons {"OK"} default button {"OK"})
tell application "AppleWorks 6"
activate
make new document with data theData with properties {name:theDocName}
tell front document
select paragraph 1
set the properties of the selection to {font:"Helvetica", size:24}
end tell
end tell

Because I approach things as a progression, If you have read my previous articles, you will probably have a pretty good idea of how to write the dialog statements that I have suggested here. If you haven't seen my previous posts, I encourage you to check them out.

Enhanced - New Doc for AppleWorks 6

A more detailed script with text entry fields

It has become abundantly clear to me that there is still a lot of interest in AppleScripts for AppleWorks 6 and questions on implementation of scripts that my previous posts on this topic have not addressed to the satisfaction of those who use AppleWorks 6 and, since it is an application that is still fairly universal in the Mac OS, I guess it is worth further examination. So, here's an enhancement of an earlier script that I hope will be useful:

set theDocName to text returned of (display dialog "Enter new document name:" default answer "" buttons {"Set"} default button {"Set"})
set paragraph1 to text returned of (display dialog "Enter text for first paragraph of document '" & theDocName & "':" default answer "" buttons {"OK"} default button {"OK"})
---->Given this entered text: Notes on AppleScript and AppleWorks 6:
set paragraph2 to text returned of (display dialog "Enter text for second paragraph of document '" & theDocName & "':" default answer "" buttons {"OK"} default button {"OK"})
---->Given this entered text: As it appears in this document, this is the second paragraph but, to AppleScript,this is the third paragraph, because even an empty return is a paragraph in AppleScript.
set paragraph3 to text returned of (display dialog "Enter text for third paragraph of document '" & theDocName & "':" default answer "" buttons {"OK"} default button {"OK"})
---->Given this entered text: This, it might seem is the fourth paragraph but, it is actually the fifth to AppleScript.
set theData to paragraph1 & return & return & paragraph2 & return & return & paragraph3
tell application "AppleWorks 6"
activate
make new document with data theData with properties {name:theDocName}
tell front document
select paragraph 1
set the properties of the selection to {font:"Lucida Grande", size:18}
set color of the selection to {9960, 12194, 65535} ----blue, an RGB value
select (paragraphs 3 thru 5)
set the properties of the selection to {font:"Monaco", size:9}
select word 14 of paragraph 3
-- set the properties of the selection to {size:12}
set size of the selection to 12
--Two ways to express things (look above)
set colorPref to (choose color default color {65535, 5195, 6617}) --red, an RGB value
set color of the selection to colorPref
--select (text 1 thru -1 of last word of paragraph 3)
set size of the selection to 12
set color of the selection to colorPref ----red, an RGB value
select (text 1 thru -1 of last word of last paragraph)
set size of the selection to 12
set color of the selection to colorPref ----red, an RGB value
save document theDocName in alias (path to desktop folder) as file type {"CWWP"}
end tell
end tell

Most of this is pretty straightforward. Take note of my use of negative indeces to groups of text above.

This could be used to change the color of a number of words in the initial script all at once:

tell application "AppleWorks 6"
tell front document
try
set everyWord to (every text)
set textCount to (count everyWord)
repeat with x from 7 to textCount
if word x = "AppleScript" then
select word x
set color of the selection to {65535, 5195, 6617} --red
end if
end repeat
on error
return
end try
end tell
end tell

This is how the initial script would appear:

To change the text style is a bit more complicated, something like this would work:

tell paragraph 1
	select (words 3 thru 4)
	set size of the selection to 14
	set style of the selection to {class:text style info, on styles:{bold, italic}}
	select word 6
	set size of the selection to 14
	set style of the selection to {class:text style info, on styles:{bold, outline}}
end tell

Which would look something like this:

If you run this script in Script Editor with our document in front...

tell application "AppleWorks 6"
activate
tell front document
select paragraph 1
get properties of the selection
end tell
end tell

Out of this, we can see that we have these styles available:

{plain, bold, italic, underline, outline, shadow, condensed, expanded, strikethrough,superscript, subscript, superior, inferior, double underline}

Converting HTML to Text Format

Using AppleWorks 6 for Batch Processing Often, I copy text from one of my posts into a text file for further editing (typos, script errors etc) only to find that I have copied the HTML too, which is not good since it's not usually the HTML that I want to edit, and therefore makes it difficult to focus on what I really want to do.

As a result, I wrote this rather lengthy script, which, once you figure out the syntax, is fairly simple. This script could be improved upon, but it does illustrate the power of AppleScript in batch editing of text:

tell application "AppleWorks 6"
	activate
	set defaultFont to "Geneva"
	set defaultSize to "14"
	set editFormat to button returned of (display alert "The default font and size settings are '" & defaultFont & ", " & defaultSize & "'." message "Use defaults or choose alternate settings." as informational buttons {"Change", "Defaults"} default button 2)
	if editFormat = "Change" then
		set defaultFont to text returned of (display dialog "Enter new text font:" default answer defaultFont with icon 1 buttons {"Change"} default button 1)
		set defaultSize to text returned of (display dialog "Enter text size:" default answer defaultSize with icon 1 buttons {"Change"} default button 1)
	end if
	tell document 1
		repeat
			set wordCount to (count of words)
			repeat with x from 1 to wordCount
				set wordCount to (count of words)
				if x > wordCount then exit repeat
				select word x
				if the selection contains "<" then
					set targetBegin to (index of text 1 of the selection)
					set y to (targetBegin + 1)
					repeat
						select text y
						if the selection contains ">" then
							set targetEnd to (index of the selection)
							exit repeat
						end if
						set y to (y + 1)
					end repeat
					select (text targetBegin thru targetEnd)
					cut selection
				end if
			end repeat
			select every text
			if "<" is not in the selection then
				set properties of the selection to {font:defaultFont, size:defaultSize}
				exit repeat
			end if
		end repeat
	end tell
end tell

Most of this does not require any explanation. The script lines:

select every text
if "<" is not in the selection then

Terminate the script when no other instances of '< ' are found.

In the script, 'targetBegin' and 'targetEnd' refer to the actual number indeces corresponding to the text strings that are about to be removed.

Note that due to the nature of the syntax, the script requires a couple of 'passes' to catch all instances of the '< ' character. To trap for other, more elaborate HTML for special characters such as &amp; or &nbsp;, for example, you would have to embellish the above code a bit to cover all of the possibilities

By the way, if you follow my posts on a regular basis, you will have noticed that I write most of my 'text-related' scripts for AppleWorks. It's not that I am totally enamored with AppleWorks, it's just that I have used it a lot in the past.

In closing, If you have questions on this or would like to suggest an article on another AppleScript issue, please go to 'Comments, Suggestions and Bugs' at the bottom of the page and leave a comment

4 stars for AppleScripts for AppleWorks 6

© 2013 AppleScripter

More by this Author

  • AppleScripts for Graphical User Interface (GUI)
    0

    Graphical User Interface with AppleScript,Free AppleScript code for Apps with no support,System Events,click menu item,click text field,click radio button,click pop up button 1,tell checkbox 1 to

  • Advanced AppleScript Code
    0

    Advanced AppleScripts. For reasons of logic, because the original site was becoming rather large, some of the posts you find here were moved from the primer site for those who are beyond the basics

  • Free AppleScript Code Examples
    1

    Write AppleScript Code,free AppleScripts of display dialog,display alert,choose from list,finder,choose file,choose file name,choose folder,choose application,choose color,Finder,TextEdit


Comments, Suggestions and Bugs

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working