Hi, I am a Billy basic user with no programming experience but am great with copy and paste
Can any of you experts help me please?
I am trying to save my poor wrists from RSI by limiting the mouse dragging and clicking, I am harvesting school names, telephone numbers and email addresses from state web sites, I can do the auto copy and paste into Excel and then back to Safari but what I can't do is automate the mouse selection process.
The school names, telephone numbers and email addresses are in similar positions but never exact so the record function doesn't work for me. I thought I may be able to manually click at the beginning of the line and get Maestro to hold the mouse key down and have it drag to a certain position and then release and copy and paste into Excel, but I can't see how to do that. The end of the line is always the same. Does this make sense?
Please see the link for an example of one of the pages.
You'd then have the name, the phone and the email in three KM variables, that you can easily use later to insert the text in Excel, with something like that (assuming you put the cursor into the Name cell and Phone and Email are next to it):
How it works:
If you look at the source code of the page (for example by right clicking on a school name and selecting Inspect Element—assuming you've enabled the Develop menu in Safari), you can see that the various fields are enclosed in a div whose class is named estabContent:
<div class="estabContent" >Albourne CofE Primary School (Hassocks)</div>
In this page, the name is in the first div that has that class, the phone is in the 5th one, and the email in the 7th one. Since Javascript (and most computer programs) counts from zero instead of one like we humans do, these numbers are respectively 0, 4 and 6. And if you look at the Javascript in the actions above, you can see that each uses one of these numbers to extract the text from the first, fifth and seventh div with class "estabContent".
Hopefully that should get you started. And if you feel like learning more about controlling Safari or Chrome from KM, you can see my tutorials at http://flipmartin.net/software .
Wow! Thank you very much for that, but you lost me at Hi! (Almost)!
The Uk schools sites are all different, I have just finished doing the West Sussex one manually. I can see how your solution would be perfect if my knowledge was even 1% of yours, is there a way to do it via the muse for the other sites without looking at the source code? Thank you very much for your time anyway.
Oh, I thought you extracted that info from a single site, sorry. Unfortunately that method won't work if each school has its own site, unless they're all made by the same organism on the same model and using the same templates.
Click and drag will probably never do it, because you have to give it coordinates, and they'll always change. But maybe triple-clicking would do. If you try triple-clicking the value of one field, like the address, on the site you referenced, it will select the contents of that field (except if the whole contents of that field is a link). So maybe you could base your macro on that. You can have KM do a triple-click where your cursor is that way:
wait for you to press Return (which you'd do after you moved the cursor over the next field to extract)
triple-click
copy the selection
put the clipboard into another variable
wait for you to press Return again
and so on.
That wouldn't be as easy or quick as my original suggestion, but it would still be better than doing it all by hand, as you'd just have to trigger the macro, then move the cursor and press a key a few times.
Thats amazing Philippe, I had no idea that a triple click existed (told you I was a billy basic), I can even triple click slightly to the right of the hyperlinked email addresses and it highlights the whole email address without opening it - my wrists are saved. Now comes the “trying to explain to one half of the thicky twins the theory of relativity” bit, the variables mention is having me stumped. So i move the mouse to where I want the fantastic Keyboard Maestro to triple click (school name), command C to copy it then do the variable bit, go to the school telephone number, repeat it and then go to the email and do the same, open excel and how do I paste the three separate bits into lateral cells? sorry that I am trying your patience. x
It’s OK, KM’s learning curve is pretty steep for everybody!
Assuming you now have three variables named schoolName, schoolPhone and schoolEmail, I showed you in my first post how to paste them. It’s the second screenshot.
When you trigger the macro, Safari comes to the front if it's not already there, and the macro waits for you.
Put the cursor over the name field, and press Return. The macro will select and copy that field, then wait for you.
Put the cursor over the phone field and press Return. Again the macro will select and copy the field, then wait for you.
Put the cursor over the email field and press Return. The macro will select and copy the field, take Excel to the front, and wait for you.
Put the cursor into the name field in Excel and press Return. The macro will paste the name, type a tab, paste the phone, type a tab, paste the email, and exit.
Of course, this assumes that you have name, phone and email in adjacent columns in Excel, but if you don't, the macro should be easy enough to modify.
Copy and paste is often quite problematic from Safari. If you feel you must c&p then Firefox will probably give you more consistent results.
Personally I would automate the heck ouf of this job by pulling the text out of the page in Safari, processing it, and then putting it into Excel.
This the attached example macro is specifically tailored for the link you provided to show how a little processing can go a long way - but it can easily be made more generic.
You start out by loading the page you want to process in Safari and then run the macro.
I'm using TextWrangler (freeware) as the output vehicle for the text, because it's very scriptable and therefore fairly ideal for secondary processing. (I use TextWrangler's commercial sibling BBEdit myself.)
The shell script in the macro assumes you have TextWrangler installed along with its command-line-tools. (Command-line-tools are installed via the TextWrangler menu in the app.)
The output of the macro looks like this:
Name:
Albourne CofE Primary School (Hassocks)
Address:
The Street
Albourne
Hassocks
W Sussex
BN6 9DH
Headteacher:
Mrs. A Sturgoen
Chair of Governors:
Mr. M. Cutress
Phone:
01273 832003
Fax:
01273 835569
Email:
office@albourne.w-sussex.sch.uk
Website:
http://www.albourne.w-sussex.sch.uk
Area:
North
DCSF number:
3060
Locality:
23 - Hassocks
Area Partnership:
Area Partnership C2
Type:
Primary (Years R to 6)
And that's pretty easy to work with.
For practical usage I'd let the whole text be sent to TextWrangler and do all processing in it with a filter or two.
If you want to give this a try I can help you with some general filters.
-Chris
P.S. Bah! I forgot to add an attribution comment to the macro.
Hi Chris, thank you very much for taking the time and effort to reply with your solution, I am learning so much here. However your variable and shell script bit has gone so far over my head I think the international space station may now be able to see it.
I have downloaded and installed Textwrangler, I have made a simple macro (and i mean simple) to click on a link and then copy the whole of that web page into text wrangler, It seems a shame, that now I have everything, not to extract more than just the school name, email and telephone details, the full address, Head teachers name and DFE number would also be very helpful - I could then put it all in a excel sheet to use better. How would I use textWrangler to remove the chaff to be left with those bits and then export that into single cells across the sheet? Hopefully once I manage to learn from you gurus I can use these methods on all the other County websites. Tony
On another point, when i copy and paste and do other multiple things, my mac seems a tad too quick which gives me erratic results, I have just found the pause control and add .1 second pauses everywhere but this seems a bind, am i missing something? this is particularly noticeable when i copy and paste 10 email address from excel and put them into the bcc field in outlook…
Understand that when you drive the user interface (UI) artificially there will often be little glitches and timing issues you’ll have to account for.
Getting and setting the clipboard takes a little time, and KM can indeed try to paste before the clipboard is finished changing to what you’ve set it to.
Use this macro to get all text from the front Safari Window into TextWrangler.
It will even set the window size (which you can change to suit).
This will provide a smoother experience than Cut & Paste and will produce generally more readable (and therefore filterable) text.
* This macro dumps the html source from the Safari window onto the Desktop (which it then process into text), but I can just as easily put it into an invisible temp folder.
Hey Chris, I found this looking for a somewhat unique version of this. Precisely this, but having the HTML be HTML on the clipboard (Text Factory before pasting would be EXTREMELY HELPFUL), but I need it to paste into BBEdit or Numbers as the final very much trimmed down pieces I derive from the text Thank you if you're able to help in advance!