can i run the terminal in the background from applescript - applescript

I have an applescript that calls the terminal to run some functions every time my mac wakes up from a sleep. I am getting a bit annoyed of the terminal constantly popping up everytime I open up the laptop. is there a way i can set it so that the terminal can run in the background or in a minimised window and not keep popping up?
My AppleScript is as follows:
set desktop_folder to "$HOME/Desktop"
tell application "Terminal"
activate
do script "cd desktop;cd project34;python3 main.py"
end tell

You can use this by itself, with out the Terminal block:
do shell script "cd ~/Desktop/project34; python3 main.py"
Note that you may need to include the path to python3.

Related

Prevent macos automator Terminal ssh script from opening two windows

I use a small macOS Automator script to ssh into a remote host so I can add it to the Dock:
tell application "Terminal"
activate
do script ("ssh robot#example.com;")
end tell
When used the first time, I get the expected remote Terminal window, but also an extra local Terminal window. All subsequent uses only generate a single window to the remote host.
I've tried some small alternatives like:
set currentWindow to do script...
and:
set currentTab to do script...
with the same result. Is there any way to prevent the initial local Terminal window?
I found a post that has the answer:
tell application "Terminal"
if not application "Terminal" is running then launch
do script ("ssh robot#example.com;")
activate
end tell

How to avoid a zombie window when running MacOS shel/Applescript script

I routinely open a lot of Terminal windows each with an ssh session. To streamline the process I have a series of shell scripts that look like this:
#!/bin/sh
osascript <<EOS
tell application "Terminal"
activate
do script "set_background_color salmon; ssh alan#demo.znyx.com"
end tell
EOS
This works. I can either execute the script from a shell prompt or click it in the Finder window. (To do this the file name suffix is .command instead of .sh)
The problem is that when I execute it from Finder, I end up with a dead window ("Process Completed") behind the window I intended to open. Is there any way to get rid of it or not have it open in the first place?
UPDATE:
The solution in this other question results in dialog boxes appearing that provide confirm/review/cancel options. This is undesirable.
When you double-click a .command file, the file is passed to Terminal and Terminal creates a window and executes the commands within it. The commands in your .command file use AppleScript to tell Terminal to run a command in yet another window.
Why not remove the middle man and just put the ultimate commands you want to run — set_background_color salmon; ssh alan#demo.znyx.com — in the .command file? Drop that stuff with running an AppleScript. In other words, the contents of your .command file should just be:
set_background_color salmon
ssh alan#demo.znyx.com
Alternatively, you could take the AppleScript from your .command file (the part between <<EOS and EOS), put it into Script Editor.app, and save it as an applet.

Shortcut to running Mac Terminal commands

Ok... I've been searching for an answer to this for a while and can't seem to find any good examples, so I thought I'd break down and ask.
How can I create a shell file (.command) in OSX that I can just double-click on which:
Opens a new Terminal window
Runs a few commands
... and stays active so I can continue to run other things!
My goal is to setup various environments using individual .command files, which will each set variables and run certain command line tools, and then remain open to manually run other commands. I currently have one like this:
#!/bin/sh
export MY_VAR_A="blah A"
export MY_VAR_B="blah B"
cd /Users/
... and this doesn't work. It just opens a Terminal window with this output:
Last login: Sat Aug 17 12:52:15 on ttys000
unknown60c5470527e4:~ me$ /Users/me/Documents/test.command ; exit;
logout
[Process completed]
Is there a better (or just different) way of accomplishing what I want? Or do I just need to adjust something simple in my current .command file?
Use applescript
tell application "Terminal" to activate
tell application "Terminal"
do script ("ls -l") in window 1
do script ("cd /Users/test/Music/iTunes/") in window 1
do script ("ls -l") in window 1
end tell
Save apple script as application bundle.

Close Terminal window from within shell script (Unix)?

Is there a way to close a Terminal window from within a shell script? I have a .command file that should just get out of the way once it's done.
Using exit 0 will cleanly terminate the script.
Whether Terminal window stays open is user-configurable. The default is to always stay open. To change this:
Terminal.app > Preferences > Profiles > Shell
- "When the shell exists:"
> Close if the shell exited cleanly
- "Ask before closing:"
(•) Never
-- OR --
(•) Only if there are....
When "Close if shell exited cleanly" is used, the script will close the window if the exit result is 0, which is the default if nothing went wrong.
Since you don't want to delete all Terminal windows, first change the name of your window from "Terminal" to something else:
echo -n -e "\033]0;My Window Name\007"
Then at the end of the script, use:
osascript -e 'tell application "Terminal" to close (every window whose name contains "My Window Name")' &
You can use apple script to quit the terminal app. Add the following to your script -
osascript -e 'tell application "Terminal" to quit'
This will give you a popup confirming to close the app. You can disable this in Terminal preferences.
Alternatively, you can also use killall command to quit the app. The following would work just as well.
killall Terminal
Note:
Just as a side note, you can freely add the above commands to your script and it would work as you want. However, there are few caveats. First being you will limit the ability of your script to work on different boxes. Secondly, it would be safer to use nohup so that any commands that are currently running won't quit due to quitting of the Terminal app.
This works for me:
#!/bin/sh
{your script here}
osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &
exit
This will work for closing just your windows opened with a .command file but leave things already running in other terminal windows. I know that I almost always have either sass or grunt watch something so I don't want to quit terminal totally.
closeWindow() {
/usr/bin/osascript << _OSACLOSE_
tell application "Terminal"
close (every window whose name contains "YourScriptName")
end tell
delay 0.3
tell application "System Events" to click UI element "Close" of sheet 1 of window 1 of application process "Terminal"
_OSACLOSE_
}
This will close the Terminal window for your script and keep any other Terminal windows open as long as their window titles don't match. For it to work Terminal will have to be added to the list of applications permitted to use the Accessibility framework. You can also cycle through Terminal windows with a repeat command and close every window x that contains a UI element "Close" on sheet 1.
I find the best solution for this is to use Automator to create a true OSX application which will work the same way regardless of how your system is configured. You can have the Automator run your shell script, or you can embed the shell script itself in Automator.
Here is how you do it:
Run Automator (in Applications).
Choose "New Document" and when it
asks "Choose a type for your document" choose "Application"
In the
left panel, select "Utilities" then "Run Shell Script".
Type in your
script commands in the workflow item in the right panel. You can either call another
shell script, or just put your commands in their directly.
Save the
Application, which will be a full-fledged Mac App. You can even
cut-and-paste icons from other apps to give your script some
personality.
#!/bin/bash -x
{your script here}
. exit 0
kill -9 $PPID
you can also create a shortcut for your script:
cp yourscript.sh ~/bin/yourshortcutnamewhateveryouwant
then type
yourshortcutnamewhateveryouwant
will run whatever is writen into script at any directory.

An applescript stopped working after upgrade from snow leopard to lion

This script worked every time with Snow Leopard.
tell application "Terminal"
activate
do script "cd web_sites/project" in front window # this line highlighted on error
do script "mate ." in front window
do script "rvm 1.8.7" in front window
do script "script/server" in front window
delay 4
do shell script "open -a Firefox http://localhost:3000"
end tell
With Lion I keep getting this error:
error "Terminal got an error: Can’t get window 1." number -1728 from window 1
Thanks.
I'm running Lion. I can execute the following with no errors. I do not get an error in Applescript in any of the following situations 1) the application is not running, 2) the app is running and a window is open, and 3) the app is running and the directory path is not valid. In case 3 the Terminal shows an error but applescript does not.
tell application "Terminal"
activate
do script "cd Development/Images" in front window -- this line highlighted on error
do script "ls -al" in front window
end tell
So your problem is something not related to this actual code.
It seems the problem is not the code but the speed at which the operating system is functioning. As i mentioned before, the code executed just fine in Snow Leopard. To compensate, after the upgrade to Lion, if i add another delay to give Terminal time to finish activating, and increase the delay before opening Firefox, everything works. My laptop is a MacBook Pro with 2.7 GHz Intel Core i7 processor.
tell application "Terminal"
activate
delay 1
do script "cd web_sites/project" in front window # this line highlighted on error
do script "mate ." in front window
do script "rvm 1.8.7" in front window
do script "script/server" in front window
delay 5
do shell script "open -a Firefox http://localhost:3000"
end tell
If you are just trying to get a script to run once, simply retrying may help. Execution speed is also a function of other demands on the system.

Resources