I have a script wherein I need to run an executable from a terminal
the script is as below
tell application "Terminal"
activate
set run_cmd to "sudo <path to my executable>"
do script run_cmd
end tell
but this times out with the error message
error "Terminal got an error: AppleEvent timed out." number -1712
I even tried using a timeout like
with timeout of 5000 seconds
tell application "Terminal"
activate
with timeout of 5000 seconds
set run_cmd to "sudo <path to my executable>"
do script run_cmd
end timeout
end tell
but no luck it still times out...
This behaviour is observed sometimes , not always. I'm using El Capitan Mac OS.
I got a chance to test on Sierra and High Sierra and it works alright there.
Any idea, how can we solve this issue ?
Note: If I open a terminal before running this script, then it executes without any problems.
Thanks in advance
How about trying something like this...
tell application "Terminal" to launch
repeat while application "Terminal" is not running
delay 0.2
end repeat
tell application "Terminal"
activate
set run_cmd to "sudo <path to my executable>"
do script run_cmd
end tell
Related
I am trying to automate this process.
step 1: change system date to a specific date.
step 2: open an application.
step 3: change system date back to normal.
Now on Automator, I have three apple scripts placed like this.
on run {input, parameters}
tell application "Terminal"
do script with command "sudo date 082704002018"
activate
end tell
delay 1
tell application "System Events"
keystroke "mypassword" & return
delay 3
end tell
end run
on run {input, parameters}
tell application "Terminal"
do script with command "open -a applicationName"
activate
end tell
end run
on run {input, parameters}
tell application "Terminal"
do script with command "sudo ntpdate -u time.apple.com"
activate
end tell
delay 1
tell application "System Events"
keystroke "mypassword" & return
delay 3
end tell
end run
The problem is that Automator only runs the first code. I'm not sure how to make it run all the codes in order.
Sorry if this is a stupid question. I am completely new to automator and applescript.
Thank you
I'm not quite sure why you chose to use three separate AppleScripts. You can combine them all into one AppleScript as I have done in this following example. I'm not quite sure why you used the “activate” commands. I don't think they are necessary so I removed those lines of the code. Anyway, this following code should work for you…
tell application "Terminal"
do script with command "sudo date 082704002018"
end tell
delay 1
tell application "System Events"
keystroke "mypassword" & return
delay 3
end tell
tell application "Terminal"
do script with command "open -a applicationName"
delay 1
do script with command "sudo ntpdate -u time.apple.com"
end tell
delay 1
tell application "System Events"
keystroke "mypassword" & return
delay 3
end tell
Alternately, launching Terminal app to run shell scripts is not necessary all the time as you can run shell scripts in AppleScript by using the “do shell script” command. This following applescript code is your code using only eight lines of code.
do shell script "sudo date 082704002018"
tell application "System Events" to keystroke "mypassword" & return
delay 3
do shell script "open -a applicationName"
delay 1
do shell script "sudo ntpdate -u time.apple.com"
delay 1
tell application "System Events" to keystroke "mypassword" & return
If my versions of your code throw errors, it may be necessary to adjust the delay commands or re-insert the activate commands
If you are hell-bent on using your version of the code and three separate Applescripts, just remove the on run {input, parameters} and end run lines of code from each AppleScript and that should eliminate your problem
In my Desktop/src/ directory I have shell scripts that I want to run in a series and some must run after the first has completed running and all the commands must run on new terminal window. I am working on Mac OS X.
So far I have tried the following code
osascript<<EOF
tell application "System Events"
tell process "Terminal" to keystroke "n" using command down
end
tell application "Terminal"
activate
do script with command "cd Desktop/src/ && sh startRM1.sh"
end tell
tell application "Terminal"
activate
do script with command "cd Desktop/src/ && sh startRM2.sh"
end tell
tell application "Terminal"
activate
do script with command "cd Desktop/src/ && sh startRM3.sh"
end tell
EOF
The problem is that startRM1.sh takes a little time to start, and startRM2.sh, startRM3.sh start straight away and crash since they must wait for startRM1.sh to complete
All three of them must start in new window.
Edit: startRM1.sh is a server which keeps running; therefore the control never goes to the second step.
problem solved using following
osascript<<EOF
tell application "System Events"
tell process "Terminal" to keystroke "n" using command down
end
tell application "Terminal"
activate
do script with command "cd Desktop/src/ && sh startRM1.sh" in window 1
end tell
tell application "Terminal"
activate
do script with command "sleep 5 && cd Desktop/src/ && sh startRM2.sh"
end tell
tell application "Terminal"
activate
do script with command "sleep 5 && cd Desktop/src/ && sh startRM3.sh"
end tell
EOF
Create a shellscript that is your start point.
You can create a .sh file that would could call:
source mynewShell.sh
and in that, it will have all the other files, called in the orders you need, and any config data etc
Using Applescript, is it possible to open a new Terminal and enter the command into the terminal but do not run it?
tell application "Terminal"
do script "echo Hello"
end tell
This code will type the line echo Hello into the Terminal and run it. Can we avoid the execution?
Good case for System Events app and emulating keystrokes:
tell application "Terminal" to activate -- only needed if Terminal may not be running
tell application "System Events"
tell application process "Terminal"
set frontmost to true
keystroke "echo Hello"
end tell
end tell
I have a modbook with a glitchy digitizer board. Until I can re-shield the cable that is causing the glitch I just want to turn the digitizer off. I found a page which taught me some code and I used it successfully last night. Upon reboot, however, it doesn't work anymore:
The script:
tell application "System Events"
set PTD to (unix id of process "PenTabletDriver") as text
do shell script "kill -STOP " & quoted form of (PTD)
end tell
The error message:
error "Can’t make «class idux» of «class prcs» \"PenTabletDriver\" of
application \"System Events\" into type text." number -1700 from
«class idux» of «class prcs» "PenTabletDriver" to text
Can I alter the code somehow to fix this problem?
PS:
I have read this post and, though it's similar, I do not understand how it could be applied to my problem.
Life is too short to mess with AppleScript. Try running the following command at the terminal prompt:
pkill -STOP PenTabletDriver
Also, check your login items to see if the driver is automatically started each time you log in. (More likely, though, is that it's configured to start at boot time via launchd.)
You can save the following script in a little applet:
set shellStr to "pkill -STOP PenTabletDriver"
do shell script shellStr
and run it whenever you need.
I think the other answers make a good point, a shell command is nice and quick. BUT, if you must have it in AppleScript, this seems to work for me....
tell application "System Events"
set PTD to (unix id of process "iTunes")
do shell script "kill -STOP " & quoted form of (PTD as text)
end tell
which results to
tell application "System Events"
get unix id of process "iTunes"
--> 37987
do shell script "kill -STOP '37987'"
--> error number -10004
end tell
tell current application
do shell script "kill -STOP '37987'"
--> ""
end tell
The Process ID is just a number so there is no need to quote it...
tell application "System Events"
set PTD to (unix id of process "iTunes")
do shell script "kill -STOP " & PTD
end tell
The above code is sufficient.
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.