Welcome Guest Login Register Member List
ExpressionEngine Forums
Advanced Search
Username: Password:
Remember Me? forgot password?
You are here: Forum Home  >  Usage  >  Tips and Tricks  >  Thread
   
 
Applescript and the API
 
fabsmartins
Posted: 31 March 2008 09:21 AM   [ Ignore ]  
Newbie
Rank
Total Posts:  5
Joined  2008-03-26

Hi all,
i am by no means a geek, i have no experience with applescript and coding. But one of the things which i want to do was to pick up an email from mail.app, set it to my default context, and allow me to create the description, or set it to the default “tricks” sender&title;.

Also i wanted to add a link to the original message from within mail (same as yojimbo link or kind of i guess).

So i wasted a fair bit of my time and i managed to come up with the following script.

NOTE: this was heavily borrowed from the original “tricks” section within TRACKS….

tell application "Mail"
    
set theSelection to the selection
    
if the length of theSelection is less than 1 then
        display dialog 
"One or more messages must be selected." buttons {"OK"} default button 1 with icon caution
    
else
        
repeat with theMessage in theSelection
            my importMessage
(theMessage)
        
end repeat
    end 
if
end tell

on importMessage
(theMessage)
    
    -- 
Get the info from the email message
    tell application 
"Mail"
        
try
            
set theSender to the sender of theMessage
            set theSubject to subject of theMessage
            
if theSubject is equal to "" then set theSubject to emptySubject
            
            
-- Construct the description string from the email info
            display dialog 
"Description of action for this email:" default answer ""
            
set myDesc to text returned of the result
            
if the length of myDesc is less than 1 then
                set myDesc to 
"Email " & theSender & " about " & theSubject
                
-- Trim the string to 100 characters otherwise it won't validate
                if length of myDesc > 100 then
                    set myDesc to characters 1 thru 100 of myDesc
                end if
            end if
        end try
        display dialog "message" default answer ""
        set _mes to text returned of the result
        set _sel to get selection
        set _links to {}
        repeat with _msg in _sel
            set _messageURL to _mes & "message" & _mes & ":message://<" & _msg'
s message id & ">"
            
set end of _links to _messageURL
        end repeat
        
    end tell
    
    
-- Now send all that info to Tracks
    
-- Edit the URL of your Tracks installation if necessary"
    set myUsername to "
place_your_name_here"
    set myToken to "
your_token"
    set myContextID to 1
    set myNotes to _messageURL
    tell application "
http://0.0.0.0:3000/backend/api"
        
set returnValue to call xmlrpc {method name:"NewTodo", parameters:{myUsername, myToken, myContextID, myDesc, myNotes}}
    end tell
    
    
(* Growl support - comment out or delete this section if 
       
you don't have Growl *)
    tell application "GrowlHelperApp"
        set the allNotificationsList to Â¬
            {"Tracks Notification"}
        
        -- Make a list of the notifications 
        -- that will be enabled by default.      
        -- Those not enabled by default can be enabled later 
        -- in the '
Applications' tab of the growl prefpane.
        set the enabledNotificationsList to Â¬
            {"Tracks Notification"}
        
        -- Register our script with growl.
        -- You can optionally (as here) set a default icon 
        -- for this script'
s notifications.
        
register as application Â¬
            
"Tracks Applescript" all notifications allNotificationsList Â¬
            
default notifications enabledNotificationsList Â¬
            icon of application 
"Script Editor"
        
set growlDescription to "Action with ID " & returnValue & " was created."
        
notify with name "Tracks Notification" title "New action sent to Tracks" description growlDescription application name "Tracks Applescript" icon of application "Script Editor.app"
    
end tell
    
(* End of Growl section *)
    
end importMessage 

Well, kudos to those who wrote and came up with most of this code and i hope you guys could add smile

Thanks
Fabs

Profile
 
fabsmartins
Posted: 01 April 2008 09:03 AM   [ Ignore ]   [ # 1 ]  
Newbie
Rank
Total Posts:  5
Joined  2008-03-26

Ahhh elegance….

I have just gotten a bit more help from friends (and bsag) and finally i learned about quotation marks.

without further ado here is the update for more elegance….

tell application "Mail"
    
set theSelection to the selection
    
if the length of theSelection is less than 1 then
        display dialog 
"One or more messages must be selected." buttons {"OK"} default button 1 with icon caution
    
else
        
repeat with theMessage in theSelection
            my importMessage
(theMessage)
        
end repeat
    end 
if
end tell

on importMessage
(theMessage)
    
    -- 
Get the info from the email message
    tell application 
"Mail"
        
try
            
set theSender to the sender of theMessage
            set theSubject to subject of theMessage
            
if theSubject is equal to "" then set theSubject to emptySubject
            
            
-- Construct the description string from the email info
            display dialog 
"Description of action for this email:" default answer ""
            
set myDesc to text returned of the result
            
if the length of myDesc is less than 1 then
                set myDesc to 
"Email " & theSender & " about " & theSubject
                
-- Trim the string to 100 characters otherwise it won't validate
                if length of myDesc > 100 then
                    set myDesc to characters 1 thru 100 of myDesc
                end if
            end if
        end try
        set _sel to get selection
        set _links to {}
        repeat with _msg in _sel
            set _messageURL to "\"message\":" & "message://<" & _msg'
s message id & ">"
            
set end of _links to _messageURL
        end repeat
        
    end tell
    
    
-- Now send all that info to Tracks
    
-- Edit the URL of your Tracks installation if necessary"
    set myUsername to "
fabsmartins"
    set myToken to "
f1e32bcd0b61433dd031e823fb2f6afc10fc869e"
    set myContextID to 1 (* YD *)
    set myNotes to _messageURL
    tell application "
http://0.0.0.0:3000/backend/api"
        
set returnValue to call xmlrpc {method name:"NewTodo", parameters:{myUsername, myToken, myContextID, myDesc, myNotes}}
    end tell
    
    
(* Growl support - comment out or delete this section if 
       
you don't have Growl *)
    tell application "GrowlHelperApp"
        set the allNotificationsList to Â¬
            {"Tracks Notification"}
        
        -- Make a list of the notifications 
        -- that will be enabled by default.      
        -- Those not enabled by default can be enabled later 
        -- in the '
Applications' tab of the growl prefpane.
        set the enabledNotificationsList to Â¬
            {"Tracks Notification"}
        
        -- Register our script with growl.
        -- You can optionally (as here) set a default icon 
        -- for this script'
s notifications.
        
register as application Â¬
            
"Tracks Applescript" all notifications allNotificationsList Â¬
            
default notifications enabledNotificationsList Â¬
            icon of application 
"Script Editor"
        
set growlDescription to "Action with ID " & returnValue & " was created."
        
notify with name "Tracks Notification" title "New action sent to Tracks" description growlDescription application name "Tracks Applescript" icon of application "Script Editor.app"
    
end tell
    
(* End of Growl section *)
    
end importMessage 

Cheers
Fabs

Profile
 
   
 
 
‹‹ Create Tasks in Tracks via Email using the RESTFul API      simplied action items page? ››

Powered By ExpressionEngine
Template Design By Sonnenvogel.com
Select a theme:

ExpressionEngine Discussion Forum - Version 2.1.2 (20091002)
Script Executed in 0.1637 seconds

Atom Feed
RSS 2.0