# Can automation using AppleScript Copy Text From A Webpage Into a Text Editor app

applescriptcopy/pastesafari

As a first try at AppleScript I'm looking to automate a find command and some follow actions that would normally be user input.

Ultimately I want to automate adding lyrics to my iTunes library. My first challenge is to tell Safari to search a webpage for a phrase, click on the link, and then copy the plain text on the page that comes up to the clipboard and then paste it into Text Editor.

I'm not sure how to tell Safari to command + F, paste a phrase from the clipboard, and then "click" the highlighted link.

I don't need a full script, but help getting started at a minimum

How would I do one or more parts of this automation?

Your first AppleScript! Amazing... And an ambitious project to start with, but I can see its value.

My answer won't write the script for you, but I will give you tips about how to approach your problem differently and achieve what you want a little more easily.

### Telling Safari to Cmd-F

    tell application "Safari" to activate

tell application "System Events" to keystroke "f" using command down


### Paste a Phrase From Clipboard

Send a Cmd-V. You might need a small delay between the two keystroke commands.

You can't. You won't be able to identify what's highlighted via a Cmd-F search with any great ease (possibly through GUI scripting, but that'll be a mess). I'll come back to this shortly.

# A Different Approach

Working backwards, let's assume you've obtained the text you need to paste into TextEdit. Don't paste it. Set the contents of a new TextEdit document to the contents of the clipboard:

    tell application "TextEdit" to set text of (make document) to the clipboard


In order to set the clipboard to the contents of a Safari web page, use this command:

    tell application "Safari" to set the clipboard to  ¬
(text of current tab of front window) as string


### And this brings us back to clicking on the link...

If you literally want to click a link on a webpage, one method would be to make sure Allow Javascript from Apple Events is checked in Safari's Develop menu, and run a do JavaScript to pick out the link containing the relevant search item and click it:

    tell application "Safari" to tell document 1 to do JavaScript ¬