applescript: how to terminate an executing terminal - macos

I'm trying to make an apple script to allow me to manage a cpp project automatically. And I used Jenkins too.
Basically, the cpp project is a kind of infinite loop, which will execute in a terminal. And I set Jenkins rebuild and execute it per 20 sec. In other words, my pc should do the tasks below:
1) rebuild the project;
2) execute the project;
3) wait for 20 sec;
4) close the terminal.
However, when I arrive at 4), a dialog will pop up: Do you want to close this window?
So I have to click the button "Close".
My question is: how to code in applescript to do the 4) automatically?
Here is my applescript:
tell application "Terminal"
do script "cd /Users/Shared/Jenkins/Home/workspace/test"
do script "make clean" in window 1
do script "make" in window 1
do script "./matrix.out" in window 1
delay 20
close window 1 # problem's here
end tell

However, when I arrive at 4), a dialog will pop up: Do you want to close this window? So I have to click the button "Close".
But you can do that using AppleScript too (via GUI scripting thru System Events), so nothing you've said is a problem so far.
tell application "Terminal" to activate
tell application "System Events"
tell application process "Terminal"
tell its window 1
tell its sheet 1
click button "Close"
end tell
end tell
end tell
end tell

I wonder if this might help:
Running process in background after closing terminal
The problem might be that what you're lastly running in the terminal is a process (matrix.out) that will get killed by closing the window, and you don't want that, right? So it may be that you need to alter the matrix.out script using the technique described (if possible) to keep it from doing that.
It may also be possible to solve your problem by making matrix.out executable (chmod it to executable or use Kilometre) and just "open it" using vanilla applescript.

Related

Close Unwanted Mac Terminal Windows En-masse

As a system admin, I write/test/run a ton of shell scripts (typically from BBEdit). I rely on the BBEdit “Run in Terminal” drop-down menu a lot to quickly test run a script.
As a result, I end up with a ton of 'dead' (unwanted/expired) Terminal windows (i.e. executed script windows with “[Process completed]" displayed at the bottom).
Does anyone have a suggestion for how to clean-up all of these dead Terminal windows em-masse?
I can't blindly close ALL windows (or quit Teminal.app) because I have current SSH sessions running and other tasks in-progress. I only want to close the Terminal windows that have been executed via BBEdit and display '“[Process completed]'.
I can't set the Apple Terminal.app preference to do this for me (Terminal -> Preferences -> Settings -> Shell -> When the shell exits) because I generally want to examine the output returned before closing for QA purposes.
My goal is to manually run a script/command from time to time to clean-up all the Terminal windows on-demand as needed. Once I have a ton of confusing dead windows that I dont need Ill run it and then continue working in BBEdit as needed.
After a few hours of writing/troubleshooting/editing a script (or multiple scripts) I end up with dozens of dead Terminal windows - of which I manually close with a click and a Command + W. This gets tedious of course.
I have played around in AppleScript for something to bind to a shortcut or widget, but haven't found a solution as of yet.
Simple commands such as
tell application "Terminal" to close (get window 1)
aren't too helpful because I havent been able to determine which windows have an inactive state and which do not.
Thoughts? Any suggestions are appreciated!
Every "window" object in Terminal has a property named "processes", which is a list of the active processes for that window.
If the "processes" list is empty (in AppleScript terms, processes = {}), then you can safely close the window.
Therefore, an AppleScript like this should work:
tell application "Terminal"
set windowsICanClose to (every window whose processes = {})
repeat with windowToClose in windowsICanClose
close windowToClose
end repeat
end tell
You can save that as a script, put it in BBEdit's "Scripts" folder, assign it a keyboard equivalent if you like, and that should do the job.
It's very simple:
tell application "Terminal" to close (every window whose processes = {})
This will close every window of Terminal that has: [Process completed]
Just to be clear.. If e.g. you have a Terminal window open with multiple tabs, then tell application "Terminal" to close (every window whose processes = {}) only closes the tabs that have [Process completed] and not any other tab in that window. The window is only closed if the only tab of that window contains [Process completed]. That is to say, that is the result as tested under macOS High Sierra anyway.

How to write apple script code to automate daily small task?

I have found the applescript code on internet, to start a terminal and launch Elasticsearch
the code is
tell app "Terminal"
do script "elasticsearch-5.5.0/bin/./elasticsearch"
end tell
It works fine,
now I want to add more stuff on it, I need to open 4 more new tab not new window just tab (command + T). and then run different command such as log tail command, start kibana like one after another in each tab.
I am new to applescript and got tired by searching samples and tutorials, can anyone suggest a solution or your idea to achieve the automation.
Terminal's scripting dictionary isn't very good — a lot of 'Apple Event Handler Failed' errors — but you can manage it with code like the following:
tell application "Terminal" to activate
my makeTab("ls -al")
my makeTab("top")
my makeTab("cd ~/Documents")
on makeTab(cmd)
tell application "System Events" to keystroke "t" using {command down}
tell application "Terminal"
do script cmd in last tab of front window
end tell
delay 0.2
end makeTab
Just put whatever commands you want run inside the makeTab() call.

How can I move a dialog window in Applescript?

I've been told that, technically, you can't move a dialog in AppleScript. Amazingly, the following code works.
I'm running a script for InDesign that creates a dialog box. Opening a new AppleScript Editor window and running this code will move the dialog box:
tell application "System Events"
tell application process "Adobe InDesign CS6"
set position of window 1 to {1000, 400}
end tell
end tell
The problem is: I can't figure out a way to incorporate this code directly into my original script -- it just doesn't work, no matter what I try or where I insert it into the code. I have to run it from AppleScript Editor
Any ideas?
Dialog boxes are usually blocking in the host application. You can certainly tell System Events to move a window, but Adobe InDesign itself is busy throwing up a blocking dialog box at the time, so it can't move its own dialog box.
If you want to get clever, you can fork off a process at the correct time and with the magically correct delays to move your dialog for you. This would be something like
do shell script "osascript -e 'delay 4' -e " & (quoted form of "tell application \"System Events\" to tell application process \"Adobe InDesign CS6\" to set position of window 1 to {1000, 400}") & " > /dev/null 2>&1 &"
Customize the delay as needed. This will be a magical value you that will have to figure out. Depending your needs, you might be able to put in a test to see if the dialog has disappeared before the script continues.
The bit at the end, > /dev/null 2>&1 &, tells bash to redirect stdout to /dev/null (junk it) and redirects stderr to stdout, then tells the whole command to run in the background. Without this entire chunk, this statement will block AppleScript from continuing until the shell script finishes executing.

infinite loop in applescript stops logout

So I am writing a basic applescript to end a very annoying program that randomly opens and interrupts me while Im working. The program needs to stay but i don't want to see it. I want my script to open automatically when the computer restarts (I did this in settings). I want to test to make sure it works but upon saving it as an application I cannot logout or shutdown or restart without manually force quitting. I assume this is because of the repeat loop but i don't know how to fix this. Ive tried everything i could think of. Any help would be greatly appreciated. thank you
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
repeat
if appIsRunning("LiveUpdate") then
tell application "LiveUpdate"
quit
end tell
end repeat
I'm not sure you have the proper approach either. However, if you want to do what you are trying to do then you want to create a stay-open application. You do that by saving the applescript as an application and checking the "stay open after run handler" checkbox. Here's how you write the code for that...
on idle
if appIsRunning("LiveUpdate") then tell application "LiveUpdate" to quit
return 10
end idle
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
Notice the "on idle" handler. That is the handler that is repeatedly run while the application stays open. Notice that I have placed "return 10" at the end of that handler. That determines how often the idle handler runs, in this case every 10 seconds. You can change that to what you want.
The advantage of this method is that you can quit this stay-open application. You won't get stuck in a repeat loop that you can't quit.
You mention that you do not want to see this application while it is running. To make that happen you will have to modify the info.plist file inside the application bundle (right-click on the application and show package contents). You have to add the "LSUIElement" key to the plist and give it a value of true. Then you won't see the application in the Dock while it's running.
Because you can't see it running you will need some way to quit the application. You can do that either using another applescript...
tell application "My Stay Open Application" to quit.
Or you can open activity monitor and quit it from there. Good luck.
Here is another approach:
on idle
if application "LiveUpdate" is running then tell application "LiveUpdate" to quit
return 10
end idle

How to use Automator to bring window to front?

I'm using a Mac, OSX 10.6 and I have a function in a desktop application that I want to automate. Manually I press Command+R wait for the application to read some data form a physical device for 1 minute, then press command+R again to take another reading (at this point it asks me if I want to save the data, so I press tab, tab and then space bar to select to save the data. I do this 3 times in total, so I want to automate the 3 times, so I can walk away from the computer and it will read 3 times automatically.
Is automator the best way to do this?
I've tried to do this already in automator by using the 'watch me do' function. This starts with the 'bring window Untitled to the front', and then the second command is press command+R. I then found a little piece of apple script to wait 1 minute and I plug the first action into that for the wait function.
However, when I click run or step, instead of going and opening up the correct window ("Untitled"), the cursor moves to the Media button in automator, and clicks that instead! But the application is definitely listed as the correct one.
Any help appreciated, but maybe automator is the wrong way to go?
Apple Script is the best way to go for things that don't require any "special processing" that would need to be done by a chain of different applications.
1) uging the AppleScript Utility
make sure that you have GUI scripting enabled in the "AppleScript Utility"
2) using the Script Editor choose File>Open Library and see if your application has any scriptable functions ... these may be a better way to go.
3) Create a new script and put in something like this ...
tell application "Firefox"
activate
delay 1 -- give it time to react
repeat 3 times
-- this gives us the keyboard
tell application "System Events"
keystroke "r" using {command down}
end tell
delay 6
end repeat
end tell
I used Firefox to test it .... should work for you ....
Once you've got the script you can use the save as to make it into an app or save it as a script in your ~/Library/Scripts folder or paste it into an automator workflow and schedule it with iCal.
I don't think automator is the way to go. You could use applescript, but you should take a look at sikuli. You will need to write the Sikuli script yourself, but what you describe shouldn't be difficult

Resources