timetobuildit
timetobuildit

Reputation: 55

Action buttons applescript functions

I am trying to make an AppleScript which creates a notification when I receive a specific email.

Rules are set up, and I have a script which launches an app with the following code:

display notification "A newBuild is now available!" with title "New Build"
tell application "Safari"
    activate
    tell window 1
        set current tab to (make new tab with properties URL:"https://latestSuccessful")
    end tell
    tell application "System Events"
        set visible of process "Safari" to true
    end tell
end tell

This launches safari and take to the the web page as soon as the notification is displayed.

Ideally I would display the notification as an Alert, with an action on the 'Show' button which then takes me to the web page. The 'Close button' naturally closes the alert.

Is this possible? Thanks in advance for any pointers.

Upvotes: 4

Views: 3165

Answers (3)

turingtested
turingtested

Reputation: 7154

Do you mean something like this?

set alertReply to display alert "New Build" message "A new build is now available!" buttons ["Cancel", "Show"] default button 2 giving up after 5
if button returned of alertReply is equal to "Show" then
    tell application "Safari"
        activate
        tell window 1
            set current tab to make new tab with properties {URL:"http://www.google.com"}
        end tell
    end tell
end if

Upvotes: 4

William T Froggard
William T Froggard

Reputation: 738

You can definitely do this! You'll have to install Growl for this. It used to be free, but costs $4 now. It's a pretty damn good long-term investment in my opinion, and it'll allow you to do awesome things with notifications in AppleScript. The code to use once it's installed is this:

set notifications to {"EMailNotify"}
tell application "Growl"
    register as application "AppleScript App" all notifications notifications default notifications notifications
    notify with name "EMailNotify" title "E-Mail received!" description "it's here!" application name "AppleScript App" callback URL "http://www.apple.com/"
end tell

Keep in mind that you must use one of the notification names in the list on the first line, as the name for the notification on the fourth line, and you must use the same application name that you register the application with on the third line, as the "application name" on the fourth line. Growl lets you specify a callback URL as an argument, which should get you exactly what you're looking for!

Keep in mind that you can configure Growl to use OS X notifications in order to get exactly what you were looking for, but Growl's notifications are far more customizable.

Upvotes: 0

vadian
vadian

Reputation: 285072

Notifications with action buttons are not possible in AppleScript,
because AppleScript can't handle the callbacks.

Upvotes: 3

Related Questions