How to have Applescript shut down confirmation dialog in frontmost - applescript

I have a simple applescript, compiled as application, that allows user to choose between restart, shut down, etc.
It's very simple and works fine, but the confirmation dialog sent by the OS "are you sure you want to shut down your computer now" with the timer is not the frontmost window, so user have to click inside this dialog to activate, and then click the button he wants.
If this confirmation dialog would be the frontmost window, a simple enter key would confirm the choice. Any idea about how to bring this confirmation dialog to top?
tell application "Finder"
set userChoice to my getChoixUser("list") -- choose from list
try
if (userChoice contains "Veille") then -- sleep
tell application "Finder" to sleep
else if (userChoice contains "Eteindre") then -- shut down
tell application "loginwindow" to «event aevtrsdn»
else if (userChoice contains "Redémarrer") then -- restart
tell application "loginwindow" to «event aevtrrst»
else if (userChoice contains "économiseur") then -- screen saver
tell application "System Events" to start current screen saver
end if
on error errMsg
beep
tell application "Finder" to display dialog errMsg buttons {"OK"} default button 1 with title scriptName with icon 0
end try
end tell

Remove the enclosing Finder tell block and activate the window with GUI scripting
set userChoice to getChoixUser("list") -- choose from list
try
if (userChoice contains "Veille") then -- sleep
tell application "Finder" to sleep
else if (userChoice contains "Eteindre") then -- shut down
tell application "loginwindow" to «event aevtrsdn»
focusLoginWindow()
else if (userChoice contains "Redémarrer") then -- restart
tell application "loginwindow" to «event aevtrrst»
focusLoginWindow()
else if (userChoice contains "économiseur") then -- screen saver
tell application "System Events" to start current screen saver
end if
on error errMsg
beep
tell application "Finder" to display dialog errMsg buttons {"OK"} default button 1 with title scriptName with icon 0
end try
on focusLoginWindow()
tell application "System Events" to tell process "loginwindow"
repeat until exists window 1
delay 0.2
end repeat
set attribute "AXFocused" of window 1 to true
end tell
end focusLoginWindow

Related

How to quit Xcode with confirmation dialog box using apple script

I'm trying to do a script to quit Xcode with currently building application. My current script only cover to quit Xcode but I'm unable to handle the confirmation dialog box that pop-ups on the Xcode. On the confirmation box, there are two button options: Cancel or Stop Tasks. I wanted to click on Stop Tasks button. Please help.
Here's my current AppleScript so far:
set apps to {"Simulator","Xcode"}
repeat with thisApp in apps
tell application thisApp to quit
end repeat
tell application "System Events"
tell process "Xcode"
click button "Stop Tasks" of sheet 1 of window 1
end tell
end tell
This is my current applescript that didn't work.
I'm able to fix my issue regarding terminating Xcode with confirmation dialog box prompt. Here's my the applescript I've come up:
try
with timeout of 10 seconds
quit application "Simulator"
quit application "Xcode"
end timeout
on error number e
if e is -1712 then
activate application "Xcode"
tell application "System Events"
tell application process "Xcode"
keystroke return
end tell
end tell
end if
end try

Activate the frontmost Safari and run Javascript

I'm trying to run Javascript on the Safari, whose frontmost is true.
tell (application "Safari" whose frontmost is true) to do JavaScript theScript in current tab of first window
But I get an error like this:
execution error: Can’t get application "System Events" whose frontmost of it = true. Access not allowed. (-1723)
where I'm making mistake?
AppleScript often requires the syntax be formatted in a specific way. In the case of your script it can't differentiate identifiers because you've placed them incorrectly on the same line.
set theScript to "alert('Hello, World!');"
tell application "Safari"
set frontmost to true
activate
do JavaScript theScript in current tab of first window
end tell
You can test your script in AppleScript editor by compiling and running it. The events, replies and results should give you some indication of where problems might be.
EDIT: to deal with a process which is the frontmost (or most recent):*
set theApp to "Safari"
set theScript to "alert('Hello, World!');"
on isOpen(appName)
tell application "System Events" to (name of processes) contains appName
end isOpen
set isActive to isOpen(theApp)
if isActive then
tell application "System Events" to tell (process "Safari" whose frontmost is true)
tell application "Safari"
activate
tell application "System Events" to set frontSafari to item 1 of (get name of processes whose frontmost is true)
if (count windows) is 0 then
display dialog "There are no " & theApp & " windows open." buttons {"OK"}
else if frontSafari is "Safari" then
tell application "Safari" to do JavaScript theScript in current tab of first window
end if
end tell
end tell
else
display dialog theApp & " is not currently running." buttons {"OK"}
end if

Verify a checkbox before clicking with Applescript

I'm facing an issue with one of my applescript.
I'm trying to create an applescript that check/uncheck the checkbox that call the password after the mac wake up or the screensaver stop in the mac security pannel.
I'm using this with proximity.app, with the idea that when i'm back home and my phone is in range, proximity.app remove the password, but when i'm out of range, it put the password back.
Well... I'm forced to do it using UI scripting, because of the new security policy in Mountain Lion.
So there is the code when out of range :
tell application "System Preferences"
set current pane to pane id "com.apple.preference.security"
tell application "System Events"
tell process "System Preferences"
tell first window
tell first tab group
click radio button 1
if not 1 then click checkbox 1
click menu item 6 of menu of pop up button 1
end tell
end tell
end tell
end tell
quit
end tell
and when in range :
tell application "System Preferences"
set current pane to pane id "com.apple.preference.security"
tell application "System Events"
tell process "System Preferences"
tell first window
tell first tab group
click radio button 1
click checkbox 1
end tell
end tell
end tell
end tell
quit
end tell
What i want to improve, is a way to first verify if the box is check or uncheck before checking or unchecking it.
Thanks for your help.
Just check the value of the checkbox.
0 = Uncheck, 1 = check
tell application "System Preferences" to ¬
reveal anchor "Advanced" of pane id "com.apple.preference.security"
tell application "System Events"
tell first tab group of first window of process "System Preferences"
tell checkbox 1 to if value is 0 then click -- checkbox was not checked.
end tell
end tell
quit application "System Preferences"

What is the code used to switch back to a previously running window via Apple's Automator?

I created a code that toggles the keyboard viewer in Automator.
on run {input, parameters}
if application "KeyboardViewer" is running then
quit application "KeyboardViewer"
else
activate application "KeyboardViewer"
end if
return input
end run
However, the Keyboard Viewer becomes the current running window and I cannot instantaneously start typing (I have to switch back to the previous window). Is there a specific code that I can add so that the previous window is highlighted again ?
You could use launch instead of activate:
tell application "KeyboardViewer"
if running then
quit
else
launch
end if
end tell
If the application wasn't open, launch usually opens a new window above other applications but below the frontmost application. Otherwise it just keeps the application on the background. You can use AXRaise to raise windows in the second case, but it also makes them look like active windows.
launch application "Terminal"
tell application "System Events" to tell process "Terminal"
perform action "AXRaise" of windows
end tell
You could also save the previous application in a variable:
set a to path to frontmost application as text
activate application "Terminal"
activate application a
If you're moving focus to a background application, you can just activate the frontmost application later:
try
tell application "SystemUIServer"
display dialog "" default answer ""
end tell
end try
activate application (path to frontmost application as text)
Try this just after the activate application "KeyboardViewer" line...
tell application "System Events" to keystroke tab using command down
EDIT: Since the original post above didn't do it for you then try this. It uses a subroutine I use to get the frontmost running application in cases just like this. Just put this code into the applescript section of your automator action...
on run {input, parameters}
if application "KeyboardViewer" is running then
quit application "KeyboardViewer"
else
set frontAppPath to my getFrontAppPath()
activate application "KeyboardViewer"
delay 0.2
tell application frontAppPath to activate
end if
return input
end run
on getFrontAppPath()
set frontAppPath to (path to frontmost application) as text
set myPath to (path to me) as text
if frontAppPath is myPath then
try
tell application "Finder" to set bundleID to id of file myPath
tell application "System Events" to set visible of (first process whose bundle identifier is bundleID) to false
-- we need to delay because it takes time for the process to hide
-- I noticed this when running the code as an application from the applescript menu bar item
set inTime to current date
repeat
set frontAppPath to (path to frontmost application) as text
if frontAppPath is not myPath then exit repeat
if (current date) - inTime is greater than 1 then exit repeat
end repeat
end try
end if
return frontAppPath
end getFrontAppPath

How to change space with applescript

I have the following applescript to skip songs in spotify. If I call the script when a fullscreen application is the frontmost, the application will not be visible after the script has finished. It will be in a different space. Is there a way that I can make the frontmost application visible again with applescript?
set front_app to (path to frontmost application as Unicode text)
tell application "Spotify"
next track
end tell
tell application front_app to activate
This probably won't get you anywhere, but you could use this script to loop through all your spaces until you see the application you're targeting...
set front_application to current application
tell application "Spotify" to next track
tell application "Finder" to set collapsed of front_application to false --makes sure the app isn't minimized
repeat
tell application "System Events" to keystroke (ASCII character 29) using {control down}
display dialog "Correct space?" buttons{"OK"} default button 1 giving up after 4 --don't click 'OK' if the current space isn't showing the target application
if gave up of the result is false then exit repeat
end repeat
set front_app to current application
tell application "Spotify"
next track
end tell
tell front_app to activate

Resources