2

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?

1 Answer 1

3

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.

To answer your specific queries first:

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.

"Click" the Highlighted Link

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 ¬
        "Array.from(document.links).find(a => a.href.includes(\"cjk\")).click()"

This only finds the first instance of the search time, which I have inferred is sufficient given that your method of doing a Cmd+F search seemed content to accept the first occurrence as the only one of importance.

However, I think the whole process could be streamlined and made much more robust by using Automator to help us out. It can extract all URLs directly from a webpage (all you have to do is feed it the URL of the webpage in question). Then you can filter these URLs to search for specific text. Once you have the URL you desire, instead of clicking on it, Automator can download its contents as text and insert it straight into a new TextEdit document. I do believe that next to no AppleScript would be needed, although you can supplement it with some AppleScript to fine-tune things here and there as required.

Without your webpage in question, I can't devise the workflow specific to your application. However, that would also deprive you of the joy of doing it yourself. But using this Stack Exchange webpage as an example, the following workflow isolates the URL of your user profile (the link to which is at the bottom of the question you asked), downloads the text from your profile, and turns it into a new TextEdit document:

enter image description here

Hopefully, this is enough to get you well on your way to achieving your goal. If you need any clarification, leave a comment and I'll get back to you when I can.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .