Setting popup box with osascript and a 10 second delay - bash

I'm looking to setup a popup box for users to see that has a 10 second delay before it exits and the script continues. So far I've got the following.
osascript -e 'tell app "System Events" to display dialog with delay "10" "Running Health Check \r \rPlease allow 30 seconds or more to complete." with title "Health Check"'
I've read that you can use the "delay" function, but I'm having problems getting it inline.

I found my answer, what I needed to do was add in the following...
giving up after 10
So my overall script now looks like this...
osascript -e 'tell app "System Events" to display dialog with delay "10" "Running Health Check \r \rPlease allow 30 seconds or more to complete." giving up after 10 with title "Health Check"'

Related

Getting several apps in fullscreen in a row via Applescript

So I am writing a script that would set up my working environment by opening and then getting several apps in fullscreen via a single command line. I am having trouble with the "getting the apps in fullscreen" part, and the script execution is very confusing to me.
My hardware is a MacBook Pro (15-inch, 2017) running on macOS Catalina 10.15.7.
Here is the part of the script that behaves erraticaly when executed:
script1.scpt:
tell application "Sublime Text"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
When executed while the app (here, Sublime Text) is not in fullscreen via the command osascript script1.scpt, the app is activated and goes fullscreen, but key code 123 using {control down} does not work, without producing any error message or sound.
When executed while the app is already in fullscreen via the command osascript script1.scpt, the app is activated, stays in fullscreen as intended but I get this error message:
execution error: System Events got an error: Can't get window 1 of process 1 whose frontmost = true. Invalid index. (-1719)
Also, key code 123 using {control down} does not work either.
When executed via a shell script calling script1.scpt while the app is not in fullscreen, the app behaves exactly as in case 1.
When executed via a shell script calling script1.scpt while the app is already in fullscreen, the app behaves exactly as it should and everything seems fine. But if I lengthen the script to get 2 apps in fullscreen in a row as in script2.scpt (see below), the 2nd app does stricly nothing, and I get the error message of case 2 again.
script2.scpt:
tell application "Sublime Text"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
tell application "Skim"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
I have been spending hours trying to understand what is going on without success, so any help or clue would be very welcome.
The only way I found to get things working is to separate the code in separate steps with 0.5sec pauses in-between them, like in the following shell script code block. But I would like to find a faster and "cleaner" solution to my problem.
osascript -e "tell application \"Sublime Text\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Skim\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Finder\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Terminal\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell"'
EDIT: For the moment being, I managed to get my script doing what I wanted it to do pretty reliably. Not completely satisfied though, because I still have to rely on fixed delays and this makes the execution a bit slow. This is what the code looks like:
in .bash_profile:
cd ~/<path>/ ;
osascript workspace_on.scpt
workspace_on.scpt:
tell app "System Events" to tell process "Terminal" to set the value of attribute "AXFullScreen" of window 1 to true
delay 0.6
tell app "Sublime Text" to activate
delay 0.6
tell app "System Events" to tell process "Sublime Text" to set the value of attribute "AXFullScreen" of window 1 to true
delay 0.6
do shell script "open -a skim ~/<path>/main.pdf"
delay 1
tell app "Skim" to set interaction mode of document 1 to full screen mode
delay 0.6
do shell script "open ~/<path>"
delay 1
tell app "System Events" to tell process "Finder" to set the value of attribute "AXFullScreen" of window "latex-template" to true
Just to copy-and-paste my most recent comment from above:
“Like you, I've spent a bit too much time figuring out this problem. It's quite a frustrating situation because I had a few light bulb moments of inspiration that, in theory, ought to have worked, and worked well, but ultimately didn't work at all. ¯_(ツ)_/¯ But in the process, I've at least written out a naive implementation that incorporates conditional checks and operates on each app one-by-one. It's not what I had aimed for in my head, but it works. See below.”
set Apps to {"TextEdit", "ManOpen", "Usenapp", "QuickTime Player"}
tell application id "com.apple.SystemEvents"
set dock to list 1 in the process named "Dock"
repeat with A in the Apps
tell my application named A to activate
set _P to (a reference to the process named A)
set _W to (a reference to _P's front window)
set _B to (a reference to the value of _W's ¬
attribute "AXFullScreenButton")
set _M to (a reference to (_P's menu bar 1's ¬
menu bar items's menu 1's menu items ¬
whose name = "Enter Full Screen"))
tell (a reference to (the dock's ¬
first UI element whose ¬
name = A)) to if ¬
exists then click
tell _W to repeat 60 times -- 60 x 0.5 = 30 seconds
delay 0.5
if it exists then
click _B
if (the value of attribute ¬
"AXFullScreen") ¬
then exit repeat
end if
end repeat
end repeat
end tell
System information:
AppleScript version: 2.8 system version: 12.6 ("Monterey")

macOS version 12.3.1: how to quit ScreenSaverEngine by using AppleScript

I use code in mac shell,but it make my computer become black screen:
open -a ScreenSaverEngine.app; sleep 5; osascript -e 'tell application "ScreenSaverEngine" to quit'
Quitting the Screen Saver is easy, but loginning back will require the user password. You have to grant 2 permissions to Terminal.app as well, when first running the osacript bellow. 1) Permissions to control the System.Events.app, 2) permissions to control computer (that is, to be able to send keystrokes):
osascript -e 'tell application "System Events"
tell screen saver preferences
start
delay 5
stop
end tell
delay 1
keystroke "YourUserPasswordHere" & return
end tell'
Other way is: uncheck Require password after sleep or screen saver begins in the General tab of Security&Ptivacy pane of System Preferences. Then you don't need login back.
osascript -e 'tell application "System Events"
tell screen saver preferences
start
delay 10
stop
end tell
end tell'
Note: you can retain iCloud keychain in the pop up dialog.

Increase timeout when prompting using osascript from bash script

I'm working on some bash scripting for use in JAMF in my environment. My scripting skills include: googling other peoples scripts, clipping out pieces of those and pasting them into my own script. Then eating the paste. I'm having a bit of a bizarre issue with a piece of AppleScript that runs within a bash script. Original script follows:
#!/bin/sh
# Remove pre-existing settings
rm -rfv /var/db/scrubbed/hostName
rm -rfv /var/db/scrubbed/imageTech
rm -rfv /var/db/scrubbed/adBinding
# Prompt for Hostname of new computer
hostName="$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1' -e 'text returned of result')"
/bin/echo "Computer hostname set to $hostName"
This code listed works just fine. Pops up my dialog box as expected. Unfortunately the box times out after 60 seconds. This is part of an imaging process so if the tech walks away for a few minutes the script continues and the hostname does not get set. Doing some research I found the with timeout of X command in AppleScript. When I update the first block of code to this:
# Prompt for Hostname of new computer
hostName="$(/usr/bin/osascript -e 'Tell application "System Events" with timeout of 86400 to display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1' -e 'text returned of result')"
/bin/echo "Computer hostname set to $hostName"
If it helps, this runs in the actual apple script app
tell application "System Events"
with timeout of 86400 seconds
display dialog "Please enter the hostname of the new computer" default answer "" with title "Hostname" buttons {"Ok"} default button 1
end timeout
end tell
I'm a scrub! Send help!
You need line breaks in the script, just as in the regular AppleScript. You're also missing end timeout, end tell, and the seconds units.
hostName="$(/usr/bin/osascript -e 'tell application "System Events"
with timeout of 86400 seconds
display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1
end timeout
end tell' -e 'text returned of result')"
If all you were looking to achieve was getting the actual host name of the computer… this one line of AppleScript code will achieve that for you.
set hostName to host name of (system info)
Otherwise
First, the display dialog command is not handled by System Events. The display dialog command is a Standard Additions command.
Another thing to point out is while using the with timeout clause, the script will continue running after the specified amount of time which was set in the clause. This puts you right back to the initial problem of the script continuing, after the time out, without user input.
The display dialog command has a giving up after option which allows you to set the amount of seconds it will wait until the dialogue will close itself and continue on. You can then add an if… then… clause to stop the script if the display dialog gave up after the specified amount of time.
Here is an example of an AppleScript which may be more suitable to your needs. I added a “Cancel” button giving the user the option to stop the script. I also used 10 seconds for testing purposes rather than 86400…. Which can easily be edited
set enterHostName to display dialog ¬
"Please enter the hostname of the new computer" default answer "" with title ¬
"Hostname" buttons {"Cancel", "Ok"} default button 2 giving up after 10
if gave up of enterHostName then
return
else
set hostName to text returned of enterHostName
end if
Again, the code was only an example. You will have to tweak it to work correctly within Terminal. Or you can save the code as an AppleScript file and use the osascript command in terminal to run the AppleScript file.

How to make osascript display dialog while script continues other commands

So I'm looking at running lets say the following script
#!/bin/bash/
do
echo "Starting script"
osascript -e 'tell app "System Events" to display dialog "Currently script is running, please do not use computer"'
do somecommands
done
I would like the display to stay on the screen while the "do somecommands" is running in the background without terminating that display. Is this possible?
Thanks
You can do it by running the osascript in the background like this:
#!/bin/bash/
echo "Starting script"
osascript -e 'tell app "System Events" to display dialog "Currently script is running, please do not use computer"' &
# do other stuff
However, you will have 2 new problems - how to dismiss the dialog when you are finished and timeouts.
So, if you want to dismiss the dialog later, you will want to know its process id, so you should capture that after you start the background job like this:
osascript -e .... &
pid=$!
# Do some other stuff
# kill the dialog
kill $pid
Unfortunately, that doesn't seem to make the dialog go away - maybe someone else can help with that.
Secondly, if you are doing something time-consuming, the dialog will time out, so you may want to add a timeout like this for, say 100 seconds:
osascript -e 'tell app "System Events" to display dialog "Currently script is running, please do not use computer" giving up after (100)' &
Maybe that is the better way to do it anyway, run a loop and if the timeout expires and you are still busy, redisplay the dialog and if you are finished don't re-display the dialog - then you don't have the problem of how to make it go away at the end.

In Mac OS X, is there a programmatic way to get the machine to go to sleep/hibernate?

Is there some command line or AppleScript that I can write/run to make the machine go to sleep automatically or even better, into hibernate mode?
I can run the program as root.
osascript -e 'tell application "Finder" to sleep'
To go into Hibernate mode, you'll need to set the correct setting on the power manager.
Possible Settings:
0 - Old style sleep mode, with RAM powered on while sleeping, safe sleep disabled, and super-fast wake.
1 - Hibernation mode, with RAM contents written to disk, system totally shut down while “sleeping,” and slower wake up, due to reading the contents of RAM off the hard drive.
3 - The default mode on machines introduced since about fall 2005. RAM is powered on while sleeping, but RAM contents are also written to disk before sleeping. In the event of total power loss, the system enters hibernation mode automatically.
5 - This is the same as mode 1, but it’s for those using secure virtual memory (in System Preferences -> Security).
7 - This is the same as mode 3, but it’s for those using secure virtual memory.
So after all that, if you want to hibernate automatically and you don't have secure VM turned on then you could run the following shell commands (as a setuid root script):
pmset -a hibernatemode 1; osascript -e 'tell application "Finder" to sleep'
Stolen from this excellent collection:
Shut down without showing a confirmation dialog:
osascript -e 'tell app "System Events" to shut down'
Shut down after showing a confirmation dialog:
osascript -e 'tell app "loginwindow" to «event aevtrsdn»'
Restart without showing a confirmation dialog:
osascript -e 'tell app "System Events" to restart'
Restart after showing a confirmation dialog:
osascript -e 'tell app "loginwindow" to «event aevtrrst»'
Log out without showing a confirmation dialog:
osascript -e 'tell app "System Events" to «event aevtrlgo»'
Log out after showing a confirmation dialog:
osascript -e 'tell app "System Events" to log out'
Go to sleep (pmset):
pmset sleepnow
Go to sleep (AppleScript):
osascript -e 'tell app "System Events" to sleep'
Put displays to sleep (10.9 and later):
pmset displaysleepnow
The common shutdown utility has a -s option to schedule sleep, rather than a restart or shutdown.
$ sudo shutdown -s now
It does not appear to support hibernation, though.

Resources