I created this AppleScript to copy whatever text you have selected in one document, then check to see if “Speech.doc” is open, if it isn’t, to open it, and then paste the selected text into Speech.doc. The script then puts the original document back up front, leaving the document with the new paste behind it. This works fine if Speech.doc isn’t open yet, but if it’s already open, the copy/paste fails. I’m new to applescript so I’d appreciate the help in figuring out the problem.
-- START
-- Send highlighted text to your speech
tell application "Microsoft Word"
-- First, let's remember which document we're looking at
set CurrentDocument to name of active document
-- Copy the selection
copy object selection
-- Check to see if Speech.doc is open, then select it
try
set theDoc to document "Speech.doc"
on error
open "Macintosh HD:Users:user:Desktop:Speech.doc"
set theDoc to active document
end try
-- Paste and insert hard return. Hard return is added to ensure that the next thing pasted in doesn't interfere or accidentally inherit the style of the text above it.
set theDoc to active document
paste object selection
type paragraph
-- Return focus and return to the original selection
activate CurrentDocument
end tell
-- END
Hi,
I recommend to reference docs always by name, not by active document which can be dynamically changed
-- START
-- Send highlighted text to your speech
set speechDoc to ((path to desktop as text) & "Speech.doc")
tell application "Microsoft Word"
-- First, let's remember which document we're looking at
set CurrentDocument to document (name of active document)
-- Copy the selection
copy object selection
-- Check to see if Speech.doc is open, then select it
if not (exists document "SpeechDoc") then
open speechDoc
end if
set theDoc to document "Speech.doc"
-- Paste and insert hard return. Hard return is added to ensure that the next thing pasted in doesn't interfere or accidentally inherit the style of the text above it.
paste object selection
type paragraph
-- Return focus and return to the original selection
activate CurrentDocument
end tell
-- END
Fixed both solutions, thanks for all the help! This has been the only place on the web capable of helping out in a timely and assistive manner.
I’m not very familiar with scripting Word
Take a look at Word 2004 Applescript Reference.
There are descriptions and examples of all commands
Ok, I have a slightly more complex question this time, hopefully someone with a bit of Word of experience can help out.
This script has a similar function to the previous script, instead, this time, the script is supposed to search for the page break above the selected text, the page break below it, copy everything between those two page breaks, then pastes it over to “Speech.doc”. I think this must have something to do with my not knowing how Find/Replace functions word inside AppleScript. This is what I have right now, unfortunately it doesn’t appear to be copying/pasting over into the other document. I’d be joyous if somebody could help me out.
-- START
-- This script sends a highlighted selection to your speech.
-- First define the speechDoc
set speechDoc to ((path to desktop as text) & "Speech.doc")
-- Alert Word that things are happening
tell application "Microsoft Word"
-- First, tell Word to remember the current selection so it won't be lost.
set CurrentStart to start of selection
set CurrentEnd to end of selection
-- First, remember which document we're looking at
set CurrentDocument to document (name of active document)
-- Find start of the previous block
set selFind to find object of selection
tell selFind
clear formatting
set content to "^m"
set content of replacement to ""
set forward to false
set wrap to find stop
set format to true
set match case to false
set match whole word to false
set match wildcards to false
set match sounds like to false
set match all word forms to false
execute find selFind
end tell
-- If no page break, set start to beginning of document. This assumes that the lack of a page break means you're on the first block of a file.
if found of selFind is false then
set BlockStart to start of range of active document
-- If a page break was found, move to the start of the block title and set the start position. The mac version doesn't require moving the cursor.
else
set BlockStart to start of selection
end if
-- Find the start of the next block, in order to determine where the current block ends. The only different with the previous Find function is that .Forward is now set to True.
set selFind to find object of selection
tell selFind
clear formatting
set content to "^m"
set content of replacement to ""
set forward to true
set wrap to find stop
set format to true
set match case to false
set match whole word to false
set match wildcards to false
set match sounds like to false
set match all word forms to false
execute find selFind
end tell
-- Check that the next block was found
if found of selFind is false then
-- If it wasn't, set BlockEnd to the end of the document and set the page break flag. This assumes that the lack of a page break means you're on the final block in the file. It also ensures that a page break gets added to the end of the file when being copied to Speech - this helps keep the file organized when sending over large numbers of blocks.
set BlockEnd to end of range of active document
page break is true
-- If a page break was found, then move to the start of the next block and set the end position.
else
set BlockEnd to start of selection
end if
-- Copy the block - sets the selection to equal the found BlockStart and BlockEnd, then copies it.
set start of selection to BlockStart
set end of selection to BlockEnd
copy object selection
-- Check to see if Speech.doc is open, if not, we'll open it, then make it the active document
if not (exists document "SpeechDoc") then
open speechDoc
end if
set theDoc to document "Speech.doc"
-- Paste and insert a hard return. The hard return is added to ensure that the next thing pasted in doesn't interfere or accidentally inherit the style of the text above it.
paste object selection
-- If copying the last page, insert a hard return and page break. This flag was set during the find process to ensure each block starts on its own page.
if page break is true then
type paragraph selection
insert break
end if
-- Return focus and return to the original selected text
activate CurrentDocument
set start of selection to CurrentStart
set end of selection to CurrentEnd
end tell
-- END
Bump, didn’t want this to be buried, I was hoping to get this problem fixed.
Are you searching for “natural” page breaks or only for inserted page breaks?
The search is supposed to find the first created/inserted page break above the current selection point, then the first created/inserted page break AFTER that first page break
I think that what you are missing is the “with/without match forward” property in the execute statement
tell application "Microsoft Word"
copy selection to StartingPlace
set myFind to find object of selection
clear formatting myFind
set content of myFind to "^m"
set wrap of myFind to find stop
if (execute find myFind without match forward) then
copy selection to startOfBlock
else
copy start of content of text object of active document to startOfBlock
end if
set selection to StartingPlace
if (execute find myFind with match forward) then
copy selection to endOfBlock
else
copy end of content of text object of active document to endOfBlock
end if
set selection to StartingPlace
end tell
Now the remaining problem I’m having is the selection of the text between the two finds. Since I’m adapting these from Word Macros, I translated this function to:
set start of selection to startOfBlock
set end of selection to endOfBlock
copy object selection
But when it pastes into the other document, currently it just pastes a page-break, instead of the text between the two page-breaks. Here’s the current code:
-- START
-- This script sends a highlighted selection to your speech.
-- First define the speechDoc
set speechDoc to ((path to desktop as text) & "Speech.doc")
-- Alert Word that things are happening
tell application "Microsoft Word"
-- First, tell Word to remember the current selection so it won't be lost.
set CurrentStart to start of selection
set CurrentEnd to end of selection
-- First, remember which document we're looking at
set CurrentDocument to document (name of active document)
-- Find start of the previous block
copy selection to StartingPlace
set myFind to find object of selection
clear formatting myFind
set content of myFind to "^m"
set wrap of myFind to find stop
if (execute find myFind without match forward) then
copy selection to startOfBlock
else
copy start of content of text object of active document to startOfBlock
end if
set selection to StartingPlace
if (execute find myFind with match forward) then
copy selection to endOfBlock
else
copy end of content of text object of active document to endOfBlock
end if
set selection to StartingPlace
-- Copy the block - sets the selection to equal the found BlockStart and BlockEnd, then copies it.
set start of selection to startOfBlock
set end of selection to endOfBlock
copy object selection
-- Check to see if Speech.doc is open, if not, we'll open it, then make it the active document
if not (exists document "SpeechDoc") then
open speechDoc
end if
set theDoc to document "Speech.doc"
-- Paste and insert a hard return. The hard return is added to ensure that the next thing pasted in doesn't interfere or accidentally inherit the style of the text above it.
paste object selection
-- If copying the last page, insert a hard return and page break. This flag was set during the find process to ensure each block starts on its own page.
if page break is true then
type paragraph selection
insert break
end if
-- Return focus and return to the original selected text
activate CurrentDocument
set start of selection to CurrentStart
set end of selection to CurrentEnd
end tell
-- END