Sending the value of a LC variable to AppleScript

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Sending the value of a LC variable to AppleScript

Post by Mag » Tue Mar 25, 2014 5:46 pm

I'm using AppleScript to compose and send an email via the app Mail. I know how to get values from AppleScript (by seeing the results).

Now I need a way to send data to AppleScript (particularly the body of the message which can have quotes, brackets and other punctation marks that could break the script whether I put them directly in the script container).

Is there a way to send a value to AppleScript? :oops:

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Sending the value of a LC variable to AppleScript

Post by Simon » Tue Mar 25, 2014 11:45 pm

Hi Mag,
Not clear on this one.
What is wrong with using revMail?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Sending the value of a LC variable to AppleScript

Post by Mag » Wed Mar 26, 2014 11:30 am

Hi Simon,

Thank you for your replay. I considered to use it but I need to not just compose the email, I need to send it.

Here is the code

Code: Select all

tell application "Mail"
	set theSubject to "%emailSubject%" -- Subject
	set theContent to "%emailBody%" -- Body
	set theAddress to "%emailAddress%" -- To
	set msg to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
	tell msg to make new to recipient at end of every to recipient with properties {address:theAddress}
	send msg
end tell

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Sending the value of a LC variable to AppleScript

Post by bn » Wed Mar 26, 2014 3:59 pm

Hi Mag,

put

Code: Select all

tell application "Mail"
	set msg to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
	tell msg to make new to recipient at end of every to recipient with properties {address:theAddress}
	--send msg -- unblock after testing
end tell
into a field that you should name "fAS"

put

Code: Select all

on mouseUp
   put "lorem ipsum" & return & "lorem ipsum" into tMessage
   put "set theSubject to " & quote & "Test Mag" & quote & return after tVariables
   put "set theContent to " & quote & tMessage & quote & return after tVariables
   put "set theAddress to " & quote & "mag@mag.com" & quote & return after tVariables -- use valid address
   
   put tVariables & field "fAS" into tWholeScript
   do tWholeScript as applescript
   
   answer the result 
end mouseUp
into a button
Now click the button and see if it generates a mail. The mail will not be sent since "send msg" is blocked in the script in field "fAS" and you might want to use a valid email address. Unblock "send msg" in field "fAS" once you are done testing.

What happens here is that you build your applescript variables in the script of the button. Then you append the rest of the applescript which is in the field and assemble all that into a variable "tWholeScript". You then do that as applescript. The result if succesful is a message from Mail otherwise "execution error".

Kind regards
Bernd

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Sending the value of a LC variable to AppleScript

Post by Mag » Wed Mar 26, 2014 5:37 pm

Hi Bern,

very nice try. It works fine but it doesn't solve the problem. I already have a way to replace the elements of the email (address, body, subject) in the script by putting the script in a variable and then replace the placeholders.

Code that I put in the variable tTheScript:

Code: Select all

tell application "Mail"
	set theSubject to "%emailSubject%" -- Subject
	set theContent to "%emailBody%" -- Body
	set theAddress to "%emailAddress%" -- To
	set msg to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
	tell msg to make new to recipient at end of every to recipient with properties {address:theAddress}
	send msg
end tell
Code I use to replace placeholders in the variable:

Code: Select all

   replace "%emailAddress%" with theAddress in tTheScript
   replace "%emailSubject%" with tehSubject in tTheScript
   replace "%emailBody%" with theMessage in tTheScript
Then I do the variable as AppleScript, and it works fine.

Unfortunately I think (not sure if I'm right or not) that those two ways (mine and yours) have the problem that you can't pass a real-word email message because it could contain characters and schemas that can broke the script, for example reserved AppleScript patterns of words, quotes and so on.

So I think that the solution could be to have AppleScript that directly gets the email body text in some way, this should avoid to put the email body text directly in the AppleScript script.

Alternatively you have to parse the text to use as email body text in the way that you are sure that don't broke the script.

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Sending the value of a LC variable to AppleScript

Post by Mag » Thu Mar 27, 2014 9:25 pm

OK, I think to have found a way. I will try to save the text I need for the body of the message in an external .txt file with LiveCode, then I will read it in a variable with AppleScript.

Thank you so much for all those that help and posted here!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Sending the value of a LC variable to AppleScript

Post by bn » Thu Mar 27, 2014 9:37 pm

Hi Mag,

could you post your solution?
I would be interested in it.

Kind regards
Bernd

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Sending the value of a LC variable to AppleScript

Post by Mag » Fri Mar 28, 2014 11:59 am

bn wrote:Hi Mag,

could you post your solution?
I would be interested in it.
Hi Bernd,

With pleasure.

With this method, even if the user enters characters reserved by AppleScript (as { } " ") etc in the Subject or Message Body, the AppleScript script does not break.

PS
I think that if one plans to release an application to the public, it should be better to have all the user-entered values handled as AppleScript's variables (included the email address which in my example is not), in fact it could not be very safe to modify an AppleScript script with "hard-coded" text entered by somebody.
Attachments
Email.livecode.zip
(1.86 KiB) Downloaded 194 times

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”