Computer Shut Off Python 3.4 - macos

A friend of mine wants to automatically shut off his computer using Python 3.4 (to put in a program). Does anyone know a basic as-small-as-possible way to do this?
OS:
Mac OSX Yosemite
Thanks

OS X is a Unix at its base, so you can use Unix commands. you can use either:
#!/usr/bin/python
import os
os.system("shutdown -h now")
Must be run as root, won't work otherwise. You can however add to sudoers (man visudo) shutdown as NOPASSWD program for the users that want to execute the script and use sudo shutdown … instead of just shutdown…
or
import subprocess
subprocess.call(['osascript', '-e',
'tell app "System Events" to shut down'])

Related

How to change volume with Python?

I'm writing a program to wake me up mornings, but I want my program to play alarm sound as loud as its possible. so it needs to raise up volume to 100%. but I don't know how. I'm using python3 on macOS Sierra.
You can control the volume of your computer with Applescript:
set volume output volume 100
To execute Applescript from python you can use py-applescript which can be installed with sudo easy_install py-applescript. The following script will set the volume:
import applescript
applescript.AppleScript("set volume output volume 100").run()
EDIT: For Python3.6 you can use osascript instead: pip3.6 install osascript and:
import osascript
osascript.osascript("set volume output volume 100")
You do not need anything outside of the standard library to do this in python. Apple supports executing AppleScript from the terminal so the subprocess module is sufficient.
from subprocess import call
call(["osascript -e 'set volume output volume 100'"], shell=True)

How to hide/quit terminal while program is still running in OS X?

I'm doing my project and I need to log keystrokes system wide in macOS. So, I turned over to GitHub and found Swift-Keylogger. The only problem is I can't quit the Terminal while the program is still running.
Is there any way to not to run this in terminal or closing the terminal window instead of creating a mac app like here.
Note: There are some mac app style executables in github but they are not providing the output I want and some need additional permissions.
Instead of running the executable like ./Keylogger use nohup ./Keylogger &.
You can quit the Terminal after executing the command.
To kill the Keylogger, run ps -e | grep "Keylogger" to get pid and kill -9 pid.
P.S. I thought of adding it as well as running at startup but I'm too lazy to update the repository.

Call osascript through perl in OSX 10.9 doesn't work anymore

I'm writing an application for Mac (OSX, not iOS).
The installation runs a perl script which contains some "osascript" call, for example:
$res = qx(osascript -e 'launch application "MyApp"');
Or:
$res = qx(osascript -e 'tell application "System event" to make login item at end with properties {path:".... .app", hidden: false}');
and more...
I have two Mac machines, one with OSX 10.9 and other with OSX 10.7.
When I'm running the installation on OSX 10.7 - All works good.
When I'm running the installation on OSX 10.9 - the call to osascript doesn't work (all of them, not only one). I tried to run only the osascript command in terminal, and it works!
The installation process done from user (not admin) but ask for admin permission during the installation.
Someone know what have been changed in the new OSx?
Thanks!

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.

Taking screen shot of current OS X or iTerm terminal window

In Linux, I'm using imagemagick import -window $WINDOWID to take a screencapture of the terminal.
Is there something similar in OS X?
You want to be using screencapture rather than import, which only works on X11 windows.
If the window is in front, you can try:
screencapture -l$(osascript -e 'tell app "Terminal" to id of window 1') test.png
See this question for some more background.
Try the tty command.
$ tty
/dev/ttys001
You can see what commands are running on that terminal by using `ps -t :
$ ps -t s001 #Where `tty` returns /dev/ttys001
You can also try pgrep too:
$ pgrep -t s001
Edit (You can/I can)
Oh, this is for a screen capture... Wasn't in the OP.
$WINDOWID is a X11 thing, and so is imagemagick. Linux GUI (both KDE and Gnome) are based upon the X11 protocol. The Mac GUI isn't.
On Mac OS X, there's a screencapture command. I haven't used it, but let's look at the manpage:
The screencapture utility is not very well documented to date.
You're on your own.
One more trick...
You can run the X11 server on the Mac. The server is no longer included in Mountain Lion, but Apple recommends you to install it from the XQuartz Project.
You can run the X11 server, then use XTerm windows which will have a Windows ID that can be used with imagemagick.
( Command + Control + Shift + 4 + Space ) for capturing a screenshot of the app window ..
Refer to this link: https://support.apple.com/en-us/HT201361 for learning more about capturing screenshots in macOS.. 👍

Resources