Hide terminal window using applescript - applescript

The purpose of the current snippet is to run a script in a new terminal window and instantly hide it. The code below initially seems to work fine but as a result, if the window is manipulated using its visible property it simply disappears and doesn't seem to be executing. Right clicking on the Terminal app within the Dock displays like there is no terminal window at all.
tell application "Terminal"
-- New Terminal Window
set newTab to do script "caffeinate -u -t 900"
set caffeinateWindow to id of front window
tell window id caffeinateWindow
set index to 1
set visible to false
end tell
end tell

instead of telling the terminal use applesripts builtin possibilities:
do shell script "caffeinate -u -t 900"
if you ever want to pass parameters then do it like this:
set param to "900"
do shell script "caffeinate -u -t " & param
Note if a parameter might contain spaces you need to escape/quote it like this:
set param to "900"
do shell script "caffeinate -u -t " & quoted form of param
if you still need to hide a window do it like so:
tell application "System events"
try
set visible of application process "Terminal" to false
end try
end

Related

How to prompt for user input and place that value in a terminal command using applescript?

I'm trying to create a script that asks the user what .mp3 they want to split using Terminal through AppleScript / Script Editor. Here's what I have so far.
I am trying to get input to display in this line:
do script "spleeter separate -i (text returned of input).mp3 -p spleeter:2stems -o output"
in terminal, it just says that there are no matches found for (text returned of input).
Here's my code:
set input to display dialog "What would you like to convert?" default answer "" with icon note buttons {"Continue", "Cancel"} default button "Continue"
tell application "Terminal"
do script "cd Desktop/spleetr"
do script "spleeter separate -i (text returned of input).mp3 -p spleeter:2stems -o output"
end tell
You are placing a command inside a string, so it is not getting evaluated. The solution for that is to concatenate the results of the command and the string parts in the desired order.
When using the Terminal, unless specified otherwise, each do script command is run in its own window/tab. If you don't neccessarily need a Terminal window, the do shell script command can be used instead (note that it uses a default shell, so you should use full paths), but to use multiple commands with either one you need to combine the various shell commands into a single statement, otherwise they will be run in separate shells.
I don't have that utility to test, but in the following script I am getting the text in the dialog statement, and quoting the result for the shell script in case it contains spaces, etc:
set input to text returned of (display dialog "What would you like to convert?" default answer "" with icon note buttons {"Continue", "Cancel"} default button "Continue")
-- to run in a new Terminal window: --
tell application "Terminal"
do script "cd Desktop/spleetr; spleeter separate -i " & quoted form of (input & ".mp3") & " -p spleeter:2stems -o output"
end tell
-- or if Terminal is not needed: --
do shell script "cd Desktop/spleetr; spleeter separate -i " & quoted form of (input & ".mp3") & " -p spleeter:2stems -o output"

Switching between two Mac terminal windows

I have an interactive bash script running in a Mac OSX bash Terminal window. I would like, from within that script, to
open a second Terminal window, print in it the content of a variable from the script in the first window,
keep that second window open somewhere on the screen while I continue interacting with the first window, and finally
have the second window closed when I do not need it anymore.
Since I am on Mac OSX, I am thinking of using osascript to run Applescript commands opening the second window, pasting the variable content in it and returning control to the first window, but I cannot make it work.
#!/bin/bash
var2print="I want this to print in the text window"
osascript -e '
tell application "Terminal"
tell window 1 # this just renames the first window
set custom title to "Main window"
end tell
do script # this opens a new window
tell window 1
set custom title to "Text window"
set selected to true # my first idea to put focus on this window
activate # my second idea to put focus on this window
end tell
end tell
'
printf "%s\n" "$var2print" # prints in main window, despite all efforts
read -sn 1 -p "Press any key to continue..."
Surprisingly to me, the very last command 'read' also takes place in the main window, but the focus is on the text window and I have to manually select the main window to press a key and end the script.
I have considered letting go of AppleScript and using the gnu-screen command instead, but it seems like overkill for my purpose to simply have some info displayed for a while.
Any help to better understand what's going on and to find a practical solution to switch between terminal windows would be greatly appreciated. W.
You can toggle two windows in Terminal.app with AppleScript this way
tell application "Terminal"
set index of window 2 to 1
end tell
window 1 is always the frontmost window
How about using a dialog box to display your message and go away automagically after a few seconds like this:
#!/bin/bash
bashvar="ZippedyDooDah"
osascript >/dev/null 2>&1 <<EOF
tell application "System Events" to display alert "$bashvar" buttons {"OK"} as warning default button "OK" giving up after 5
EOF
You can do it like this:
osascript -e 'tell app "Terminal" to do script "echo hello"'
Or, you can set a bash variable like this and send that for display:
MSG="FreddyFrog"
osascript<<EOF
tell app "Terminal"
do script "echo $MSG"
end tell
EOF
If you want to send it more than one message, you could make it tell you its tty as the command you pass, then you will have that in a file and you can send it further messages...
osascript -e 'tell app "Terminal" to do script "tty > tty.txt"'
If you now look in the file tty.txt you will see the device special file of the terminal window you created, some thing like /dev/ttys002, then you can do this from your original window
echo "Some stuff" > /dev/ttys002
echo "More stuff" > /dev/ttys002
or, more succinctly
echo hello > $(cat tty.txt)

Invoke copy & paste commands from terminal

Is is possible to invoke a copy command (as if the user pressed Cmd+C) from a bash script? Basically I want to write a simple script that I run with a global hotkey and it should take the current selection from the active app, replace something and paste the result. Is this possible?
The best I could come up so far is using pbpaste and pbcopy, but I'd like to automate that if possible.
If you're just trying to modify a text selection, you could use AppleScript.
osascript -e 'try
set old to the clipboard
end try
try
delay 0.3
tell application "System Events" to keystroke "c" using command down
delay 0.2
set text item delimiters to linefeed
set input to (paragraphs of (the clipboard as text)) as text
set the clipboard to do shell script "shopt -u xpg_echo; echo -n " & quoted form of input & " | rev" without altering line endings
tell application "System Events" to keystroke "v" using command down
delay 0.05
end try
try
set the clipboard to old
end try'
The first delay is for releasing modifier keys if the script is run with a shortcut that has other modifier keys than command. The second delay could also be reduced to something like 0.05, but long selections or for example web views often need a longer delay. Without the third delay, the clipboard would sometimes be set to old before the text would get pasted.
the clipboard as text and do shell script convert line endings to carriage returns by default. shopt -u xpg_echo is needed because the echo in sh interprets backslashes inside single quotes by default. If the input is longer than getconf ARG_MAX bytes, you can't use echo and have to either write it to a temporary file or use pbpaste.
pbpaste and pbcopy replace non-ASCII characters with question marks by default in the environment used by do shell script You can prevent that by setting LC_CTYPE to UTF-8.
Telling System Events to click menu bar items would often be even slower, and it wouldn't work in applications that don't have a menu bar or in full screen windows.
Another option would be to create an Automator service. But they also have small delays before they are run. There's a bug where the shortcuts for services don't always work until the services menu has been shown once on the menu bar. And the services aren't available when the frontmost application doesn't have a menu bar or a services menu.

How can I make my Applescript 'do' a script in my custom window?

I'm working on writing an Applescript that gets my Terminal ready for me to make Firefox add-ons.
tell application "Terminal"
do script "cd Public/addon-sdk-1.0"
do script "source bin/activate"
do script "clear"
end tell
When I run this script, my custom Terminal opens along with a regular Terminal window; and the bash script is ran in the regular window.. So, I'm trying to find out how to make the Applescript only open my custom Terminal, and execute the bash script in it.
The answer to your problem is to not use do script but to send keystrokes to your current terminal window with either keystroke or key code. Here's a script I use to do something similar. I just call this from the terminal with osascript myscript.scpt or launch it directly (I use LaunchBar for invoking applescripts) and it opens a new terminal tab (if the terminal is already open), gives it a custom name and then runs whatever commands I feed it. You could modify this to skip creating a new tab and just run in the current terminal window. I only use this approach when I have to do more than just run some standard terminal commands (such as send keys to an interactive python session), otherwise I just create a bash script.
global ENTER_, ESC_
set ENTER_ to 52
set ESC_ to 53
on run_commands(commands, pause)
tell application "System Events"
repeat with cmd in commands
keystroke cmd
key code ENTER_
delay pause
end repeat
end tell
end run_commands
on new_terminal_tab(tab_name)
activate application "Terminal"
delay 0.5
tell application "System Events"
# create new tab
keystroke "t" using {command down}
delay 0.5
# give it a name
keystroke "I" using {shift down, command down}
keystroke tab_name
delay 0.5
key code ESC_ # escape
end tell
end new_terminal_tab
new_terminal_tab("addon-sdk-work")
run_commands( { "cd /Users/username/Documents/dev/projname",¬
". env/bin/activate", ¬
"clear"}, 0.5)

Programmatically launch Terminal.app with a specified command (and custom colors)

I can launch an xterm from the command line (or a program, via a system call) like so:
/usr/X11/bin/xterm -fg SkyBlue -bg black -e myscript
That will launch an xterm with blue text and a black background, and run an arbitrary script inside it.
My question: How do I do the equivalent with Terminal.app?
You can open an app by bundle id too, and give other parameters.
If there's an executable script test.sh in the current directory, the following command will open and run it in Terminal.app
open -b com.apple.terminal test.sh
The only down side that I can find is that Terminal doesn't appear to inherit your current environment, so you'll have to arrange another way to pass parameters through to the script that you want to run. I guess building the script on the fly to embed the parameters would be one approach (taking into account the security implications of course...)
Assuming you already have the colors you want in one of your Terminal profiles, here's what I came up with (with some help from Juha's answer and from this Serverfault answer).
Update:
On reflection, I think this echo business is too complicated. It turns out you can use osascript to make an executable AppleScript file with a shebang line:
#!/usr/bin/osascript
on run argv
if length of argv is equal to 0
set command to ""
else
set command to item 1 of argv
end if
if length of argv is greater than 1
set profile to item 2 of argv
runWithProfile(command, profile)
else
runSimple(command)
end if
end run
on runSimple(command)
tell application "Terminal"
activate
set newTab to do script(command)
end tell
return newTab
end runSimple
on runWithProfile(command, profile)
set newTab to runSimple(command)
tell application "Terminal" to set current settings of newTab to (first settings set whose name is profile)
end runWithProfile
Save that as term.scpt, make it executable with chmod +x, and use it the same way as below, e.g. term.scpt "emacs -nw" "Red Sands".
Original answer:
Assuming we save the script below as term.sh...
#!/bin/sh
echo '
on run argv
if length of argv is equal to 0
set command to ""
else
set command to item 1 of argv
end if
if length of argv is greater than 1
set profile to item 2 of argv
runWithProfile(command, profile)
else
runSimple(command)
end if
end run
on runSimple(command)
tell application "Terminal"
activate
set newTab to do script(command)
end tell
return newTab
end runSimple
on runWithProfile(command, profile)
set newTab to runSimple(command)
tell application "Terminal" to set current settings of newTab to (first settings set whose name is profile)
end runWithProfile
' | osascript - "$#" > /dev/null
...it can be invoked as follows:
term.sh
opens a new terminal window, nothing special
term.sh COMMAND
opens a new terminal window, executing the specified command. Commands with arguments can be enclosed in quotes, e.g. term.sh "emacs -nw" to open a new terminal and run (non-windowed) emacs
term.sh COMMAND PROFILE
opens a new terminal window, executing the specified command, and sets it to the specified profile. Profiles with spaces in their names can be enclosed in quotes, e.g. term.sh "emacs -nw" "Red Sands" to open a new terminal and run (non-windowed) emacs with the Red Sands profile.
If you invoke it with a bad command name, it'll still open the window and set the profile, but you'll get bash's error message in the new window.
If you invoke it with a bad profile name, the window will still open and the command will still execute but the window will stick with the default profile and you'll get an error message (to stderr wherever you launched it) along the lines of
525:601: execution error: Terminal got an error: Can’t get settings set 1 whose name = "elvis". Invalid index. (-1719)
The invocation is slightly hacky, and could probably be improved if I took the time to learn getopt (e.g., something like term.sh -p profile -e command would be better and would, for instance, allow you to easily open a new terminal in the specified profile without invoking a command). And I also wouldn't be surprised if there are ways to screw it up with complex quoting. But it works for most purposes.
Almost all (every?) osx program can be launched from command line using:
appName.app/Contents/MacOS/command
For terminal the command is:
/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
You can use the autocomplete (tab) or ls to find the correct filenames. ".app" is basically a folder.
To change the colors and run a script... I think you cannot do it with shell scripts as Terminal does not accept arguments ("Terminal myScript.sh" does not launch myScript). With iTerm this works.
Workaround is to use applescript (wrapped in a shell script):
#!/bin/sh
osascript -e '
tell application "Terminal"
activate
tell window 1
do script "sleep 5; exit"
set background color to {0, 11111, 11111}
set win_id to id
end tell
set w_ids to (id of every window)
repeat while w_ids contains win_id
delay 1
set w_ids to (id of every window)
end repeat
end tell'
Ok, now it should behave exactly the same as the xterm example. The drawback is the constant polling of the window ids (which is bad programming).
edit: A bit more elegant applescript would use the 'busy' property of Terminal. I will leave the original code as is works for a general program (not just terminal).
tell application "Terminal"
tell window 1
do script "sleep 2"
set background color to {0, 11111, 11111}
repeat while busy
delay 1
end repeat
close
end tell
end tell
Also for perfectly correct program, one should check that whether the terminal is running or not. It affects the number of windows opened. So, this should be run first (again a nasty looking hack, that I will edit later as I find a working solution).
tell application "System Events"
if (count (processes whose name is "Terminal")) is 0 then
tell application "Terminal"
tell window 1
close
end tell
end tell
end if
end tell
br,
Juha
You can also go into terminal GUI, completely configure the options to your heart's content, and export them to a ".terminal" file, and/or group the configurations into a Window Group and export that to a terminal file "myGroup.terminal". Then
open myGroup.terminal
will open the terminal(s) at once, with all your settings and startup commands as configured.
you can launch terminal with the following command, not sure how to specify colors:
open /Applications/Utilities/Terminal.app/
The answer from #david-moles above works but run the terminal and command in ~ rather the current working directory where term was launched. This variation adds a cd command.
#!/usr/bin/env bash
# based on answer by #david-moles in
# https://stackoverflow.com/questions/4404242/programmatically-launch-terminal-app-with-a-specified-command-and-custom-colors
echo "
on run argv
if length of argv is equal to 0
set command to \"\"
else
set command to item 1 of argv
end if
set command to \"cd '"$PWD"' ;\" & command
if length of argv is greater than 1
set profile to item 2 of argv
runWithProfile(command, profile)
else
runSimple(command)
end if
end run
on runSimple(command)
tell application \"Terminal\"
activate
set newTab to do script(command)
end tell
return newTab
end runSimple
on runWithProfile(command, profile)
set newTab to runSimple(command)
tell application \"Terminal\" to set current settings of newTab to (first settings set whose name is profile)
end runWithProfile
" | osascript - "$#" > /dev/null
There may be a way to set PWD this with applescript.
Note: When I use this, I sometimes two Terminal windows, one a shell running in ~ and a second which runs the cd command and command from argv[1]. Seems to happen if Terminal is not already running; perhaps it is opening old state (even tho I had no open terminals when I closed it).

Resources