AppleScript: Call handler inside tell statement - applescript

I get this error everytime i run this script: System Events got an error: "Test123" doesn’t understand the notify message.
Code:
--more code...
tell application "System Events"
if some_system_events_property then
my notify of "Test123" thru "Test"
end if
end tell
--more code...
to notify of message thru level
display dialog message with titel level
end notify
I have tried to replace
my notify of "Test123" thru "Test"
with the following, without any success:
notify of "Test123" thru "Test" of me
(notify of "Test123" thru "Test") of me

not exactly sure what your trying to do but here is an example of how to call a function and pass parameter
tell application "System Events"
set m to "message content"
my notify(m)
end tell
--more code...
on notify(message)
display dialog (message)
end notify

Try this:
tell application "System Events"
if some_system_events_property then
tell me to notify of "Test123" thru "Test"
end if
end tell
to notify of message thru level
display dialog message with title level
end notify
Although I'll also say that I never use the direct parameter syntax for AppleScript handlers, preferring positional parameters (i.e., notify( message, level )), as it avoids the ambiguous syntax troubles you discovered.

Related

Best way to delete all attachments in all messages in specific Mailbox

I'm total AppleScript novice and I'm trying to write a script which will delete all attachments in all messages in specified Mailbox (Mail.app).
Here is what am I trying:
tell application "Mail"
tell mailbox "Sent" of account id "<XXX>"
set ListMessage to get every message
repeat with aMessage in ListMessage
set aList to get every mail attachment of aMessage
repeat with aFile in aList
if (downloaded of aFile) then
delete aFile -- this gives an error
end if
end repeat -- next file
end repeat -- next message
end tell
end tell
Running this results in error:
Error: the Apple Event handler failed (errAEEventFailed:-10000)
What am I doing wrong?
Thank you!
It turns out, that delete doesn’t work on mail attachments, but there is an alternative way to solve the issue: you can click the Remove Attachments menu item with System Events:
tell application "Mail"
activate
every item of (get selection)
end tell
tell application "System Events" to click menu item "Remove Attachments" of menu "Message" of menu bar item "Message" of menu bar 1 of process "Mail"

Applescripts getElementByName().value not working

I tried to fill a web form with the data from Excel using AppleScript, but Safari said:
TypeError: undefined is not an object (evaluating 'document.getElementsByName('txtnama')[0].value=txtnama')
Here's the code :
tell application "Microsoft Excel"
activate
set txtnama to value of cell "I2"
tell application "Safari"
activate
do JavaScript "
var txtnama= '" & txtnama & "';
document.getElementsByName("txtnama")[0].value=txtnama;" in document 1
end tell
end tell
The element of the form I'm trying to fill is:
<input type="text" name="txtnama" style="width:300px;" maxlength="80" value="">
Your quotes are mismatched and/or unescaped, but the basic code is correct. Here is the Safari part of your code re-formatted with correct quoting:
tell application "Safari"
do JavaScript ¬
"var txtnama=" & quoted form of txtnama & ";" & ¬
"document.getElementsByName('txtnama')[0].value=" & ¬
quoted form of txtnama ¬
in document 1
end tell
You also shouldn't nest the Safari tell block inside the Microsoft Excel tell block unless there's specific reason to do so. Therefore, move the end tell at the end to just after you define your variable txtnama. It should be laid out like this:
tell application "Microsoft Excel"
-- Commands sent to Excel
end tell
tell application "Safari"
-- Commands sent to Safari (As above)
end tell

Close system dialog with applescript

Is there a way to click OK button on system dialog from applescript? Currently I have made all my workflow on it and only closing that dialog part is missing.
Dialog appears on Safari app (since I make my script to work on Safari) when button "stop reminders" clicked via Javascript function. It's confirmation dialog for destructive action (stoping reminders).
clickId("stop reminders button id") --clicking on button I need
delay 2 -- making sure that dialog has enough time to appear
pressEnterButton() --just trying to close it
to clickId(theId)
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').click();" in document 1
end tell
end clickId
to pressEnterButton()
tell application "System Events"
keystroke return
end tell
end pressEnterButton
that's how I try to do it now, but it not works that way (it's sad, because when I press "enter" on keyboard, it works as it should and dismisses dialog).
Try this:
to pressEnterButton()
tell application "Safari" to activate
tell application "System Events"
tell application process "Safari"
keystroke return
end tell
end tell
end pressEnterButton
#ShooTeKo had a good point about wrapping the dialog part of the code in a
ignoring application responses block
This does work with your existing code.
I tested using a adjusted html - javascript code snippet from www.w3schools
( I added an id to the button. )
The html page to test on is:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to demonstrate the prompt box.</p>
<button id="myBtn" type="button"" onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("demo").innerHTML =
"Hello " + person + "! How are you today?";
}
}
</script>
</body>
</html>
And you code adjusted.
clickId("myBtn") --clicking on button I need
delay 2 -- making sure that dialog has enough time to appear
pressEnterButton() --just trying to close it
to clickId(theId)
tell application "Safari"
ignoring application responses
activate
do JavaScript "document.getElementById('" & theId & "').click()" in document 1
end ignoring
end tell
end clickId
to pressEnterButton()
tell application "System Events"
keystroke return
end tell
end pressEnterButton

apple scripting with Messages to take action on URL

I'm trying to write an AppleScript for Messages such that if I get a message that contains a URL, the script automatically opens the link in my browser.
I've been able to set a script to run that sends me a notification with the properties of the message, but I've not yet been able to figure out how to take action on the URL.
Here's what I've been able to produce:
using terms from application "Messages"
on message received from theSender for theChat with theContents
display notification theContents as text ¬
with title "New Message from " & theSender
end on message received
end using terms
Here's an example that might help you:
set msg to theContents as text
set AppleScript's text item delimiters to space
set msg to msg as list
repeat with x in items of msg
if "http" is in x then
display dialog x
end if
end repeat

Use Applescript to set the "Reply-To" address

I have a registered domain, and I use hushmail as a mail provider. I'd like to send emails from Mail.app as if they were sent from my domain. The Hushmail SMTP server does not allow me to use a different "from" address than my account name, for security reasons (spam).
I found a way to have Apple mail fill the reply-to mail with a default all the time, here: http://email.about.com/od/macosxmailtips/qt/etalwaysreplyto.htm but that's too drastic for me, as I have multiple mail accounts in my mail client.
In Mail.app, I can set the "Reply to" field manually, but there is no setting in Mail.app to have that automatically filled based on the mailbox I select.
So far, I have an AppleScript which is able to create a reply on the selected mail:
tell application "Mail"
set theSelection to selection
if theSelection is {} then return
activate
repeat with thisMessage in theSelection
set theOutgoingMessage to reply thisMessage with opening window
# Wait for Mail.app to create the reply window
repeat until exists (window 1 whose name = "Re: " & subject of thisMessage)
end repeat
delay 0.1
#
# Here I want to set the reply-to address here based on the
# selected mailbox, or the "from" address of
# the current mail.
#
#
# The I need to restore the cursor to the body of the mail
# (if cursor was moved)
#
end repeat
end tell
I've looked in the AppleScript Dictionary (File -> Open Dictionary -> Mail.app -> Message -> message -> reply to), and this seems to be a property I should be able to set, but when I do something like:
tell theOutgoingMessage
make new recipient at end of reply to with properties {address:"myreplyto#example.com"}
An error pops up saying "Mail got an error: Can't get reply to of outgoing message id 65."
I also tried
tell theOutgoingMessage
set reply to to "myreplyto#example.com"
But that pops up an error saying "Mail got an error: Can’t set reply to of outgoing message id 69 to "myreplyto#example.com".
How can I set the reply-to property of the reply mail I just created?
As I mentioned in the comments to your post, you can't set the reply-to address programmatically because it's a read-only property. Therefore you need to ui script this solution.
The problem with ui scripting is that it's not an exact science. If Apple changes the positioning of the ui elements then your script will stop working. For example, I have Mail.app v6.5 and the reply-to field can be referenced using "text field 1 of scroll area 1 of window 1". In other versions of Mail.app that could be different (and probably is).
Anyway, in v6.5 of Mail.app this will do what you want using ui scripting.
tell application "Mail"
set theSelection to selection
if theSelection is {} then return
activate
repeat with thisMessage in theSelection
set replyToAddress to item 1 of (get email addresses of account of mailbox of thisMessage)
set replyToWindowName to subject of thisMessage
if replyToWindowName does not start with "Re:" then set replyToWindowName to "Re: " & replyToWindowName
-- open the reply message
set theOutgoingMessage to reply thisMessage with opening window
-- Wait for Mail.app to create the reply window
repeat until exists (window 1 whose name is replyToWindowName)
delay 0.2
end repeat
-- make sure the reply-to field is showing
my openReplyToField()
-- set the reply-to address here based on the selected mailbox
my setValueOfReplyToField(replyToAddress)
end repeat
end tell
on openReplyToField()
tell application "System Events"
tell process "Mail"
-- figure out if the reply-to text field is visible
set staticText to value of every static text of window 1
if staticText contains "Reply To:" then return
-- the reply-to field is not visible so we click the menu item
set viewMenu to menu 1 of menu bar item "View" of menu bar 1
set replyToMenuItem to first menu item of viewMenu whose name contains "Reply-To"
click replyToMenuItem
end tell
end tell
end openReplyToField
on setValueOfReplyToField(theValue)
tell application "System Events"
tell process "Mail"
set replyToField to text field 1 of scroll area 1 of window 1
set value of replyToField to theValue
end tell
end tell
end setValueOfReplyToField
Try if this works:
make new recipient at end of to recipients with properties {address:"myreplyto#example.com"}

Resources