How can I detect if the screen is locked or screensaver is active from the Terminal in OS X 10.6? - macos

Due to the new limitations of Kerberos in OS X 10.6, I'm working on a script that offers similar functionality to what used to be available in 10.5. Essentially it parses the klist output to see if your ticket is expiring and displays how long until it expires. If we hit the 10 minute mark, it calls kinit to do a GUI password prompt to ask for your kerberos password. If the ticket is expired it does the same thing.
The script makes sure that kinit is not running before calling it again so we don't have multiple kinit calls, and the script works really great (called out of GeekLog so you can see the status). Problem is this morning my system was giving the spinning beachball when I went to unlock the screen. I am suspecting my script and/or kinit from doing something; the machine was available via ping, but otherwise unresponsive to ssh or AFP.
So what I want to do is detect whether or not the screen is locked or the screensaver is engaged. I've found that on previous versions of OS X, you could grep for ScreenSaverEngine to determine whether it was active or not, but that doesn't seem to be the case any longer.
So how can I determine if the screen is locked or otherwise engaged using commandline tools? If the screen is locked, I want the script to simply exit so it doesn't bother with the klist or try to do a kinit. I'm hoping that will prevent the lockup I experienced this morning. Any ideas?

A bit of a kludge but you can easily query the System Events background app via Apple Events to tell if a screen saver is running. For example:
$ osascript -e 'tell application "System Events"' \
> -e 'get running of screen saver preferences' \
> -e 'end tell'
false
$ # engage screen saver after starting next command
$ sleep 5; osascript -e 'tell application "System Events"' -e 'get running of screen saver preferences' -e 'end tell'
true
$
You probably really need to find out why you're getting the lock up, though.

Just out of curiosity, have you tried ssh'ing into the OS X machine and compare the process list before/after the screen saver / lock ?
That's what I'd try.

Related

Is there a way to get my laptop to beep from within a bash script running on a remote server via SSH?

I have a bash script that I have to regularly run on a remote server. Part of the script includes running a backup which takes a while, and after it has run, I have to hit "Y" to confirm that the backup worked before the script will continue.
I would like to know if there is a way to get my laptop to make a beep (or some sort of sound) when that happens. I know that echo -e '\a' makes a beep, but if I run it from within a script on the remote server, the beep happens on the remote server.
I have control of the script that is being run, so I could easily change it to do something special.
You could send the command through ssh back to your computer like:
ssh user#host "echo -e '\a'"
Just make sure you have ssh key authentication from your server to your computer so the command can run smoothly
In my case the offered solutions with echo didn't work. I'm using a macbook and connect to an ubuntu system. I keep the terminal open and I'd like to be informed when a long running bash script is ready.
What I did notice is that if I shutdown the remote system then it will beep the macbook and show an alarm icon on the relevant tab. So I have now implemented a bit of dirty workaround:
sudo shutdown 1440 && shutdown -c
This will initiate the system to shutdown and will immediately cancel the request. And I do get the alarm beep + icon. You will need to setup sudo to allow the user to permit shutdown. As it was my own remote server it was no problem but could limit the usability for others.

macOS - Switch to admin user when running a shell script as a root user

So, I have this name.sh script with bunch of shell commands.
I need to change the wallpaper before ending that script.
I'm using
osascript -e 'tell application "System Events.app" to set picture of every desktop to "/Library/Desktop Pictures/my.jpg"'
at the end of the line. The thing is the Apple Script won't run under root. Because System Events will throw 10810 at first and then 600 (Application isn't running). If I run this osascript under admin user, it will work just fine. The wallpaper will be set.
Let me know how you guys can help me in this!
If you are already root, you have full privileges to switch to a different user account at any time. The command for that is su.
If your user is admin,
su - admin <<\:
osascript -e 'tell application "System Events.app" to set picture of every desktop to "/Library/Desktop Pictures/dneg.jpg"'
:
There are various ways to pass the command as standard input to su; perhaps see also Pass commands as input to another command (su, ssh, sh, etc)

Applescript fails with error (-600) when launched over ssh on Mavericks

I've tried searching for this, and have seen others with similar problems but don't seem to have found an answer anywhere....
I have an AppleScript that I am trying to run over ssh so that I can remotely control my mac. This has worked previously with OSX 10.8 but no longer seems to work since upgrading to 10.9.
The command I am executing is:
ssh <user>#mymac.local "osascript -e 'tell application \"iTunes\" to play'"
I have already set up RSA keys so the ssh session opens correctly
I am connecting as the same user that the Mac is currently logged in and running under
iTunes is running on the Mac with that user at the time the script is launched
The script fails, coming back with:
execution error: iTunes got an error: Application isn’t running. (-600)
Similarly, a number of other scripts that I had previously been using also seem to now be broken on 10.9 with the same error, so this seems to be related to the fact that it's running over ssh, rather than an issue with iTunes or a specific application.
I've tried packaging the applescripts as applications, saving them on the remote Mac, and then opening them from within an ssh session, but this also fails:
ssh <user>#mymac.local
open "~/Desktop/Play Music.app"
(Where 'Play Music.app') is an applescript exported as an app).
This does not report an error within the ssh session, but an applescript dialog appears on the remote mac:
I also have several scripts that were scheduled with crontab on my Mac, and these are also failing since upgrading.
I assume this is some sort of security change as part of Mavericks, but I can't seem to find a way to make it work again. Does anyone have any solutions to this?
Application isn’t running(-600) is an operating system error.
An operating system error is an error that occurs when AppleScript or
an application requests services from the Mac OS. They are rare, and
often there is nothing you can do about them in a script, other than
report them.
Arrrrgh! I don't want this to be the answer, but after trying just about everything, this now seems to be working after a restart.... My guess is that something in appleeventsd got confused (although restarting just appleeventsd on its own didn't fix anything). After a restart osascript seems to be behaving again. I'm still not convinced this is fully fixed, but it does seem to be working for the moment...
For me, it was Apple Entitlements in Xcode.
Specifically,
com.apple.security.temporary-exception.apple-events
Set it as an Array
Then add two items to it.
com.apple.finder
com.apple.iTunes
See: My applescript doesn't work any more when I upgrade my OS X to 10.9
Apple Script may not be the issue. Assistive devices could be what is causing this.
Enable access for assistive devices and applications by opening System Preferences > Security & Privacy > Privacy > Accessibility and check the applications you want to allow access.
for me this happened when I tried to open gitk. Changing back to the branch I was on before, and gitk was able to open again
On my computer, with an uptime of 162 days, killing appleeventsd fixed the problem. I think appleeventsd and long uptimes is a bad combination.
System Events is a really finicky asshat component of OS X. Here is my method for getting around that dreaded "Application isn't running -600" error:
set app_name to "System Events"
set the_pid to (do shell script "ps ax | grep " & (quoted form of app_name) & "$
if the_pid is not "" then do shell script ("kill -9 " & the_pid)
tell application "System Events"
-- activate
end tell
I kill "System Events" with a kill -9 and then relaunch it.
#benmarbles code seems to be missing something at the end of line 2 -- it won't even compile.
Anyhow, I've seen the same issue with "Image Events" and I solved it with a simplified version of that script. Here's how I handle it:
tell application "System Events" to set thePID to (unix id of process "Image Events")
set killCMD to ("kill -9 " & thePID) as text
do shell script killCMD with administrator privileges
Replace Image Events with System Events to kill that process instead. The System Events process keeps itself alive, so there's no need to do anything to relaunch it.
I got the same error when I was unable to do GUI Scripts, but changing the System Preferences > Security & Privacy > Privacy > Accessibility settings for that specific app and adding a delay 0.5 between each line corrected it!
I was confused by this message "for System Events" not working in newer versions of Mac OS X from the command line:
osascript -e 'tell application "System Events" to display dialog "Build finished"'
It turns out the syntax of Applescript is (changed to?) just:
osascript -e 'display dialog "Build finished"'
Hello from 2k19 :) The approach below has helped to me
tell app "app_name"
launch
delay 2
end tell
tell app "app_name"
do something usefull
end tell
or
osascript -e "tell app \"app_name\"" -e "launch" -e "delay 2"-e "end tell" -e "tell app \"app_name\"" -e "do someting usefull" - e "end tell"
For my issue resolved by enable access for AEServer by opening System Preferences > Security & Privacy > Privacy > Accessibility
You will also get this error if you run the script as the wrong user via ssh - make sure you are logged in as the same user you want to script.
I killed the "System Events" process from Activity Monitor and then my ssh script worked. "System Events" was restarted as a result. I'm guessing it just got into a messed up state.
For me, the line:
tell application "Contacts"
failed with the error -600 only if the Contacts app was already open (most of the time it worked, but I noticed it only failed when the app was running).
I have added:
killall Contacts
in the bash (or zsh) script calling osascript and now I don't get the issue any more.

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.

Disable/Cancel Sleep Command on MacOSX

It seems to be impossible to completely disable the Sleep option in MacOSX so that a user cannot manually put the system to sleep.
Is there a way in Leopard (or even Snow Leopard) for AppleScript to catch the Sleep event and cancel it?
The answer to your problem is however a simple Apple-osx-Terminal command that prevents OSX to go idle:
$ pmset noidle
Preventing idle sleep (^C to exit)...
The only way to reset the no-sleeping is to issue a 'Control-C'.
See the man pages of pmset for more details.
This command has been deprecated in favor for
$ caffeinate -i
Which does roughly the same.
You can also run caffeinate for a pre-determined amount of time to prevent sleeping, say for 4 hours while you download something, and then run it in the background by adding & to it:
$ caffeinate -t 144000 &
You can't do this from user mode, because the power manager prevents it, so you'll need to use a kernel extension, such as InsomniaX.
Newer version(s) of OSX make it more possible to disable certain options according to https://apple.stackexchange.com/questions/35256/mac-os-x-lion-server-how-to-disable-sleep-restart-shutdown-options-for-users

Resources