Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to open text edit document cut and paste to MS Word

Hi all

I'm hoping that someone out there can help me with this one.

I've created an automation workflow that cleans up an HTML file (in text edit) and gives me a list of addresses.

I'd like an applescript to execute at the end of the this that will open the resulting text edit file copy all the contents, close the file, open MS word, open an MS word document and paste the copied text into the file (the first label field)

Is this possible, can someone help? I have virtually no knowledge of applescript, but am getting to grips with automator (could I do it in that?)

Thanks in advance
Chris

Mac Mini, Mac OS X (10.4.5)

Posted on Jul 14, 2010 4:39 AM

Reply
12 replies

Jul 14, 2010 5:27 PM in response to chriszammett

Hi Chris,

I don't have MS Word but this should get you pointed in the right direction.

tell application "Finder"
open "Macintosh HD:Users:short_name:Desktop:hello.txt" --example path to file
end tell
delay 2 --allow time for TextEdit to open
tell application "System Events"
keystroke "a" using command down --select all the text
keystroke "c" using command down --copy all the text
end tell

tell application "TextEdit"
quit
end tell

--the text is now stored in the clipboard

From here something like

tell application "Microsoft Word"
create new document
end tell
delay 2
tell application "System Events"
keystroke "v" using command down
end tell

Hope this helps and ask if you can't figure out the rest on your own.

Jul 15, 2010 4:17 AM in response to taylor.henderson

Hi Taylor, thanks for your help.

OK, I'm trialing this with textedit before I MS word to get the script right, then will swap it to MS Word.

This is what I have so far, it come up with the following error: Syntax error: Expected end of line but found identifier:

tell application "Finder"
open "Macintosh HD:Users:Chris:Desktop:Labels:Finished labels:labelstoprint.txt"
end tell
delay 2
tell application "System Events"
keystroke "a" using command down
keystroke "c" using command down
end tell

tell application "TextEdit"
quit
end tell

tell application "TextEdit"
create new document
end tell
delay 2
tell application "System Events"
keystroke "v" using command down
end tell

Jul 15, 2010 4:32 AM in response to chriszammett

The problem is in the block of code

+tell application "TextEdit"+
+create new document+
+end tell+

To script applications you need to be sure that they are scriptable and that you are using the correct syntax. Open up Script Editor and choose File - Open Dictionary. Then select TextEdit. This will give you the list of commands and usage instructions for TextEdit which can differ from other programs.

I apologize if my "create new document" line threw you off but I believe that is what is used in MS Word and either way it was just an example. Check the dictionary for TextEdit and you'll be on your way 😀

Jul 15, 2010 4:59 PM in response to chriszammett

I'm a little confused what the path problem is but this should be doing what you expect it to.

tell application "Finder"
open "Macintosh HD:Users:Chris:Desktop:Labels:Finished labels:labelstoprint.txt"
end tell
delay 2
tell application "System Events"
keystroke "a" using command down
keystroke "c" using command down
end tell
tell application "TextEdit"
make new document
end tell
delay 2
tell application "System Events"
keystroke "v" using command down
end tell

Jul 15, 2010 5:20 PM in response to taylor.henderson

Here's what I'm pretty sure is a working script for what you want to do overall now as well. I think it'll help you more than plodding through the rest of this.

tell application "Finder"
open "Macintosh HD:Users:Chris:Desktop:Labels:Finished labels:labelstoprint.txt"
end tell
delay 2
tell application "System Events"
keystroke "a" using command down
keystroke "c" using command down
end tell
--tell application "TextEdit"
-- quit --not good if you had it open for something else
--end tell
tell application "Microsoft Word"
activate
make new document --may be redundant if word is not open
end tell
delay 2
tell application "System Events"
keystroke "v" using command down
end tell
tell application "Microsoft Word"
save active document in "Macintosh HD:Users:Chris:Desktop:Labels:Finished Labels:labelstoprint.doc"
-- quit --not good if you had it open for something else
end tell


note that by using system events you can check if TextEdit and Microsoft Word are already open or not before the script runs and then try to return them to their original state after you are done with them. A little project continuation if you would.

Message was edited by: taylor.henderson

Jul 16, 2010 2:55 AM in response to taylor.henderson

Hi Taylor, thanks for your help with this and your advice has helped me progress this project and make it better!

I'd like a little bit more help if possible.

Since posting this I've moved away from MS Word as my job is a designer, so I normally use Quark.

What I've managed to do with automator and applescript is come up with a program which does the following things:

• pastes source code in text editor
• cleans text to provide addresses
• move addresses to quark
• replace "*" with the down tab to flow addresses between text boxes to create labels
• apply style sheet.

I'd just like to add 1 step before all this which is taking the source code from the webpage which I am viewing. I searched internet and found the following which takes the code and drops it in text edit, but it errors with the warning "TextEdit got an error: AppleEvent handler failed." and this stops it moving on to the new automator action.

This is the code taken out of automator:

on run {input, parameters}

tell application "Safari"
-- The following line collects info used by the Terminal editors
set myURL to the URL of document 1 as string

-- Retrieve the source from the browser
set mySource to the source of document 1 as string


-- Start TextEdit Code
tell application "TextEdit"
activate
make new document at the beginning of documents with properties {text:mySource}
set modified of front document to yes
end tell
-- End TextEdit Code


end tell

return input
end run

Jul 16, 2010 4:16 AM in response to chriszammett

Hey Chris,

So far we've been mostly using GUI Application Scripting mainly because it can be easier to visualize and because it is often simpler code. For grabbing source code off the net though, the best tool at your disposal will be cURL which is a command line application accessible through the Terminal. Here is a brief tutorial...

http://curl.haxx.se/docs/httpscripting.html

as for implementation into applescript


set mySource to do shell script "curl http://www.google.ca"
--or if you want to get the url in another way
set mySource to do shell script "curl " &myURL


If you are really set on using a big browser for this...perhaps you want to grab info as you are surfing...you can use GUI scripting again with System Events key presses and the short cut for view source. In Firefox "command u"

Also reading over your bullet list of code makes me think you have a few extra steps in there. This might be a little more refined.

-use cURL to get source of a website saved in a string variable
-search the string for emails saving them to a list variable
-send the list to Quark

Jul 16, 2010 4:24 AM in response to taylor.henderson

Thanks for the quick reply, I'll look into this and whilst I'm doing that I thought I'd tell you exactly what I'm doing which may help you help me!

I'm trying to automate getting addresses for ebay sales, from the print label window. ie. I have a safari window with all the addresses in, so I'm retrieving this source code and automating the process to move it into quark and avery label template I've set up.

Thanks

Jul 16, 2010 6:23 AM in response to chriszammett

Further to this...

I set this automator/applescript action up on a leopard machine, but it will actually run on a tiger machine...

So, my watch and do automator action for pasting the info into quark doesn't work anymore!

So if you could tell me how to add a "paste into the first box" applescript to the find and replace bit, that would be most helpful.

Am I going mad - should I just use my fingers and do it manually?!

Jul 16, 2010 6:58 AM in response to chriszammett

okey, solved that. Currently have this script.


on run {input, parameters}

tell application "QuarkXPress"
tell document 1
set tool mode to contents mode
tell page 1
(select story 1 of text box 1) paste
end tell
end tell
end tell
tell application "QuarkXPress"
tell document 1
tell current box
tell story 1
set every text where it is "*" to ASCII character 11
end tell
end tell
end tell
end tell
return input
end run

Applescript to open text edit document cut and paste to MS Word

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.