Close system dialog with applescript - 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

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

AppleScript clicking a button in safari error

I am trying to click a simple button in safari when on a web page using applescript. I have the following code:
tell application "Safari"
activate
repeat 5 times
do JavaScript "document.all(\"Roll Again\").click()" in document 1
delay 2
end repeat
end tell
However, when I run this code, I get an error: "missing value". The event log displays the following result:
tell application "Safari"
activate
do JavaScript "document.all(\"Roll Again\").click()" in document 1
--> missing value
do JavaScript "document.all(\"Roll Again\").click()" in document 1
--> missing value
do JavaScript "document.all(\"Roll Again\").click()" in document 1
--> missing value
do JavaScript "document.all(\"Roll Again\").click()" in document 1
--> missing value
do JavaScript "document.all(\"Roll Again\").click()" in document 1
--> missing value
end tell
Could someone please tell me why the button is not being pressed and what missing value means? Is there another simpler way to achieve the button press? I can press the button using automator, however I would like to do it using AppleScript.
P.S. document is the current window in safari
document.all is specific to IE and doesn't work in Safari. Try using getElementById or querySelectorAll:
tell application "Safari"
activate
tell document 1
do JavaScript "document.getElementById(\"nav-badges\").click()"
delay 5
do JavaScript "document.querySelectorAll(\"#hmenus a\")[0].click()"
end tell
end tell

AppleScript: Call handler inside tell statement

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.

applescript buttons

I have followed this youtube tutorial: http://www.youtube.com/watch?v=0ux3S_G8HuU
I want the second button go to a special website....
I have tried this:
if "button 2" then open location "(the name of the site)"
What do I do? I hope you can help.
Try this:
set search to (display dialog "Enter YouTube Video Search" default answer "" buttons {"Cancel", "Search", "Search2"} default button 3)
set keyword to text returned of search
set buttonPressed to button returned of search
if buttonPressed is equal to "Search" then
tell application "Safari"
open location "http://www.youtube.com/results?search_query=" & keyword
end tell
else if buttonPressed is equal to "Search2" then
tell application "Safari"
open location "http://www.youtube.com/results?search_query=" & keyword
end tell
end if
Just change the URLs to whatever you want for Search or Search2.

Resources