Use applescript to open pop-up?

Discussion in 'Mac Programming' started by winterdude010, Jul 28, 2008.

  1. winterdude010 macrumors regular

    winterdude010

    Joined:
    Feb 24, 2006
    Location:
    New York
    #1
    Hi.

    Just noticed the ability to use applescripts in iChat 4.0 so I was wondering how I could just create a script to use that would open a pop-up window saying something simple like "Buddy has logged in!" to use for one of my AIM buddies.

    Thanks!
     
  2. kanenas macrumors newbie

    Joined:
    Jun 20, 2008
    #2
    Use "display alert" or growl

    You could use display alert for the notification or use Growl (which is less intrusive).

    For anyone but the OP, note that you need at least iChat 4.0 for the following to work. I still have 3.1, so the code isn't completely tested.

    Code:
    property myNotifications : {budAvail:{title:"Buddy Available", action:" is available"}, budUnavail:{title:"Buddy Unavailable", action:" went away"}, invitation:{title:"Invitation", action:" invites you to chat"}, textInvite:{title:"Text Invitation", action:" wants to chat"}, audInvite:{title:"Audio Invitation", action:" wants to talk"}, vidInvite:{title:"Video Invitation", action:" invites you to a video chat"}, generic:{title:"Generic", action:""}}
    property myAppName : "iChat"
    property shouldInit : true
    
    if shouldInit then init()
    
    using terms from application "iChat"
    	--on budAvail(bud)
    	on buddy became available bud
    		notify of (budAvail of myNotifications) by bud
    	end buddy became available
    	--end budAvail
    	
    	--on budUnavail(bud)
    	on buddy became unavailable bud
    		notify of (budUnavail of myNotifications) by bud
    	end buddy became unavailable
    	--end budUnavail
    	
    	on notify of notif by bud
    		set uName to full name of bud
    		--set uName to name of bud
    		set msg to uName & (action of notif)
    		set img to a reference to image of bud
    		growl(title of notif, msg, image of bud)
    	end notify
    end using terms from
    
    on growl(notif, descr, img)
    	tell application "GrowlHelperApp"
    		try
    			notify with name notif ¬
    				title notif ¬
    				description descr ¬
    				application name myAppName ¬
    				image img
    		on error
    			notify with name notif ¬
    				title notif ¬
    				description descr ¬
    				application name myAppName
    		end try
    	end tell
    end growl
    
    on init()
    	set shouldInit to false
    	set myNotificationList to {}
    	repeat with bar in myNotifications as list
    		copy title of bar to end of myNotificationList
    	end repeat
    	
    	tell application "System Events"
    		if (count of (every process whose name is "GrowlHelperApp")) > 0 then
    			tell application "GrowlHelperApp"
    				register as application ¬
    					myAppName all notifications myNotificationList ¬
    					default notifications myNotificationList ¬
    					icon of application myAppName
    			end tell
    		end if
    	end tell
    end init
    
    Some of the above code is original work and some came from sources I can't remember. I only added handlers for the buddy available & unavailable events, but it's tremendously easy to add other handlers & notifications.


    Just found two other solutions, "Topic : Script for text edit, ichat" and on this very site.
     
  3. lancestraz macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #3
    You can set an Applescript to automatically run when a specified buddy come online by right clicking on him/her in the iChat window and selecting "Get Info" and the "Alerts". You could point it to a simple script like this:
    Code:
    tell application "iChat"
    activate
    display dialog "What's his name is now online." buttons {"OK"} default button 1
    end tell
    Or use the growl method above.
     
  4. kanenas macrumors newbie

    Joined:
    Jun 20, 2008
    #4
    Bugfix

    Already there's a bug and a fix. The first version of my script keeps appending to myNotificationList, which thus grows linearly with each run. Here's the new code, which should replace the myNotificationList property, repeat and "System Events" tell:
    Code:
    property shouldInit : true
    
    if shouldInit then init()
    
    on init()
    	set shouldInit to false
    	set myNotificationList to {}
    	repeat with bar in myNotifications as list
    		copy title of bar to end of myNotificationList
    	end repeat
    	
    	tell application "System Events"
    		if (count of (every process whose name is "GrowlHelperApp")) > 0 then
    			tell application "GrowlHelperApp"
    				register as application ¬
    					myAppName all notifications myNotificationList ¬
    					default notifications myNotificationList ¬
    					icon of application myAppName
    			end tell
    		end if
    	end tell
    end init
    
    The original posting has been updated
     
  5. Benjamindaines macrumors 68030

    Benjamindaines

    Joined:
    Mar 24, 2005
    Location:
    A religiously oppressed state
    #5
    I have your script set up to run when a buddy becomes available and unavailable. It works fine when a buddy becomes available; however when a buddy becomes unavailable I get this error:

    [​IMG]
     
  6. kanenas macrumors newbie

    Joined:
    Jun 20, 2008
    #6
    Bugfix #2

    Whoops... a typo. Change
    Code:
    on buddy became unavailable theBuddy
    to
    Code:
    on buddy became unavailable bud
    The code in the original posting has been updated.
     
  7. Benjamindaines macrumors 68030

    Benjamindaines

    Joined:
    Mar 24, 2005
    Location:
    A religiously oppressed state
    #7
    Thanks, works now. =]
     
  8. Bonatapus macrumors newbie

    Joined:
    Nov 3, 2008
    #8
    I'm an applescript noob, could you explain how i could edit the message it sends to Growl so that it is one line that says, Buddy Name + "went offline." (for when the buddy logs off) and, Buddy Name + "came online." (for when the buddy logs on)
     
  9. kanenas macrumors newbie

    Joined:
    Jun 20, 2008
    #9
    myNotifications is the key

    The short answer is to edit the "myNotifications" record.

    With a little bit of work, you can learn how to figure this out yourself. The quickest way is to look within the script for the text of one of the growl messages. Say your friend Fred comes online and you get the Growl notification "Fred Derf is available." The name "Fred Derf" will change from notification to notification, but "is available" should be the same every time a buddy comes online. Looking for "is available", we find it in myNotifications.

    If the quick method doesn't work for a particular script (for instance, there's not enough text that remains constant from notification to notification), there's a slightly more involved way: trace handler calls. Growl notifications are posted by sending a "notify" message to Growl. Even if you didn't know this, the "growl(..)" handler in this script seems a likely place to start. Indeed, the "growl(...)" handler sends a "notify" message to "GrowlHelperApp".
    Code:
    on growl(notif, descr, img)
    	tell application "GrowlHelperApp"
    		try
    			notify with name notif ¬
    				title notif ¬
    				description descr ¬
    				application name myAppName ¬
    				image img
    Examining the code, the message is passed in the 2nd argument ("descr") to "growl(notif, descr, img)". "growl(...)" is called in the script's own "notify" handler.
    Code:
    	on notify of notif by bud
    		set uName to full name of bud
    		set msg to uName & (action of notif)
    		set img to a reference to image of bud
    		growl(title of notif, msg, image of bud)
    	end notify
    Here we see the value for "descr" comes from "msg" and is "uName & (action of notif)". "uName" comes from the buddy information ("full name of bud") and is provided by iChat; "notif" is an argument to "notify", and its action property provides the message text. In other words, the Growl message is the buddy's full name prepended to a string that depends on what the buddy has done (the "buddy event"). To change the message, we need to find where that string comes from. Now we examine a call to "notify", such as when a buddy comes online.
    Code:
    	on buddy became available bud
    		notify of (budAvail of myNotifications) by bud
    	end buddy became available
    
    Here we see that "notif" is item "budAvail" of "myNotifications". Now we have our answer: to change the message, find the item of "myNotifications" that corresponds to the buddy event, and change the value for its "action" property.

    Once you absorb this, you can figure out what needs to be changed in other's scripts on your own.

    To learn a little more about AppleScript, read MacWorld's "Using Tiger: Learning AppleScript" article. If you really want to learn how to use AppleScript, read Apple's documentation
     
  10. Bonatapus macrumors newbie

    Joined:
    Nov 3, 2008
    #10
    Wow thanks much for the very in-depth response.
     

Share This Page