I have two very simple OSA scripts to allow logon and logoff of computers in a lab environment. These scripts work flawlessly in Snow Leopard when pushed via ARD, interactively within an ssh session, but they fail on machines running Lion.
Stripped down to its essentials, the logout script looks like this:
osascript -e 'tell application "System Events" to log out'
WORKS when run directly from an interactive shell on a machine
WORKS when pushed from ARD
FAILS with "execution error: The variable out is not defined. (-2753)" when run from an ssh session
WORKS when the script is compiled to an .scpt, then run from ssh (e.g. "/usr/bin/osacript logout.scpt")
The login script is directly based on this. A stripped-down version that exhibits the problem is:
osascript -e 'tell application "System Events" to keystroke "frontend"'
WORKS when run directly from an interactive session
WORKS when pushed from ARD
FAILS with execution error: An error of type -10810 has occurred. (-10810) when run from ssh
WORKS when run as a compiled scpt and run from ssh
Because these scripts work fine interactively, and because they worked fine in all modes in Snow Leopard, I think something must have changed in osascript, but I don't know what, and the error messages aren't very descriptive. Any suggestions would be welcome.
Try escaping the quotes.
So:
osascript -e 'tell application "System Events" to log out'
becomes:
osascript -e 'tell application \"System Events\" to log out'
And
osascript -e 'tell application "System Events" to keystroke "frontend"'
becomes:
osascript -e 'tell application \"System Events\" to keystroke \"frontend\"'
Give that a go and tell us what happens.
Related
I have TextEdit open.
I want to enter "hello" in TextEdit, but not by typing it in myself, but using a command from Terminal.
I tried this:
osascript -e 'tell application "TextEdit" to keystroke "hello"'
but it give the following error:
31:48: execution error: TextEdit got an error: Can’t get keystroke "hello". (-1728)
What am I doing wrong?
red_menace is right, this works:
osascript -e 'activate application "TextEdit"'; osascript -e 'tell application "System Events" to keystroke "hello"'
I want to start two different databases but want to keep each process running in a separate tab in a terminal. How do I do this within a shell script? I currently have the following code:
#! /bin/bash
mysqld &
redis-server
Each database needs its own tab. This is on OSX.
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' -e 'tell application "Terminal" to do script "mysqld" in selected tab of the front window'
(based on https://stackoverflow.com/a/7177891/1566267)
The similar command is for redis-server.
I have a shell script that runs an applescript. The applescript is executed from a terminal and I want that terminal to close after its done.
In my shell script I execute my applescript with this line. It opens a terminal, runs and then the terminal just sits there even though the applescript is done. I have other terminals open that I use all the time so I don't want to close all of them, just that one.
osascript -e 'Tell application "Terminal" to do script "osascript ~/Scripts/reset_simulator.applescript"'
Thanks
In your shell script, you executing the
osascript -e 'Tell application "Terminal" to do script "osascript ~/Scripts/reset_simulator.applescript"'
what really opens an new Terminal.app window.
Have you some special reason to open an new terminal window? Why just not running the
osascript ~/Scripts/reset_simulator.applescript
from your shell script and in the current window.
But if you want such thing (sounds to me strange) always can use the next:
osascript -e 'Tell application "Terminal" to do script "osascript ~/Scripts/reset_simulator.applescript;exit"'
#note the "exit" here ---------------------------------------------------------------------------------^^^^^
and change the Preferences to "close window" when the shell exits.
Reading your comments above, sounds me than you want run some applescript in the "background".
So you can do the next:
a.) from your shell script
osascript ~/Scripts/reset_simulator.applescript &
2.) if you want still open the another terminal window
osascript -e 'Tell application "Terminal" to do script "osascript ~/Scripts/reset_simulator.applescript&exit"'
#note the & here --------------------------------------------------------------------------------------^
In your Terminal settings, change the setting under "Shell->When the shell exits" to "Close if the shell exited cleanly".
I am using max msp to run shell commands, I have been prototyping code in applescript and need them to run in osascript for example -
tell application "Google Chrome" to close tab 1 of window 1
converts to
osascript -e 'tell application \"Google Chrome\" to close tab 1 of window 1'
I have converted around 10 commands, but am stuck on the very last one which is
tell application "Google Chrome" to activate
tell application "System Events"
tell process "Google Chrome"
do shell script "/usr/local/bin/cliclick/ c:360,550"
end tell
end tell
which I think goes to
osascript -e 'tell application \"Google Chrome\" to activate' -e 'tell application \"System Events\" to tell process \"Google Chrome\" to do shell script \"/usr/local/bin/cliclick c:360, 550\"'
cliclick lets you use the mouse through shell. http://www.bluem.net/en/mac/cliclick/. The c is the command identifier for clicking, so at x360 y550
is my syntax correct? it works when I dont include the c identifier.
Thanks
I tried this and it gave me this error message:
99:151: execution error: System Events got an error: Invalid argument “360,” to command “c”: Expected two coordinates, separated by a comma. Example: “c:123,456” (1)
Solution: you have an extra space before the number 550 (c:360, 550) and the second value gets lost. Remove the space character and it should work (c:360,550)…
Here is a version where I also changed the quoting:
osascript -e "tell application \"Safari\" to activate" -e "tell application \"System Events\" to tell process \"Safari\" to do shell script \"/usr/local/bin/cliclick c:360,550\""
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.