3 Quick AppleScripts to Save You Time

AppleScript is an automation solution that allows you to handle tasks quickly and efficiently without a lot of thought or effort on your part. This can be repetitive things you may want to take care of in short order or this might simply be stuff you want to do from anywhere regardless of what app you are in. Whatever the case, AppleScript can usually be employed for such tasks.

I enjoy solving process streamlining needs on my own with AppleScript and I am not one to keep good things to myself so here are just 3 little scripts I would like to share. You might have a use for them and they might just make your day easier.

Toggle apps on or off

If you’re like me you might have one or two apps that do all that you need them to just by turning them on or off. So why leave them running in the background all the time? Two apps that I can think of in my list are Nocturne and Isolator. I use them from time to time but not often enough to keep them in the menu bar permanently. Activating their features requires me to select their icon from the menu bar and selecting the option that toggles their behavior on or off. Or if you just leave their behavior toggled on then quit and restart the app, it has the exact same effect. Quitting the app turns the feature off (obviously) and starting the app turns the feature on…

Sounds like a job for AppleScript:

-- replace "YourAppName" with your app
set yourApp to "YourAppName"

-- leave the rest to us
if appIsRunning(yourApp) then
    tell application yourApp to quit
    tell application yourApp to activate
end if

on appIsRunning(appName)
    tell application "System Events" to ¬
        (name of processes) contains appName
end appIsRunning

By changing the name of “YourAppName” to the app you want to toggle on or off and then setting yourself a keyboard short-cut for the script, you can then activate the app and it’s only desired feature with a stroke of the keys.


Get your fresh copy of “On-Off_AppToggle” from CodeCollector.net

Simple Web Search

Google Web Search AppleScriptDo you remember when a query to a search engine was a simple string that looked like http://www.google.com/search?q=this%20or%20that? Now-a-days search strings tend to include all sorts of information like the source of the search, encoding, language, client, OS… and ends up like and unreadable bunch of garbage like this http://www.google.com/search?source=qsb-mac&oe=UTF-8&hl=en&q=this%20or%20that&client=qsb-mac&ie=UTF-8. I for one don’t need every one of my queries to be counted so I wrote a simple script to pose that basic string once again. For your own purposes you can change the search engine and browser to suit your needs.

set userQuery to text returned of ¬
    (display dialog "Google search:" ¬
        default answer "" buttons {"Cancel", "OK"} ¬
        default button 2)

set httpArray to ¬
    {"http://", "www.", ".com", ".ca", ".net", ".org", ".info", ".us"}

set httpBool to false
repeat with httpAny in httpArray
    if userQuery contains httpAny then
        set httpBool to true
        exit repeat
    end if
end repeat

if httpBool then
    tell application "Safari" to open location userQuery
    tell application "Safari" to open location ¬
        "http://www.google.com/search?q=" & userQuery
end if
tell application "Safari" to activate

When you launch this script you just enter your search query and press return.


r4 10-29-09 09:43 – It would be handy if you could use this script to open known urls to, so I’ve edited the script to do so in a basic manner. It’s not perfect yet as I need to write some regular expressions to really detect proper TLDs and such. But it will do for now. So, perform a query and Safari will open that query in as a Google search. Or, enter a URL and Safari will directly open that website.


Get your fresh copy of “Google_Search” from CodeCollector.net

Launch apps by task

Launch_Task_Apps-20091013-211940My days, weeks and even months can often be broken into many different tasks. I wear a lot of hats as a small business owner. One day I might be providing support for the RapidWeaver themes I make and the next day I will be knee deep in code making the latest greatest product. One minute I am social networking and the next I am designing something shiny.

Each of these tasks involves a unique set of apps to be open which can make changing gears a time consuming — if not confusing — process.

To address this I wrote a script that launches (or quits) a particular set of apps based on the task I need to do. For example:

  • If I am busy with support, I want to launch:
    • Mailplane
    • Safari
    • TextMate
  • If I am networking I will use:
    • Safari
    • Adium
    • LimeChat
    • Mailplane
    • TweetDeck
  • If I am designing I want:
    • DigitalColor Meter
    • LittleSnapper
    • Photoshop
  • Or if I am developing a new theme:
    • RapidWeaver
    • Safari
    • TextMate
    • MAMP

And so on… As mentioned, I can use the script to either launch these sets of apps or quit them and I can do so with a single keyboard-shortcut. This saves a lot of time and helps keep the clutter to a minimum. You can fill in your own task sets and your own apps to suit.

-- define the sorts of tasks you wish to activate multiple apps for
set taskArray to {"Surfing", "Media", "Designing"}

set taskName to ¬
    {choose from list taskArray with prompt ¬
        "Pick your process:"} ¬
            as string
set toDoArray to {"Launch apps", "Quit apps"}
set toDoResult to ¬
    {choose from list toDoArray with prompt ¬
        "Would you like to launch or quit these applications?"} ¬
            as string

if taskName is equal to "Surfing" then

    -- define the apps you would like to open/close
    set appArray to ¬
        {"Safari", "LittleSnapper", "Mail"}

else if taskName is "Media" then

    -- define the apps you would like to open/close
    set appArray to ¬
        {"iTunes", "Last.fm", "DVD Player"}

else if taskName is "Designing" then

    -- define the apps you would like to open/close
    set appArray to ¬
        {"Adobe Photoshop CS3", "DigitalColor Meter", "LittleSnapper"}

    display dialog ¬
        "Something is wrong! Please check your array values."
end if

-- runTrue function
on runTrue(appArray,toDoResult)
    repeat with appName in appArray
        if toDoResult is "Launch apps" then
            tell application appName to activate
        else if toDoResult is "Quit apps" then
            tell application appName to quit
            display dialog "Houston, we have a problem!"
        end if
    end repeat
end runTrue


Get your fresh copy of “LaunchTaskApps” from CodeCollector.net

Go play

I hope you can make use of these scripts in your own daily workflow. I know I would be lost without them. Do have any scripts of your own you want to share? Feel free to comment and let me know.