Showing posts with label AppleScript. Show all posts
Showing posts with label AppleScript. Show all posts

Tuesday, September 22, 2015

Automatically Open Multiple Websites, Part 1


(There's nothing I like better than saving people time and trouble by showing them a better way to use their Macs. These simple AppleScripts will save you time and trouble day after day after day.)

I am willing to bet that you have a bunch of websites that you look at every day.

Let's pretend that every morning you have a look at Time Magazine, Google News, the New York Times, and the Los Angeles Times. Even if you use a bookmark for each site there's enough mousing around that just opening the sites takes a measurable amount of time. It might not be much, but it all counts. And, you actually have to do the work-- it doesn't happen unless you do the work.

What if you could click a single item in your Dock and have all four of your news websites open, each in its own window, in a nice neat stack on your screen? Wouldn't that be cool?

Of course that would be cool. And, it's totally doable. In fact, I've done it for you, along with two variations (one for Mac-related news sites, and one for sports news). I did it with AppleScript, a very powerful feature of the Mac OS, available on every Mac.

Click to download the AppleScripts. You'll get a disk image. (If you're using Safari the disk image will probably open automatically. If not, or if you're using Firefox, look in your Downloads folder for something called "URL_AppleScripts.dmg" and double-click it.)

Eventually you'll see the three items shown below. They're AppleScript Applications. Double-click each one to see what it does. Drag the ones you like to your Applications folder. Then, from there (that is, from in the Applications folder), drag them to the Dock. Now a single click in the Dock will trigger a multi-page masterpiece.

Of course you are probably thinking it would be even better if the sites that opened were the ones YOU liked, not the ones I like. That's easy to do, and I'll write that up sometime soon. If you're in a hurry, tell me and I'll help you out right away.

In the meantime-- isn't this neat? One click opens four websites, neatly, quickly, efficiently. Who could ask for more?

Monday, September 21, 2015

Your Appointments, Sir


No matter how careful you are about putting stuff into your calendar you still have to look at it to see where you're supposed to be. I had the same problem, but not anymore, because I figured out how to have my Mac read my appointments to me out loud, every day, on a timer. It's very cool, and since it's scheduled to run every morning at 7:30 AM, I think it's going to help me out quite a bit. Especially on weekends, when I tend to forget to check the calendar.

I was hoping to do this with an Automator Workflow, and if not for an iCal bug Automator would have been the way to go. I worked pretty hard at making it work with Automator but eventually realized that the reason it didn't work was something I couldn't work around. So I turned to AppleScript.

Here's the script. The gray parts are comments, put there to help you understand what's going on. Note: the script gets you part of the way there. You still need something to trigger the script at the appropriate time. Lots of programs can do that for you. I chose Script Timer, a nice little $12 program that I just found out about. Here's the link. You can get a free 30-day trial.


set the_text to ""
set today to current date
set time of today to 0
set tomorrow to (today) + 86400 -- seconds
--
-- Here are the calendars I want to check. Yours will be different. Change the following line to match the names of your calendars.
-- If you are going to check ALL of your calendars this script could be simplified. Send me an email and I'll help you.
-- Christian Boyce, macman@ioperating.com
set the_calendar_list to {"CB & A", "usc football 2009", "Cal Football 2009", "Texas Football 2009", "UCLA Football 2009", "Birthdays"}
--
tell application "iCal"
-- First we need to tell iCal which calendars are going to be checked. We match the names in "the_calendar_list" to the names of the actual calendars in iCal. The ones that match are added to our "the_calendars" list.
--
set the_calendars to {}
set every_calendar to every calendar
-- Now we have a list of calendars to check.
repeat with an_item in the_calendar_list
set end of the_calendars to (first calendar whose name is an_item)
end repeat
-- Now we check, on a calendar by calendar basis, for appointments on the current day.
repeat with a_calendar in the_calendars
tell a_calendar
set the_events to (every event whose start date > today and start date < tomorrow)
--
-- Here we sort the list of events for the day. If we don't do this they won't be chronological. iCal sorts them in creation order unless we run this little "sortEvents" routine.
--
set the_events to my sortEvents(the_events)
-- Now we have a sorted list. Let's create a string for the Mac to speak. Loop through the events and make that string.
set i to 1
repeat with an_event in the_events
set x to properties of an_event
set the_summary to summary of an_event
set the_start_date to start date of an_event
set the_end_date to end date of an_event
set the_start_time to time string of the_start_date
set the_end_time to time string of the_end_date
--
set the_text to the_text & return & "Appointment number" & i & "." & return & the_start_time & " to " & the_end_time & "." & return & summary of an_event & return & return
set i to i + 1
end repeat
end tell
end repeat
--
-- If there aren't any events the string "the_text" will be empty. In that case we want to say something different.
if the_text is "" then
set the_text to "Good morning." & return & "Today is " & date string of (current date) & return & return & "Unfortunately, you have no appointments today."
else
set the_text to "Good morning." & return & "Today is " & date string of (current date) & return & return & "Here are today's appointments." & return & return & the_text & return & "That was the last appointment for today."
end if
end tell
--
-- This is neat: I want to set the volume loud enough for me to hear it, and then set the volume back to where it was before I adjusted it.
set old_volume to output volume of (get volume settings)
set volume output volume 60
say the_text using "Alex"
set volume output volume (old_volume)
--
-- This is the sorting subroutine. I found it on MacScripter.net.
on findLeastItem(lst)
tell application "iCal"
set theLeast to start date of item 1 of lst
set theIndex to 1
set iterater to 1
repeat with i in lst
if start date of i theLeast then
set theLeast to start date of i
set theIndex to iterater
end if
set iterater to iterater + 1
end repeat

return theIndex
end tell
end findLeastItem

on removeItemAtIndex(lst, theIndex)
set newList to {}
set theLength to length of lst
if theLength = 1 then
set newList to {}
else if theLength = theIndex then
set newList to items 1 thru (theLength - 1) of lst
else if theIndex = 1 then
set newList to items 2 thru theLength of lst
else
set newList to items 1 thru (theIndex - 1) of lst & items (theIndex + 1) thru (theLength) of lst
end if
return newList
end removeItemAtIndex

on sortEvents(myList)
set myNewList to {}
repeat until length of myList = 0
set leastIndex to findLeastItem(myList)
set end of myNewList to item leastIndex of myList
set myList to removeItemAtIndex(myList, leastIndex)
end repeat
return myNewList
end sortEvents

Thursday, May 21, 2015

FREE job-hunting AppleScript

You might not have heard but there’s a recession on. People are looking for work. Even I, the great and powerful Christian Boyce, am being a little more proactive about being gainfully employed.

I had the brilliant idea of searching the job postings on www.craigslist.org every day for Mac-related stuff, but quickly found that craigslist is sort of picky about search terms, and if you aren’t careful, you’ll miss stuff. For example, if you’re looking for AppleScript jobs, and someone’s posted one that says “Wanted: AppleScripter” you WON’T find the job if you search for “AppleScript.” Don’t ask me why-- that’s just the way it is. Search for AppleScripter, and you find it. Search for AppleScript, and you don’t. (Search for “Apple” and you do. Search for “Apples” and you don’t.)

Frankly, I don’t quite understand it. But, understanding it is not our job here, and while it is tempting to try to figure out why craigslist works the way it does, it would be tangential to my original goal, which is to search for jobs on craigslist. Related, but off on a tangent.

I determined that if I searched for these terms I’d find what I wanted:

  • Apple
  • AppleScript
  • AppleScripter
  • Scripter
  • Mac
  • Macintosh
  • iPhone

That’s seven searches. Doable, but then I decided it would be nice to search craigslist in Austin, TX (austin.craigslist.org) as well as craigslist in Los Angeles, CA (losangeles.craigslist.org). Twice as many cities means twice as many searches-- now up to 14, and I could see that this would not be a lot of fun after the first day or two. And remember, I wanted to do this every day.

So, what do we do when we have a repetitive task? One option: pass it to someone else. That gets it out of my hands, but it’s not reasonable to expect anyone to do 14 searches perfectly every day. Mistakes get made when you have so much to do.

The correct answer, of course, is to make an AppleScript. Talk about practicing what you preach!

Here’s what I wrote, word for word. You can copy this script, paste it into Script Editor (you have it-- look in the AppleScript folder inside your Applications folder), and run it. You can change the cities and the search terms as you wish. (You can even change it to look for things other than jobs. I can help you with that.)

The script:

-- AppleScript by Christian Boyce, to search craigslist.org for jobs
-- Original version written May 21st, 2009.
-- May be copied and modified as desired. Let me know if you find it handy.
-- Write me at macman@ioperating.com
--
set the_cities to {"losangeles", "austin"}
set the_search_terms to {"apple", "applescript", "applescripter", "scripter", "mac", "macintosh", "iPhone"}
--
tell application "Safari"
activate
repeat with a_city in the_cities
repeat with a_search_term in the_search_terms
make new document at end of documents
-- the next two lines belong together-- from “set” to “a_search_term
set URL of document 1 to "http://" & a_city & ".craigslist.org/search/jjj?query=" & a_search_term
end repeat
end repeat
end tell



You can almost read it like a book. The gray italicized stuff is just comments, notes for us so that down the road we remember what we were doing. We set up a list of cities, using the terms craigslist uses in its URLs. Then we set up a list of search terms. Then, starting with the first city, we tell Safari to open a new window (document) and put a certain URL into it. (The first URL is “http://losangeles.craigslist.org/search/jjj?query=apple”.) And then we make another window for the next search term, then another etc. until we’ve done all the search terms for the first city. Then we go back and do it all for the second city (in this case, Austin).

The whole thing takes about 8 seconds to load 14 pages here. Plenty fast-- a lot faster than doing it by hand, and of course there are no typos.

The nice thing about this script is it’s easily modified. If I decide to search San Francisco, or Dallas, or San Diego, or Sacramento, all I have to do is add those cities to my city list (“the_cities”). If I want to search for other kinds of jobs (“snake handler”, “exotic dancer”) I can easily add those to my search terms (“the_search_terms”).

It’s going to save us a lot of time over here. Copy it and modify it for your own purposes (and if we apply for the same job remember who wrote the script for you).