Sending the value of a LC variable to AppleScript
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Sending the value of a LC variable to AppleScript
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?
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?
-
- 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
Hi Mag,
Not clear on this one.
What is wrong with using revMail?
Simon
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!
Re: Sending the value of a LC variable to AppleScript
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
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
-
- 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
Hi Mag,
putinto a field that you should name "fAS"
putinto 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
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
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
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
Re: Sending the value of a LC variable to AppleScript
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 I use to replace placeholders in the variable:
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.
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: Select all
replace "%emailAddress%" with theAddress in tTheScript
replace "%emailSubject%" with tehSubject in tTheScript
replace "%emailBody%" with theMessage in tTheScript
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.
Re: Sending the value of a LC variable to AppleScript
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!
Thank you so much for all those that help and posted here!
-
- 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
Hi Mag,
could you post your solution?
I would be interested in it.
Kind regards
Bernd
could you post your solution?
I would be interested in it.
Kind regards
Bernd
Re: Sending the value of a LC variable to AppleScript
Hi Bernd,bn wrote:Hi Mag,
could you post your solution?
I would be interested in it.
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