Apple Event Handler Failure while trying to growl - ruby

I'm trying to growl from Ruby/Appscript, based on this sample Applescript code:
tell application "GrowlHelperApp"
set the enabledNotificationsList to {"Mail Notification"}
register as application "MailWidgetGrowlHelper" all notifications enabledNotificationsList default notifications enabledNotificationsList icon of application "Mail.app"
notify with name "Mail Notification" title (item 1 of argv) description (item 2 of argv) & return & return & (item 3 of argv) application name "MailWidgetGrowlHelper" icon of application "Mail.app" without sticky
end tell
My code looks like this:
GH = app("GrowlHelperApp.app")
enabledNotifications = "AppscriptMessage"
GH.register(:as_application => "AppscriptHelper",
:all_notifications => enabledNotifications,
:default_notifications => enabledNotifications)
the last call fails with:
Appscript::CommandError: CommandError OSERROR: -10000 MESSAGE: Apple event handler failed.
any ideas what I'm doing wrong?

D'Oh. The enabledNotifications should be an Array, not just a string.

Related

How do I pause and resume an AppleScript without a dialog box?

I am working to outline a workflow in AppleScript. The script takes the next task I need to do from Omnifocus and requires me to determine if I can do it in 2 minutes or less. If I can, it starts a timer and I want it to wait until I actually do the task. Right now I have a dialog box pop up and I can mark the task complete when I am done with it. Unfortunately, some of the tasks I need to do are in Omnifocus and I can't do anything in Omnifocus with the dialog box open.
I would like to avoid using the dialog box so I can work in Omnifocus while the script is running. I'd like to be able to tell the script I'm done so it can stop the timer, tell me how long it took to do the task and then go on to mark the task complete in Omnifocus. I initially thought the best way to do this would be by entering a key combination. After a bit of research, I don't think I can do this in AppleScript. I am open to any idea of how to allow me to work in the middle of my script and then tell the program I am done with the task.
Here's my code:
on run {}
with timeout of (30 * 60) seconds
tell application "OmniFocus"
activate
end tell
tell application "OmniFocus"
tell default document to tell front document window
set perspective name to "Daily Wrap-Up"
tell content to set TreeList to (value of first leaf)
repeat with ListItem in TreeList
set ProjectName to name of containing project of ListItem as text
set TaskName to " - " & name of ListItem
set NoteName to " - " & note of ListItem
display dialog "The task is:" & return & ProjectName & TaskName & NoteName & return & "Can you do this in 2 minutes or less?" buttons {"Yes", "No"} default button "Yes"
set Button_Returned to button returned of result
if Button_Returned = "Yes" then
say "Get to work!"
set T1 to minutes of (current date)
set T1s to seconds of (current date)
display dialog "Click when done." buttons {"Complete", "Cancel"} default button "Complete"
set Button_Returned to button returned of result
if Button_Returned = "Complete" then
set T2 to minutes of (current date)
set T2s to seconds of (current date)
set TT_ to ((T2 * 60) + T2s) - ((T1 * 60) + T1s)
say "that took you" & TT_ & "seconds to complete"
display dialog ProjectName & TaskName & NoteName buttons {"Complete", "Defer", "Cancel"} default button "Complete"
set Button_Returned to button returned of result
if Button_Returned = "Complete" then
mark complete ListItem
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "Defer" then
display dialog "Defer for how long (in minutes)?" default answer "60"
set TimeAdd to text returned of result
set defer date of ListItem to ((current date) + (TimeAdd * minutes))
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "Cancel" then
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "No" then
tell application "OmniFocus"
compact default document
end tell
end if
else if Button_Returned = "No" then
display dialog "Breakdown task."
set perspective name to "Projects"
end if
end if
end repeat
end tell
end tell
end timeout
end run
Thanks in advance for any help.
I don't have OmniFocus on my machine, so I can't properly compile this much less test it, but in vanilla AppleScript you can do something like the following:
global start_time, end_time, TreeList, current_task_index, TaskName, NoteName
on run
tell application "OmniFocus"
tell default document to tell front document window
set perspective name to "Daily Wrap-Up"
tell content to set TreeList to (value of first leaf)
end
end
set current_task_index to 1
beginTask()
end
on reopen
-- inserted try block to aid debugging
try
set end_time to (current date)
set elapsed_time to end_time -start_time
say "that took you " & elapsed_time & " seconds to complete"
display dialog ProjectName & TaskName & NoteName buttons {"Complete", "Defer", "Cancel"} default button "Complete"
set Button_Returned to button returned of result
if Button_Returned = "Complete" then
mark complete ListItem
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "Defer" then
display dialog "Defer for how long (in minutes)?" default answer "60"
set TimeAdd to text returned of result
set defer date of ListItem to ((current date) + (TimeAdd * minutes))
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "Cancel" then
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "No" then
tell application "OmniFocus"
compact default document
end tell
end if
else if Button_Returned = "No" then
display dialog "Breakdown task."
set perspective name to "Projects"
end if
set current_task_index to current_task_index + 1
if current_task_index <= count of TreeList then
beginTask()
else
quit
end
on error errstr number errnum
display alert "Error " & errnum & ": " & errstr
end try
end
on idle
(*
you can use this handler if you want the app to give you a countdown, or
announce a time limit, or anything that needs doing while you're working on the task
*)
end
on beginTask()
tell application "OmniFocus"
tell default document to tell front document window
set perspective name to "Daily Wrap-Up"
set ListItem to item current_task_index of TreeList
set ProjectName to name of containing project of ListItem as text
set TaskName to " - " & name of ListItem
set NoteName to " - " & note of ListItem
display dialog "The task is:" & return & ProjectName & TaskName & NoteName & return & "Can you do this in 2 minutes or less?" buttons {"Yes", "No"} default button "Yes"
set Button_Returned to button returned of result
if Button_Returned = "Yes" then
say "Get to work!"
set start_time to (current date)
end if
end tell
end tell
end
Copy this into the script editor, debug it, and save it as an application with the 'Stay open after run handler' checkbox checked. Operation is as follows:
When OmniFocus is ready, run this script application. It will prompt you to begin the first task.
When you have finished the first task, double-click the script application icon again to invoke the reopen handler. The script will present you with the elapsed time for the first task, give you the choices you outlined, and then prompt you to begin the second task.
After the last task is complete, the script will automatically quit.
The global variable at the beginning allow necessary data to be passed between handlers as the script progresses.
If you want something more complex — e.g., a floating window or menu bar item that gives you more fine-grained control — then you'll need to start using ASOC to build that up. But that's fine-tuning; this should give you a general structure.

"tell application 'Mail'" won't accept string from IBOutlet in AppleScript-ObjC app

I'm working on an AppleScript-ObjC application that generates an email message in Mail. My UI has three IBOutlets (two text fields and a pop-up menu) where users can enter text that will be filled into the email. I can save the values from those outlets into variables, but when I try to use those variables inside my tell application "Mail" statement, I get this error:
AppleEvents: received mach msg which wasn't complex type as expected
in getMemoryReference.
And here's what prints in the log:
(
"<NSAppleEventDescriptor: 'ctxt'>",
": ",
"Status: MyProjectName, Part No.: 12345"
)
It seems like there's a difference between ctxt (which I think is an NSString) and an AppleScript string, but I can't figure out how to convert to an AppleScript string. Please let me know how if you do.
Here's the code for the whole function:
-- IBOutlets
property theWindow : missing value
property statusMenu : missing value
property partNumberField : missing value
property projectNameField : missing value
on generateButtonClicked:sender
set projectName to projectNameField's stringValue() as text
set partNumber to partNumberField's stringValue() as text
set status to statusMenu's objectValueOfSelectedItem as text
set theSubject to (status & ": " & projectName & ", Part No.: " & partNumber) as string
log (class of theSubject) & ": " & theSubject
tell application "Mail"
try
set newMessage to make new outgoing message with properties {subject: theSubject, theContent: "", visible: true}
on error e
log e
end try
activate
end tell
end generateButtonClicked:
set newMessage to make new outgoing message ¬
with properties {subject: theSubject, theContent: "", visible: true}
theContent isn't a property of outgoing message. Change it to content.

Process emails from mac using javascript (JXA) instead of applescript

I am trying to make an email parser in JXA. I was able to do something similar in applescript:
tell application "Mail" to set theMessages to every message of mailbox "Inbox" of account "MyAcount" whose subject is equal to "Search Text"
repeat with aMessage in theMessages
tell application "Mail" to set {mContent, mDate} to {content, date received} of aMessage
......process each mail.....
end repeat
How would I replicate this but in javascript?
Perhaps with something like this. Hope this helps.
(() => {
'use strict';
const
appMail = Application('Mail'),
account = appMail.accounts.byName('MyAccount'),
inbox = account.mailboxes.byName('INBOX'),
listMessages = inbox.messages.whose({
subject: 'Search Text'
})();
return listMessages.map(x => {
const [mContent, mDateReceived] = [x.content(), x.dateReceived()];
...
}
)
})();

Cannot set outlook message body on compose mode using appleScript

I am trying to get and set outlook message body on compose mode.setting value does not work. is something wrong with script ?. but get value is working fine.
activate application "Microsoft Outlook"
tell application "System Events"
tell process "Microsoft Outlook"
get value of static text 1 of group 1 of UI element 1 of scroll area 4 of splitter group 1 of window 1
set value of static text 1 of group 1 of UI element 1 of scroll area 4 of splitter group 1 of window 1 to "sample text"
end tell
end tell
I would avoid using UI scripting unless there is no other way.
This script shows you how to set the message body.
tell (current date) to get (it's month as integer) & "-" & day & "-" & (it's year as integer)
set MyDay to the result as text
set Mytitle to "Daily Email - " as text
set Mytitle to Mytitle & MyDay
tell application "Microsoft Outlook"
set newMessage to make new outgoing message with properties {subject:Mytitle}
make new recipient at newMessage with properties {email address:{name:"Name", address:"test#example.com"}}
#make new cc recipient at newMessage with properties {email address:{name:"Name", address:"test#example.com"}}
--- SET EMAIL BODY USING PLAIN TEXT ---
set plain text content of newMessage to "This is just a TEST."
open newMessage
end tell

Why doesn't this simple applescript work any more in ML mail.app (as a rule)

It works only when you right click on the mail message and choose "run rules", but not on incoming messages (without interaction).
The first dialog is shown both when incoming or running manually, but the second dialog (with the id) is only shown when running manually. Nothing is shown in console.log
Any ideas?
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with theMessage in theMessages
display dialog "inside"
set theId to id of theMessage
display dialog "the id is " & theId
end repeat
end tell
end perform mail action with messages
end using terms from
update: i added a try catch block around
set theId to id of theMessage
and this is the error I get:
Can't get class mssg 1 of class mbxp "Incoming POP messages" of class mact "Telenet". Invalid index. -1719
Any idea what this means? I don't get the error when applying rules manually.
Update 2: OK i found out that incoming messages don't have an ID yet. That's a problem since I want to save the email to disk:
set theEmail to (do shell script "mdfind -onlyin ~/Library/Mail \"kMDItemFSName = '" & theId & ".emlx'\"")
set archiveName to theId & "-" & (extract address from theMessage's sender) & ".emlx"
set saveLocation to "Users:wesley:Documents:Incoming:"
do shell script "cp '" & theEmail & "' '" & POSIX path of saveLocation & "';"
Is there any way around this?
As promised in my comment, here's an AppleScript that logs Mail messages' subject, id, and message id to ~/Desktop/log.txt.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
try
repeat with theMessage in theMessages
LogText("subject: " & (theMessage's subject as string))
LogText("id: " & (theMessage's id as string))
LogText("message id: " & (theMessage's message id as string))
LogText("")
end repeat
on error errorText number errorNumber
tell application "Mail" to display alert ("Error: " & errorNumber) message errorText
return
end try
end perform mail action with messages
end using terms from
on LogText(theText)
tell application "Finder"
set logPath to (path to the desktop as text) & "log.txt"
try
set writeText to open for access file logPath with write permission
set currentDateAsString to do shell script "date +\"%Y-%m-%d %T\""
write (currentDateAsString & " " & (theText as text) & return) to writeText starting at ((get eof writeText) + 1)
close access writeText
on error errorText number errorNumber
close access writeText
error errorText number errorNumber
end try
end tell
end LogText
I'm also running OS X 10.8.2 and Mail 6.2. As I said, I'm surprised to say this, but triggering the above AppleScript via mail rule works just as well for incoming messages as it does when selecting the menu "Apply Rules".

Resources