View Full Version : Debugging applescript for Thunderbird
grotsasha
Feb 19, 2006, 04:16 PM
Ok, here's a bit more proper version of the script for Compress&Mail for Thunderbird client, say, number 0.2 :)
It is more like the original script, can fill out subject, body of the message, etc., and can handle errors. It has still the bugs mentionned above: works ONLY with latest Thunderbird nightly builds (1.6a1, built after 15th of February), fails if Thunderbird is already launched and can't attach more than one file.
on send_mail(email_subject, email_message, email_sender, email_recipient, email_files)
try
set thunderbird_bin to "/Applications/Thunderbird.app/Contents/MacOS/thunderbird-bin -compose "
--Thunderbird needs an URL path
set email_files to "attachment='file://" & email_files & "'"
set email_subject to "subject=" & email_subject
set email_recipient to "to=" & email_recipient
set email_message to "body=" & email_message
set arguments to email_subject & "," & email_recipient & "," & email_message & "," & email_files
do shell script thunderbird_bin & quoted form of arguments
on error error_message number error_number
log error_message & " " & error_number
end try
end send_mail
flip
Feb 19, 2006, 05:52 PM
Ok, here's a bit more proper version of the script above, say, number 0.2 :)
I think it has a flaw: what if email_files is passed a list of several files?
AFAIK, that parameter is always given a list, so I guess your script works by implicitly coercing that list into a string when you build the url. But that would fail if the list contained several strings. I'm not sure the command allows to pass Thunderbird several files, but if it does you'll have to loop on the list, making a url for each file it contains.
Also, Thunderbird probably expects the urls to be properly encoded. In this case, the script would fail with files that have spaces and/or high ascii characters in their path. A solution to both problems (assuming Thunderbird accepts several attachments) would go something like this:
set attachments to "attachment='"
repeat with a_path in email_files
set a_file to POSIX file a_path
tell application "Finder" to set a_url to URL of a_file
set attachments to attachments & a_url
if a_path is not (last item of email_files) then
set attachments to attachments & ","
end if
end repeat
set attachments to attachments & "'"
Update: just noticed that you said earlier "Thunderbird needs to have these multiple files separated with commas". So that answers my question about its ability to attach several files and I just corrected the AppleScript above accordingly.
grotsasha
Feb 19, 2006, 11:42 PM
Thank you for your help and script suggestion! Yeah, I was thinking about looping the list, just couldn't figure out the AppleScript loops syntax... It's my first script that I made in AppleScript, after reading a tutorial for about half an hour or so, so your help is really welcome! (I have only really basic programming knoweledge. For this task I would be a bit more comfortable with bash or python though)
Surprisingly if Thunderbird gets several files with my script, it can attach the first of them, so the script doesn't fail. Also spaces and non-ASCII characters in the path seem to cause no problems (and that's rather strange, because theoretically they should cause some...).
I think I should avoid to rely on Finder, because there are a lot of people who erase it completely or keep it quit, so it'll relaunch it...
Version 0.3 coming soon! :D :D :D
flip
Feb 20, 2006, 07:24 AM
Thank you for your help and script suggestion!
You're welcome! :)
I think I should avoid to rely on Finder, because there are a lot of people who erase it completely or keep it quit, so it'll relaunch it...
Ah yeah, good point! The interest of this command is that it automatically encodes the url, which would be a pain to code manually. But if Thunderbird accepts unencoded urls... Have you tried sending an attachment whose url isn't encoded, though? PF has a URL command too, but I haven't been able to get it to work yet.
grotsasha
Feb 20, 2006, 07:55 AM
Can you please take a look at this one? It DOESN'T work yet, it continues to attach only one file and I can't yet figure out why.
My edit: removed not to be too long
Though this chunk of code:
set email_files to {"myfile", "myfile1", "myfile2"}
log (email_files)
set attachments to "attachment='"
repeat with a_file in email_files
set a_file to "file://" & a_file
if a_file is not (last item of email_files) then
set attachments to attachments & a_file & ","
end if
end repeat
set arguments to attachments & "'"
log (arguments)
log (a_file)
log (email_files)
gives the following output on the event log console:
(*myfile, myfile1, myfile2*)
(*attachment='file://myfile,file://myfile1,file://myfile2,'*)
(*file://myfile2*)
(*myfile, myfile1, myfile2*)
And the second line above here is the exact syntax that Thunderbird needs.
flip
Feb 20, 2006, 08:13 AM
Can you please take a look at this one? It DOESN'T work yet, it continues to attach only one file and I can't yet figure out why.
Might be because the last file in the attachment string ends up followed with a comma too.
1. repeat with a_file in email_files
2. set a_file to "file://" & a_file
3. if a_file is not (last item of email_files) then
Here, you get the variable a_file (1), you modify its value (2), and then you try to see if it was the last item in the list (3). But since its value has been modified, it's no longer possible. You could work around that by incrementing directly the attachments variable:
repeat with a_file in email_files
set attachments to attachments & "file://" & a_file
if a_file is not (last item of email_files) then
Does it help?
OldVAXGuy
Feb 20, 2006, 08:23 AM
The interest of this command is that it automatically encodes the url, which would be a pain to code manually. But if Thunderbird accepts unencoded urls... Have you tried sending an attachment whose url isn't encoded, though? PF has a URL command too, but I haven't been able to get it to work yet.
I think you can avoid the need to encode (caused by the presence of spaces in the file name, for instance) by using this:
set a_url to quoted form of posix path of afile
It won't have the "file://" prefix, either.[/u]
flip
Feb 20, 2006, 08:44 AM
I think you can avoid the need to encode (caused by the presence of spaces in the file name, for instance) by using this:
set a_url to quoted form of posix path of afile
Sorry but it's not the same. In an url, a space is supposed to be encoded as "%20". Quoted form of posix path of afile will just escape the space (using a backslash) and put the string into quotes. Also high ascii chars won't be encoded in the posix path, but they're supposed to be encoded in urls.
Now if Thunderbird doesn't require the urls to be encoded, that's fine for now. But I would call it a bug, and if they eventually fix it the script would break. Also, I'm not sure yet if Thunderbird will send successfully a file (with spaces and high ascii in its path) whose url isn't encoded. Sasha, have you tried?
grotsasha
Feb 20, 2006, 09:28 AM
Thanks for pointing out that I had a comma after the last item, I didn't even notice :oops:
But my brains are going to boil soon :) :)
It doesn't want to work :?
Your suggestion ends up with this string as argument:
(*attachment='file://myfilefile://myfile,file://myfile1file://myfile1,file://myfile2file://myfile2,'*)
or maybe I didn't understand and made a mess.
However this chunk of code:
set email_files to {"myfile", "myfile1", "myfile2"}
log (email_files)
set attachments to "attachment='"
repeat with a_file in email_files
if a_file is (last item of email_files) then
set attachments to attachments & "file://" & a_file
else
set attachments to attachments & "file://" & a_file & ","
end if
end repeat
set arguments to attachments & "'"
log (arguments)
log (a_file)
log (email_files)
To my puzzling astonishment gives EXACTLY the same error, i.e. a comma after the last item... :shock:
(*attachment='file://myfile,file://myfile1,file://myfile2,'*)
And you see that I don't modify a_file before the if statement. When does the last item get this comma?!!
BTW, I didn't try to send attachments yet, so I dunno if Thunderbird gets stuck at this point.
flip
Feb 20, 2006, 09:49 AM
To my puzzling astonishment gives EXACTLY the same error, i.e. a comma after the last item... :shock:
Oh, I see! That's because a_file is a reference (item 3 of {myfile, myfile1, myfile2}) while last item of email_files is returned as a string ("myfile2"). So the comparison evaluates to false (AppleScript can be pretty dumb, sometimes ;) ). Sorry I missed to catch this in my previous example.
You can work around it by coercing the reference into a string (using "as string"):
set email_files to {"myfile", "myfile1", "myfile2"}
set attachments to "attachment='"
repeat with a_file in email_files
if a_file as string is (last item of email_files) then
set attachments to attachments & "file://" & a_file
else
set attachments to attachments & "file://" & a_file & ","
end if
end repeat
set arguments to attachments & "'"
arguments -->"attachment='file://myfile,file://myfile1,file://myfile2'"
BTW, in case you wonder why a_file is correctly treated as a string in "file://" & a_file, it's because the & operator causes the operands to be automatically coerced when needed.
grotsasha
Feb 20, 2006, 10:15 AM
What a strange language really!
Ok, now my small test chunk of code works correctly, and there's no comma after the last item. But the whole script continue to attach only one file :? (I tried "as string" and without it, no difference)
I think the problem is this email_files variable. When I define it myself - it's a list of strings, but it is obviously not, when PF passes it to the script. Maybe there's an expicit way to redefine it as a list of strings? Is there a way to log it somewhere to see it? As the script works internally, log (something) doesn't appear inside the event log, and in the console.log I have only useless things like:
in: [NTActiveDirectory_queryThread doThreadProc]
flip
Feb 20, 2006, 10:56 AM
What a strange language really!
Yeah! That's what happens when you try to make a machine speak like a human. :D
Ok, now my small test chunk of code works correctly, and there's no comma after the last item. But the whole script continue to attach only one file :? (I tried "as string" and without it, no difference)
I think the problem is this email_files variable. When I define it myself - it's a list of strings, but it is obviously not, when PF passes it to the script. Maybe there's an expicit way to redefine it as a list of strings?
If you know it's a string, you can coerce it into a list like this:
set s to "this is a string"
set ls to {s}
ls --> {"this is a string"}
Is there a way to log it somewhere to see it?
You could use ScriptDebugger (http://www.latenightsw.com/) which allows you to debug and see the values of variables as you step across the script. Or you could use display dialog to see the value or type of your variables:
set s to "this is a string"
display dialog class of s
--> TEXT
display dialog s
--> this is a string
set ls to {s}
display dialog class of ls
--> list
display dialog ls
--> this is a string (automatic coercion of one item lists)
set ls to ls & "test"
display dialog ls
--> error: Can't make {"this is a string", "test"} into type string.
You can also use "say" instead of "display dialog", with the same limitations.
What I would suggest you, if Thunderbird keeps attaching only one file, is to try from Terminal with a command you know should work. If it works, try from AppleScript, with the same command entirely hardcoded. Then, if it works, it should definitely work as well when you build the command on the fly. If it doesn't, you know the problem is somewhere in the building of the command. Good luck! :)
Update: maybe we should take this conversation offline, no? I'm afraid it's getting off topic.
grotsasha
Feb 20, 2006, 12:25 PM
He, he... I think I discovered a bug in PF...:)
Can you confirm that other mail clients and the default script can attach multiple files?
In fact I wrote this "display dialog" everywhere in the code to see all the variables and classes. AND:
- if I choose 2 files in the browser (email or compress&email doesn't matter) the email_files variable contains only ONE file at the very beginning of the script! (BTW it is a list) And this file is always the second one in the list (I use file, file1, file2 and the file that get attached is file1 )
- if I choose 3 files the script crashes at the point where I ask it to display what the email_files contains at the very beginning of the script...
Here's the script (with all display dialogs to see what's going on). Theoretically it must work, when PF really pass a correct list of files. (I think the problem with this email_files variable has something to do with the beginning of count in the list in different languages. I suppose this variable comes from Obj-C environment and when it comes to AppleScript, it doesn't count from index 0, but from index 1, or something like that. And that's why I get only a second item and then nothing at all. But I have really no idea of Obj-C language...
on send_mail(email_subject, email_message, email_sender, email_recipient, email_files)
display dialog class of email_files
display dialog email_files
display dialog class of send_mail
try
set thunderbird_bin to "/Applications/Thunderbird.app/Contents/MacOS/thunderbird-bin -compose "
set email_subject to "subject=" & email_subject
set email_recipient to "to=" & email_recipient
set email_message to "body=" & email_message
set attachments to "attachment='"
repeat with a_file in email_files
display dialog email_files
if a_file as string is (last item of email_files) then
set attachments to attachments & "file://" & a_file
else
set attachments to attachments & "file://" & a_file & ","
end if
end repeat
set arguments to email_subject & "," & email_recipient & "," & email_message & "," & attachments & "'"
display dialog arguments
do shell script thunderbird_bin & quoted form of arguments
on error error_message number error_number
log error_message & " " & error_number
end try
end send_mail
BTW: Thunderbird works fine from Terminal and from a hard-coded AppleScript.
flip
Feb 20, 2006, 01:03 PM
He, he... I think I discovered a bug in PF...:)
Can you confirm that other mail clients and the default script can attach multiple files?
Yes, but one of them is always missing.
- if I choose 3 files the script crashes at the point where I ask it to display what the email_files contains at the very beginning of the script...
Yes, it cannot coerce into a string a list with more than one items (as demonstrated by the last statement in my previous example). At that point, the list contains two elements, and if you remove that call, you'll see them from the display dialog inside the loop.
I think the problem with this email_files variable has something to do with the beginning of count in the list in different languages. I suppose this variable comes from Obj-C environment and when it comes to AppleScript, it doesn't count from index 0, but from index 1, or something like that. And that's why I get only a second item and then nothing at all. But I have really no idea of Obj-C language...
You're right. Objective-C indexes from 0 when AppleScript starts from 1. Well done. :)
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.