I have an AppleScript (created by DevonThink) that will detach email attachments in Apple Mail and save them to DevonThink. The script works just fine when I invoke it from the menubar script menu item with Mail open in the foreground. However, when I copied the script into the KB Maestro Execute AppleScript action, it does not run when I execute the trigger. I'm trying to assign the keyboard trigger to a Better Touch Tool Stream Deck action key (hey, might as well use all the tools in one action, right!)
I'm sure I'm doing something wrong here. Here are the log entries from the Engine Log:
2022-08-17 17:05:18 Action 9782 failed: Execute an AppleScript failed with script error: text-script:969:969: script error: Expected “end” or “on” but found end of script. (-2741)
2022-08-17 17:05:18 Execute an AppleScript failed with script error: text-script:969:969: script error: Expected “end” or “on” but found end of script. (-2741). Macro “Save mail attachment to Devonthink” cancelled (while executing Execute AppleScript).
Thanks in advance!
To get an answer you would need to show the AppleScript.
if you paste it:
```
between triple backticks, above and below
```
it will be properly formatted.
( At first glance it looks possible that you inadvertently left off a few lines when you copied and pasted it )
Here is the script in question. Thanks!
-- Import attachments of selected messages to DEVONthink.
-- Created by Christian Grunenberg on Fri May 18 2012.
-- Copyright (c) 2012-2020. All rights reserved.
tell application "Mail"
try
tell application id "DNtp"
if not (exists current database) then error "No database is in use."
set theGroup to preferred import destination
end tell
set theSelection to the selection
set theFolder to (POSIX path of (path to temporary items))
if the length of theSelection is less than 1 then error "One or more messages must be selected."
repeat with theMessage in theSelection
set theSender to the sender of theMessage
repeat with theAttachment in mail attachments of theMessage
try
if downloaded of theAttachment then
set theFile to theFolder & (name of theAttachment)
tell theAttachment to save in theFile
tell application id "DNtp"
set theAttachmentRecord to import theFile to theGroup
set URL of theAttachmentRecord to theSender
perform smart rule trigger import event record theAttachmentRecord
end tell
end if
end try
end repeat
end repeat
on error error_message number error_number
if error_number is not -128 then display alert "Mail" message error_message as warning
end try
end tell
Seems to compile and run here.
What happens if you try to run this on your system ?
Mail and DT script.kmmacros (2.7 KB)
Here is the script, copied directly from the script file in My Library. It looks identical to what I pasted above, which I copied directly from my KB Maestro Execute Script action
-- Import attachments of selected messages to DEVONthink.
-- Created by Christian Grunenberg on Fri May 18 2012.
-- Copyright (c) 2012-2020. All rights reserved.
tell application "Mail"
try
tell application id "DNtp"
if not (exists current database) then error "No database is in use."
set theGroup to preferred import destination
end tell
set theSelection to the selection
set theFolder to (POSIX path of (path to temporary items))
if the length of theSelection is less than 1 then error "One or more messages must be selected."
repeat with theMessage in theSelection
set theSender to the sender of theMessage
repeat with theAttachment in mail attachments of theMessage
try
if downloaded of theAttachment then
set theFile to theFolder & (name of theAttachment)
tell theAttachment to save in theFile
tell application id "DNtp"
set theAttachmentRecord to import theFile to theGroup
set URL of theAttachmentRecord to theSender
perform smart rule trigger import event record theAttachmentRecord
end tell
end if
end try
end repeat
end repeat
on error error_message number error_number
if error_number is not -128 then display alert "Mail" message error_message as warning
end try
end tell
When I run it on my system, it works perfectly, 100% of the time. Its only when I try to run it from KB Maestro that it doesn't fire.
Problem solved and its a big "duh" moment for me: the macro was not enabled.
Sorry for wasting your time on this.... Appreciate the help!
1 Like