Tell an AppleScript to Restart Itself - applescript

I have an AppleScript saved as an application. When first run, it asks the user if they want to move it to the Applications folder. What I would like to be able to do is, after it's been moved, have the script quit itself and then reopen.
Obviously I can't say
tell me to quit
tell me to activate
...because it would stop running after the quit command.
Any suggestions?

Just run the script from inside the script, and make sure to terminate the current running of it with a return (can skip the actual return command if it's the last line of the script
-- do stuff
display dialog "Here I am again"
-- set alias to the script
-- run the script
set myScript to path to me
run script myScript
-- end current iteration
return
You can break out of this script by canceling the dialog, but you'll probably want to set a condition to check whether to run the script again.

Here's how I'd do this. Basically, You check if the application is running from the Applications folder. If it isn't, move it there, open another instance, and quit. Seems to work flawlessly. The activate in the beginning is because it seems that the application doesn't always move itself to the foreground:
--incase the application doesn't do this automagically
activate
set my_path to POSIX path of (path to me)
if my_path does not start with "/Applications/" then
set new_path to "/Applications/" & quoted form of (my name & ".app")
--"mv" wont move the application into the new location if it exists
try
do shell script "rm -rf " & new_path
end try
do shell script "mv -f " & quoted form of my_path & " " & new_path
do shell script "open -n " & new_path & " &> /dev/null &"
quit
end if

What I am doing.
First I enabled at by running the following command in Terminal (this only has to be done once)
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist
then I have the following script
display dialog "running"
set mypath to POSIX path of (path to me)
set lun to open for access POSIX file "/tmp/springboard" with write permission
write "open " & mypath & linefeed to lun
close access lun
do shell script "at -f /tmp/springboard +1 minute"
quit

Related

How to ensure Applescript dialog focus in OS X 10.10 (Yosemite)?

Before OS X 10.10 (Yosemite), I could ensure an applescript dialog had focus by telling the "current application" to activate:
tell current application
activate
set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges)
end tell
This would bring the password dialog to the front to type in. The dialog no longer appears with focus in Yosemite and you have to click on it before you can type.
Searching on stack exchange and elsewhere hasn't provided any solutions/workarounds.
Does anyone have a way that works in OS X 10.10?
UPDATE 1: In case anyone else runs into this problem, it appears to be a new sandboxing issue. A non-password dialog box gets focus properly. In fact, a non-password dialog before the "with administrator privileges" password dialog will cause the password dialog to also have focus. I added a dialog to confirm Safari was still the front app while testing and discovered this. As a workaround, I have added a preceding dialog with timeout of 1 (second) until a better solution can be found.
tell me -- alternate method: tell current application
activate
display dialog "Trying to get focus…" with title "Can't focus in Yosemite" buttons {"Cancel", "Idle"} cancel button "Cancel" giving up after (1)
set output to (do shell script "printf '" & hostsLine & commentString & "' >> /etc/hosts" with administrator privileges)
end tell
Interestingly, BBedit's Authenticated Save helper script (for the MAS version of the app) uses a "with administrator privileges" password dialog. But it gets focus properly in Yosemite. Something different between a native app call to applescript vs. an Applescript tell command for an app to do so?
on documentShouldFinalizeAuthenticatedSave(theDocument, tempFilePath, destinationPath)
-- on input: tempFilePath points to the contents of the document written to a temp file, ready to move to the destination; destinationPath is where the file should be copied.
-- on exit: if the operation succeeded, delete the temp file (or else the application will assume the operation failed) and return YES for success
-- this is pretty straightforward: "cp tmpFilePath destinationPath"
do shell script "cp" & " " & quoted form of tempFilePath & " " & quoted form of destinationPath with administrator privileges
-- now remove the temp file, this indicates to the application that we did the work
do shell script "rm" & " " & quoted form of tempFilePath
return true
end documentShouldFinalizeAuthenticatedSave
UPDATE 2: If anyone is curious, this part of a Safari helper script for my wife to block spammy popup ad sites like mackeeper.zeobit.com that have modal dialog boxes. Editing /etc/hosts was a little too complicated/daunting and she wanted a turnkey solution. I can post the whole script if there's interest. It works fine under 10.9, but the focus for password issue is annoying in 10.10.
You could try... Tell me instead of Tell current application. "Tell me" basically tells the applescript to activate and run the "do shell script" command. This makes more sense. "do shell script" is an applescript command so it makes sense to ask applescript to run it. Maybe this will help with your problem.
Good luck.
Personally, I would remove the need to enter a password. The simplest way to do this is to append the following line to the /private/etc/sudoers file.
wife_user_name ALL=(ALL) NOPASSWD:ALL
To remove the need for a password for all Administrator accounts, change the following line in the /private/etc/sudoers file from
%admin ALL=(ALL) ALL
to
%admin ALL=(ALL) NOPASSWD: ALL
Next, change your applescript line from
set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges)
to
set output to (do shell script "sudo printf '" & hostsLine & commentString & "' >> /private/etc/hosts")
Changes to the /private/etc/sudoers file can be accomplished by using the Terminal and TextEdit applications. Open the Terminal application and type the following commands:
cd ~/desktop
sudo cp -n /etc/sudoers /etc/sudoers.orignal
sudo cp /etc/sudoers sudoers.txt
sudo chmod ug+w sudoers.txt
open sudoers.txt
visudo -c -f sudoers.txt
sudo cp -X sudoers.txt /etc/sudoers
When done, the sudoers.txt file on your desktop can be put in the trash.
To undo your changes, use the command:
sudo cp /etc/sudoers.original /etc/sudoers
This was tested using OS X 10.10.1
Below is a brief explanation of what each command does:
cd ~/desktop
This makes sure you are working from your desktop folder.
sudo cp -n /etc/sudoers /etc/sudoers.original
This backups your sudoers file. The backup can be used to undo your changes. The -n option insures that an existing sudoers.original file will not be overwritten.
sudo cp /etc/sudoers sudoers.txt
Copies the sudoers file to your desktop. The .txt extension is added so OS X will know this is a text file.
sudo chmod ug+w sudoers.txt
Changes the file’s permissions to allow write access.
open sudoers.txt
Opens the file in the TextEdit application. You need to edit the file and save the changes.
visudo -c -f sudoers.txt
Checks the edited file for syntax errors. The output should be sudoers.txt: parsed OK.
sudo cp -X sudoers.txt /etc/sudoers
Copies the file back to the /etc directory.
I stumbled upon this post trying to get the password dialog to get focus in an Alfred script that I am working on. I finally got it to work reliably by adding tell me to activate prior to my shell command. The resultant script was:
tell me to activate
do shell script "<command>" with administrator privileges
Hope this works for you.
2015-01-08 Edit
Mike, I understand your question as: "I have an AppleScript with a do shell script that asks for an admin password, but I have to click on the password dialog before I can type in it. How can I ensure the dialog has focus, saving me the nuisance of clicking it?"
My response to you is: "What if your AppleScript never asked for a password in the first place? Wouldn't you be happier?"
If so, then here's a solution. That question demonstrates how you can bypass the dialog by supplying a password parameter along with the with administrator privileges parameter.
Ok, Mike this is my second try at an answer.
Instead of using
tell current application
activate
set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges)
end tell
as you proposed at the start of your above question, try using
tell current application
activate
tell me to set output to (doShellScript for "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administratorPrivileges)
end tell
where the doShellScript handler would be
on doShellScript for command as text given administratorPrivileges:adminPriv as boolean : false
if adminPriv then
set message to (name of current application) & " wants to make changes. Type your password to allow this."
set again to ""
set pw to ""
set icn to 1
repeat
set fullCommand to "printf '%s\\n' " & (quoted form of pw) & " | sudo -p '' -S " & command
try
return do shell script fullCommand
on error eStr number eNum partial result rList from badObj to expectedType
set errorMessage to "Sorry, try again." & return & return & "sudo: 1 incorrect password attempt"
if eStr ≠ errorMessage or eNum ≠ 1 then
error eStr number eNum partial result rList from badObj to expectedType
end if
end try
set actual to again & message
set values to display dialog actual default answer "" with icon icn with hidden answer
set again to "Sorry, incorrect passord. Try again." & linefeed
set pw to text returned of values
set icn to 2
end repeat
end if
return do shell script command
end doShellScript
An example dialog is shown below.
Your application name and icon will be different. The main thing is that the focus will be on the password text box when the dialog pops up.
dave.
I had a similar issue for which I had an applescript execute a continuous ping in Terminal, and then a dialog box would pop up in order for user to click OK to kill that ping and start a different one. The issue was similar as the dialog box kept showing behind the Terminal window. As I would have to execute this script numerous times in an urgent situation with very limited time, this was very annoying. I was finally able to resolve it by telling System Events to wait 1 second after it starts the continuous ping, and then keystroke CMD+Tab to switch back to the running script before opening the dialog box. The CMD+Tab initially only intermittently resolved it until I added the delay 1.
Example of issue:
tell application "Terminal" to do script pingCommand
set userDialog to display dialog "Client Connection Test Completed" buttons {"Ping Server", "Cancel"}
With above code, the dialog would always come behind the Terminal window.
Example of resolution:
tell application "Terminal" to do script pingCommand
delay 1
tell application "System Events" to key code 48 using {command down}
set userDialog to display dialog "Client Connection Test Completed" buttons {"Ping Server", "Cancel"}
I was facing the same problem, and created a solution that would bring my applet's dialog to the front.
I wrote a function called ActivateMe() that replaces the "activate" or "tell me to activate" commands that would proceed the "display dialog" command.
This function gets the current process name, and then executes the following AppleScript commands via a shell script.
delay 0.1
tell application "System Events"
set frontmost of process \"" & processName & "\" to true
end tell"
Here's the function. Enjoy.
on ActivateMe()
set myPath to path to me as text
if myPath ends with ":" then
set n to -2
else
set n to -1
end if
set AppleScript's text item delimiters to ":"
set processName to text item n of myPath
if (processName contains ".") then
set AppleScript's text item delimiters to "."
set processName to text 1 thru text item -2 of processName
end if
set AppleScript's text item delimiters to ""
set a to "delay 0.1"
set b to "tell application \"System Events\""
set c to "set frontmost of process \"" & processName & "\" to true"
set d to "end tell"
do shell script "osascript -e '" & a & "' -e '" & b & "' -e '" & c & "' -e '" & d & "'"
end ActivateMe
Appears to be a bug in 10.10.x most likely due to sandboxing. Fixed in 10.11 (El Cap) as the unmodified script gets focus from the activate command once again like it did in 10.9 and earlier versions.

End Service if already running on Mac

I'm trying to create an Automator service that allows me to speak selected text.
I want to be able to use a keyboard shortcut, however, I also want to use a keyboard shortcut to end the service before it finishes.
I cannot figure out how to make the service stop once started running.
Here is my applescript code in Automator:
on run {input, parameters}
say input using "Alex" speaking rate 400
return input
end run
I know you can speak text in system prefs. But it maxes out at 300 wpm. I need this to do more than 300. Hence the Automator service.
Thanks for your help.
Another way should be to start saying nothing.
say "" stopping current speech true
If executed it will stop the current say output.
It's possible by getting the pid of the service, just write the pid to a temporary file.
When you want to stop the speaking, the script get the pid in the temporary file to quit this process ID.
on run {input, parameters}
set tFile to POSIX path of (path to temporary items as text) & "__auto_Runner_Speak_text__last_Pid__.txt"
try
set lastPid to word 1 of (read tFile) -- get the pid of the last speak text
if lastPid is not "" then
set tPids to words of (do shell script "/bin/ps -Axcro pid,command | sed -n '/Automator Runner/s/^ \\([0-9]*\\).*/\\1/p'")
if lastPid is in tPids then
do shell script "echo '' > " & (quoted form of tFile) & ";/bin/kill " & lastPid & " > /dev/null 2>&1 &" -- empty the file and stop the speaking
return -- quit this service
end if
end if
end try
do shell script "echo $PPID > " & quoted form of tFile -- write the PID of this workflow to the temp file
say input using "Alex" speaking rate 400
do shell script "echo '' > " & quoted form of tFile -- remove the PID of this workflow in the temp file
end run
Basically you'll have to kill the speech synthesis process...
try
set thePID to word 1 of (do shell script "/bin/ps -Axcro pid,command | grep speechsynthesis")
do shell script "kill -15 " & thePID
end try

Applescript Execute Shell with Input and Admin Privileges

I'm trying to write an automator service to fire up virtualhost.sh in a terminal.
Using the Services context menu the dialog opens to ask for the name of virtual host, then runs an applescript to launch terminal and pass in the input text.
What I want is to pass in my username and password to admin privileges so that I don't need to pass it in the terminal with sudo.
This can be done with do shell script but that executes a bin/sh and the virtualhost.sh is a bash script so I get the error bin/sh: virtualhost.sh command not found
Alternately I can use do script with command but this doesn't allow me to pass in the user name and password.
My code looks like so:
on run {input, parameters}
set vhost to "virtualhost.sh " & input
tell application "Terminal"
activate
do shell script vhost user name "user" password "pass" with
administrator privileges
end tell
end run
This produces the bin/sh error previously mentioned.
With do script with command
on run {input, parameters}
set vhost to "virtualhost.sh " & input
tell application "Terminal"
activate
do script with command vhost user name "user" password "pass" with
administrator privileges
end tell
end run
This produces an escaping error: Expected end of line, etc. but found property.
Is there a way to do this correctly?
Not specifically familiar with AppleScript Studio, but you can do it in plain old AppleScript (which appears to have the same issue) if you provide a full path to virtualhost.sh. (Also, Terminal is not required with "do shell script".) Example:
set vhost to "/usr/local/bin/virtualhost.sh " & input
do shell script vhost user name "user" password "pass" ¬
with administrator privileges
You can also extend $PATH (which is by default /usr/bin:/bin:/usr/sbin:/sbin with "do shell script") to include the path to virtualhost.sh, e.g.:
set vhost to "{ PATH=$PATH:/usr/local/bin; virtualhost.sh " & input & "; }"
do shell script vhost user name "user" password "pass" ¬
with administrator privileges
If you want a relative path, you can put virtualhost.sh inside the script application or bundle (e.g. in Contents/Resources), either in Terminal or by control-clicking and choosing "Show Package Contents". Then use "path to me":
set vhostPath to "'" & POSIX path of (path to me) & ¬
"/Contents/Resources/virtualhost.sh" & "'"
set vhost to vhostPath & space & input
do shell script vhost user name "user" password "pass" ¬
with administrator privileges
Per the comment on my other answer, I'm posting a secondary answer more in the spirit of that there's a will, there's a way, but it's a different, more dangerous approach. However, it's the only solution I can think of to this particular requirement (to get the interactivity of Terminal, but without having to prompt for an administrator password while running a script as administrator).
This solution runs Terminal as root, which is what do shell script "command" with administrator privileges does. This is dangerous because you have an open Terminal window with root access, so carefully weigh benefits against potential consequences of, say, opening a new Terminal window and being at the Bash prompt as root.
For this reason, the Terminal instance that is opened is killed upon completion of the script; if the "kill" command is removed, be aware you'll get multiple instances of Terminal, rather than multiple windows within the same instance.
No idea if this works in AppleScript Studio (it works in AppleScript Editor), but I can't think of any reason why it wouldn't.
set input to "some_input"
set vhost to "/usr/local/bin/virtualhost.sh " & input
set kill to ¬
"terminal_pid=$(</tmp/terminal_pid); rm /tmp/terminal_pid; kill $terminal_pid"
-- launch Terminal as root, and save its process ID in /tmp/terminal_pid
tell application "Finder" to set beforeProcesses to processes
do shell script ¬
"/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal " & ¬
"&> /dev/null & echo $! > /tmp/terminal_pid" user name "user" password ¬
"pass" with administrator privileges
-- wait until the new Terminal is confirmed to be running
tell application "Finder"
repeat while (processes is equal to beforeProcesses)
do shell script "sleep 0.5"
end repeat
end tell
-- Perform script in root Terminal window that we just opened,
-- and kill Terminal when done to prevent open root prompt
-- and multiple processes.
tell application "Terminal"
activate
do script vhost & "; " & kill
end tell
-- optional: wait until Terminal is gone before continuing
do shell script "while [[ ( -f /tmp/terminal_pid ) " & ¬
"&& ( \"$(ps -p $(</tmp/terminal_pid) -o%cpu='')\" ) ]]; do sleep 0.5; done"

How do you open a terminal with a specific path already cd'ed to?

How do I use the terminal to open another terminal window but with a path I specify?
I am using automator to load my work stuff when I get to work, but I need to know how to do this:
Open Terminal and Type:
• cd Work/Company/Project/
• script/server
And then new tab in that terminal window and cd to the same folder.
This opens a new terminal window from a command prompt on Mac OSX , executes "cd /" and then keeps the window on top:
osascript -e 'tell application "terminal"' -e 'do script "cd /"' -e 'end tell'
You can put this into a script like this:
#!/bin/sh
osascript -e 'tell application "terminal"' -e "do script \"cd $1\"" -e 'end tell'
Hope this helps.
Use an applescript to do this.
e.g. Open Terminal Here
You can write a shell script to cd to that directory
So write a script that executes something like cd /user/music or something like that, save it as myscript.sh and run it using chmod +x myscript.sh.
This resource from the OS X developer network is pretty helpful
The two scripts below together handle the common scenarios:
1) If Terminal is already running, open a new terminal window and run the 'cd mydir' there
2) If terminal is not already running, use the initial window that Terminal spawns (window 0), rather than annoyingly launching a second window
NOTE: what's not quite perfect is if Terminal has several windows open, all of them will be brought to the front, overlapping any other apps. A solution to raising only the last terminal window to the front appears to require the black magic of AppleScriptObjC - references below:
https://apple.stackexchange.com/questions/39204/script-to-raise-a-single-window-to-the-front
http://tom.scogland.com/blog/2013/06/08/mac-raise-window-by-title/
Script 1 - open a text editor and save as:
/usr/local/bin/terminal-here.sh
#!/bin/sh
osascript `dirname $0`/terminal-here.scpt $1 > /dev/null 2> /dev/null
Script 2 - open 'AppleScript Editor', paste contents below and save as:
/usr/local/bin/terminal-here.scpt
# AppleScript to cd (change directory) to a path passed as an argument
# If Terminal.app is running, the script will open a new window and cd to the path
# If Terminal.app is NOT running, we'll use the window that Terminal opens automatically on launch
# Run script with passed arguments (if any)
on run argv
if (count of argv) > 0 then
# There was an argument passed so consider it to be the path
set mypath to item 1 of argv
else
# Since no argument was passed, default to the home directory
set mypath to "~"
end if
tell application "System Events"
if (count (processes whose bundle identifier is "com.apple.Terminal")) is 0 then
# Terminal isn't running so we'll make sure to run the 'cd' in Terminal's first window (0)
tell application "/Applications/Utilities/Terminal.app"
# Turn off echo, run the 'cd', clear screen, empty the scrollback, re-enable echo
do script "stty -echo; cd " & (mypath as text) & ";clear; printf \"\\e[3J\"; stty echo" in window 0
activate last window
end tell
else
# Terminal is already running so we'll let it open a new window for our 'cd' command
tell application "/Applications/Utilities/Terminal.app"
# Turn off echo, run the 'cd', clear screen, empty the scrollback, re-enable echo
do script "stty -echo; cd " & (mypath as text) & ";clear; printf \"\\e[3J\"; stty echo"
activate last window
end tell
end if
end tell
end run

Running a command in a new Mac OS X Terminal window

I've been trying to figure out how to run a bash command in a new Max OS X Terminal.app window. As, an example, here's how I would run my command in a new bash process:
bash -c "my command here"
But this reuses the existing terminal window instead of creating a new one. I want something like:
Terminal.app -c "my command here"
But of course this doesn't work. I am aware of the "open -a Terminal.app" command, but I don't see how to forward arguments to the terminal, or even if I did what arguments to use.
one way I can think to do it off the top of my head is to create a .command file and run it like so:
echo echo hello > sayhi.command; chmod +x sayhi.command; open sayhi.command
or use applescript:
osascript -e 'tell application "Terminal" to do script "echo hello"'
although you'll either have to escape a lot of double quotes or not be able to use single quotes
Partial solution:
Put the things you want done in a shell-script, like so
#!/bin/bash
ls
echo "yey!"
And don't forget to 'chmod +x file' to make it executable. Then you can
open -a Terminal.app scriptfile
and it will run in a new window. Add 'bash' at the end of the script to keep the new session from exiting. (Although you might have to figure out how to load the users rc-files and stuff..)
I've been trying to do this for a while. Here is a script that changes to the same working directory, runs the command, and closes the terminal window.
#!/bin/sh
osascript <<END
tell application "Terminal"
do script "cd \"`pwd`\";$1;exit"
end tell
END
In case anyone cares, here's an equivalent for iTerm:
#!/bin/sh
osascript <<END
tell application "iTerm"
tell the first terminal
launch session "Default Session"
tell the last session
write text "cd \"`pwd`\";$1;exit"
end tell
end tell
end tell
END
Here's yet another take on it (also using AppleScript):
function newincmd() {
declare args
# escape single & double quotes
args="${#//\'/\'}"
args="${args//\"/\\\"}"
printf "%s" "${args}" | /usr/bin/pbcopy
#printf "%q" "${args}" | /usr/bin/pbcopy
/usr/bin/open -a Terminal
/usr/bin/osascript -e 'tell application "Terminal" to do script with command "/usr/bin/clear; eval \"$(/usr/bin/pbpaste)\""'
return 0
}
newincmd ls
newincmd echo "hello \" world"
newincmd echo $'hello \' world'
see: codesnippets.joyent.com/posts/show/1516
Here's my awesome script, it creates a new terminal window if needed and switches to the directory Finder is in if Finder is frontmost. It has all the machinery you need to run commands.
on run
-- Figure out if we want to do the cd (doIt)
-- Figure out what the path is and quote it (myPath)
try
tell application "Finder" to set doIt to frontmost
set myPath to finder_path()
if myPath is equal to "" then
set doIt to false
else
set myPath to quote_for_bash(myPath)
end if
on error
set doIt to false
end try
-- Figure out if we need to open a window
-- If Terminal was not running, one will be opened automatically
tell application "System Events" to set isRunning to (exists process "Terminal")
tell application "Terminal"
-- Open a new window
if isRunning then do script ""
activate
-- cd to the path
if doIt then
-- We need to delay, terminal ignores the second do script otherwise
delay 0.3
do script " cd " & myPath in front window
end if
end tell
end run
on finder_path()
try
tell application "Finder" to set the source_folder to (folder of the front window) as alias
set thePath to (POSIX path of the source_folder as string)
on error -- no open folder windows
set thePath to ""
end try
return thePath
end finder_path
-- This simply quotes all occurrences of ' and puts the whole thing between 's
on quote_for_bash(theString)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "'"
set the parsedList to every text item of theString
set AppleScript's text item delimiters to "'\\''"
set theString to the parsedList as string
set AppleScript's text item delimiters to oldDelims
return "'" & theString & "'"
end quote_for_bash
I made a function version of Oscar's answer, this one also copies the environment and changes to the appropriate directory
function new_window {
TMP_FILE=$(mktemp "/tmp/command.XXXXXX")
echo "#!/usr/bin/env bash" > $TMP_FILE
# Copy over environment (including functions), but filter out readonly stuff
set | grep -v "\(BASH_VERSINFO\|EUID\|PPID\|SHELLOPTS\|UID\)" >> $TMP_FILE
# Copy over exported envrionment
export -p >> $TMP_FILE
# Change to directory
echo "cd $(pwd)" >> $TMP_FILE
# Copy over target command line
echo "$#" >> $TMP_FILE
chmod +x "$TMP_FILE"
open -b com.apple.terminal "$TMP_FILE"
sleep .1 # Wait for terminal to start
rm "$TMP_FILE"
}
You can use it like this:
new_window my command here
or
new_window ssh example.com
A colleague asked me how to open A LOT of ssh sessions at once. I used cobbal's answer to write this script:
tmpdir=$( mktemp -d )
trap '$DEBUG rm -rf $tmpdir ' EXIT
index=1
{
cat <<COMMANDS
ssh user1#host1
ssh user2#host2
COMMANDS
} | while read command
do
COMMAND_FILE=$tmpdir/$index.command
index=$(( index + 1 ))
echo $command > $COMMAND_FILE
chmod +x $COMMAND_FILE
open $COMMAND_FILE
done
sleep 60
By updating the list of commands ( they don't have to be ssh invocations ), you will get an additional open window for every command executed. The sleep 60 at the end is there to keep the .command files around while they are being executed. Otherwise, the shell completes too quickly, executing the trap to delete the temp directory ( created by mktemp ) before the launched sessions have an opportunity to read the files.
Another option that needs to be here is "ttab"
https://www.npmjs.com/package/ttab
Install
npm install ttab -g
Usage : Open another tab and run ls
ttab ls
Usage 2 : Open new tab, with Green BG, with title "hi", in directory ~/dev, and run code .
ttab -s Grass -t hi -d '~/dev' code .
I call this script trun. I suggest putting it in a directory in your executable path. Make sure it is executable like this:
chmod +x ~/bin/trun
Then you can run commands in a new window by just adding trun before them, like this:
trun tail -f /var/log/system.log
Here's the script. It does some fancy things like pass your arguments, change the title bar, clear the screen to remove shell startup clutter, remove its file when its done. By using a unique file for each new window it can be used to create many windows at the same time.
#!/bin/bash
# make this file executable with chmod +x trun
# create a unique file in /tmp
trun_cmd=`mktemp`
# make it cd back to where we are now
echo "cd `pwd`" >$trun_cmd
# make the title bar contain the command being run
echo 'echo -n -e "\033]0;'$*'\007"' >>$trun_cmd
# clear window
echo clear >>$trun_cmd
# the shell command to execute
echo $* >>$trun_cmd
# make the command remove itself
echo rm $trun_cmd >>$trun_cmd
# make the file executable
chmod +x $trun_cmd
# open it in Terminal to run it in a new Terminal window
open -b com.apple.terminal $trun_cmd
You could also invoke the new command feature of Terminal by pressing the Shift + ⌘ + N key combination. The command you put into the box will be run in a new Terminal window.

Resources