How to make osascript display dialog while script continues other commands - bash

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.

Related

How to force close terminal window id using AppleScript?

I am starting a new terminal window from a terminal:
osascript -e 'tell app "Terminal" to do script "myProgram.sh"'
Since the new process is not ending(it is basically a server), I want to be able to stop it at some point using AppleScript.
If I do:
osascript -e 'tell app "Terminal" to close window id {windowId}'
I am getting the alert box "Do you want to terminate running process...?".
Is it possible to force close the window id? If not, how can I confirm this alert box using AppleScript?
You could try this:
do shell script "killall -QUIT Terminal"
I hope this helps you solve your problem 🙂.

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.

Apple script open Terminal with ready command

I'm trying to open terminal using apple script with a ready command but without executing it and allowing user to do this just by clicking enter (so I don't want to use tell Terminal to do script)
One of the approaches I used is using keystrokes:
tell application "Terminal" do script "echo Hi!"
keystroke "abc"
end tell
but it doesn't work for me. Any ideas?
I think you want to start Terminal and have a command all lined up ready in the Terminal ready for the user so he/she only has to press "Enter". If so, you can do this:
tell application "Terminal"
activate
delay 1
tell application "System Events"
keystroke "echo hi"
end tell
end tell
Then the user just has to press Enter and the command echo hi will execute.
it is hard to understand what you mean.
You can't for instance have the terminal wait for a user to click its window.
(But you can poll for a keystroke after the terminal window is opened.)
You'd have to use a dialog before your code, in order to make the user enter the terminal consciously.
display dialog "Press ok to enter the terminal" buttons {"Cancel","Enter"} cancel button 1 default button 2
Other than that, the way you'd need to use system events to send keystroke to the Terminal
tell application "System Events"
tell application process "Terminal"
keystroke "abcd"
end tell
end tell
You can poll for a keypress in the do script command to your terminal with this:
read -n 1 -s MYCHAR </dev/tty
This will force the user to press enter from a do script
a=`read`

Setting popup box with osascript and a 10 second delay

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"'

Script to shutdown mac

I'm trying to automate the shutdown of my mac, I've tried the scheduled shutdown in energy saver and I wanna sleep but these don;t seem to work. VLC player runnign seems to prevent the shutdown. I think I need a script to forcefully shutdown the mac regardless of of what errors may thrown to screen by various programs running.
Thanks
Ok,
This is the applescript code im using to shutdown may mac. I've added it as an iCal event thats runs nightly.
tell application "System Events" to set the visible of every process to true
set white_list to {"Finder"}
try
tell application "Finder"
set process_list to the name of every process whose visible is true
end tell
repeat with i from 1 to (number of items in process_list)
set this_process to item i of the process_list
if this_process is not in white_list then
do shell script "killall \"" & this_process & "\""
end if
end repeat
on error
tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
tell application "System Events"
shut down
end tell
Could you try a simple applescript, which goes something like this...
tell application "System Events"
shut down
end tell
See if it works, and then you can make it run through Automator at certain time, etc.
my solution (somwhat late). Just a bash script with apple in it:
#!/bin/bash
# OK, just shutdown all ... applications after n minutes
sudo shutdown -h +2 &
# Try normal shutdown in the meantime
osascript -e 'tell application "System Events" to shut down'
I also edited the /etc/sudoers (and /private/etc/sudoers) file(s) and added the line:
ALL=NOPASSWD: /sbin/shutdown
Always worked for me for an assured shutdown (knock knock ;-) )
This should do:
do shell script "shutdown" with administrator privileges
If you want to pass the admin password from key chain, with no prompt:
do shell script "shutdown" with administrator privileges password "password here"
But do not store the admin password in clear anywhere. Instead use the keychain access.
Alternatively you could kill all user processes, via:
do shell script "kill -9 -1"
This however would also kill your own Applescript process, preventing it from requesting the shutdown/restart afterwards.
Either way you're playing with fire, when using sudo or kill.
do what linux users do. use a bash script. if u dont know how to create one just go ahead and download ANY bash script u find using your internet search and open it with text edit app and paste the following:
( be careful if many people use the pc , then this method is not recommended, cause they can learn your user login password from inside this script )
#!/bin/bash
echo -n "Enter a number > "
read x
echo [your password] | sudo -S shutdown -h +$x
it will work the same way it works in linux. the terminal will pop up a message and ask you to enter a number. if we choose for exaple 50 , then the pc ( niresh ) or mac will shutdown in 50 minutes.

Resources