Sunday, May 31, 2020

#1 2013-06-14 11:07:21 pm

oscar
Member
Registered: 2007-06-13
Posts: 11

AppleScript to Boxcar Notifications

I have recently written an applescript to send boxcar notifications. It would be accessed via a Terminal command:

osascript /path/to/script boxcar_username "Title of Notification" "Body of Notification" "URL/to/54x54icon"

This in turn maps to:

Applescript:

on run {BoxcarUSER, boxcartitle, boxcarmessage, BoxcarICONURL}

The BoxcarUSER variable is the only required one, the rest will use defaults. If you have your own provider, you can open the script in script editor and change the property at the top of the script(I currently have it setuo with a provider I created, so feel free to change that) Other defaults could be changed within the

Applescript:

set {

in the

Applescript:

run{}

handler.
Some of the error catching is rough, as if you do not have an internet connection, cURL will fail with a non zero status (I think, no real expert there) so the script can't catch the error message.

Some ideas I have thought about. Since you can map the icon that shows in boxcar, you could use different 57x57 icons to show the user different things that are happening. So maybe you could push out a red icon for stop, green for go, etc.

If someone could design a better icon, that would be great =p

**UPDATED WITH NEW VERSION**

Applescript:


--Usage on the command line would be:
--osascript path/to/script "Email-Address" "Title of Notification" "Message of Notification" "http://address_of_57x57_image"
--All these fields are optional but fields that want to use defaults should use ""

property AS2BoxcarVersion : "0.8.2"


(*
Versions:
0.8.2
Cleaned up code, made it more readable
Added "date" option to insert date into notification, so now boxcartitle or boxcarmessage can be "date" and this would return (current date)
0.8.1
Script now returns message when successful
0.8
Made all fields optional, expect for BoxcarUSER, and BoxcarDefaultAPIKey. These 2 are required for the script to function
0.7
Made the icon Boxcar shows passable in terminal, see second line of script for syntax
0.6:
Added Support if boxcar API service is down
0.5:
Initial Release, works as expected, just no features

*)


--on run {}
--    my test123("identd@me.com", "date", "date", "")
--end run

on run {BoxcarUSER, boxcartitle, boxcarmessage, BoxcarICONURL}
   
   
   --reset variables, this is where you can set your defaults, remember these should all be strings!
   set {BoxcarDefaultTitle, BoxcarDefaultMessage, BoxcarDefaultICON, BoxcarDefaultAPIKey} to {"Boxcar Version: " & AS2BoxcarVersion, "Yup, This all looks good!", "[url]http://i39.tinypic.com/b85nd4.jpg[/url]", "BHyitk1NJ37xRLKg6rnQ"}
   --end reset
   --    display dialog boxcartitle as string
   if boxcartitle is "" then
       set boxcartitle to BoxcarDefaultTitle
   else if boxcartitle is "date" then
       set boxcartitle to ((current date) as text)
   end if
   --    display dialog boxcartitle as string
   if boxcarmessage is "" then
       set boxcarmessage to BoxcarDefaultMessage
   else if boxcarmessage is "date" then
       set boxcarmessage to ((current date) as text)
   end if
   if BoxcarICONURL is "" then
       set BoxcarICONURL to BoxcarDefaultICON
   end if
   
   set SubscribeServiceResponse to (do shell script "curl -d \"email=" & BoxcarUSER & "\" \\
[url=http://boxcar.io/devices/providers/]http://boxcar.io/devices/providers/"
[/url] & BoxcarDefaultAPIKey & "/notifications/subscribe")
   
   delay 1
   
   set boxcartitle to ListToString(stringtolist(boxcartitle, " "), "+")
   set boxcarmessage to ListToString(stringtolist(boxcarmessage, " "), "+")
   
   try
       set BoxcarResponse to (do shell script "curl -d \"emails=" & BoxcarUSER & "\" \\
-d \"¬ification[from_screen_name]="
& boxcartitle & "\" \\
-d \"¬ification[message]="
& boxcarmessage & "\" \\
-d \"¬ification[icon_url]="
& BoxcarICONURL & "\" \\
-d \"¬ification[from_remote_service_id]="
& my md5create(((current date) & BoxcarUSER & BoxcarDefaultTitle & BoxcarDefaultMessage as text)) & "\" \\
[url=http://boxcar.io/devices/providers/]http://boxcar.io/devices/providers/"
[/url] & BoxcarDefaultAPIKey & "/notifications")
       if BoxcarResponse contains "favicon.ico" then
           return ("AS2Boxcar v" & AS2BoxcarVersion & ": Boxcar API Service is temporarily down. Error 1")
       end if
       if BoxcarResponse contains "Could not resolve host" then
           return ("AS2Boxcar v" & AS2BoxcarVersion & ": Unable to resolve [url]http://boxcar.io[/url] Please check your internet connection.")
       end if
   on error errmsg
       return ("AS2Boxcar v" & AS2BoxcarVersion & ": Boxcar API Service is temporarily down. Error 2")
   end try
   --return ("AS2Boxcar " & AS2BoxcarVersion & ": Notification sent to " & BoxcarUSER)
   return ("AS2Boxcar " & AS2BoxcarVersion & ": " & BoxcarUSER & " : " & boxcartitle & " : " & boxcarmessage & " : " & BoxcarICONURL)
end run

--SUBS--
on stringtolist(theString, delim)
   set oldelim to AppleScript's text item delimiters
   set AppleScript's text item delimiters to delim
   set dlist to (every text item of theString)
   set AppleScript's text item delimiters to oldelim
   return dlist
end stringtolist

on ListToString(theList, delim)
   set oldelim to AppleScript's text item delimiters
   set AppleScript's text item delimiters to delim
   set alist to theList as string
   set AppleScript's text item delimiters to oldelim
   return alist
end ListToString

on md5create(stringtohash)
   set hashedBoxcarUSER to item 2 of stringtolist((do shell script "md5 -s \"" & stringtohash & "\""), " = ")
   return hashedBoxcarUSER
end md5create

Last edited by oscar (2013-07-04 02:09:02 pm)


Filed under: API, AS2Boxcar, Boxcar

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)