Is there a command for macOSX terminal(CMD) to pause a script, open a NEW terminal, and resume? - macos

I am trying to find a method to combine two scripts so that they can be run in one go. However, it would be useful if both scripts still ran in separate windows as I need to look through the logs for certain bits of information. Any help would be much appreciated!

If you have X11 installed (e.g. XQuartz), you can run your scripts in new xterm windows:
xterm -e ./scriptA.sh & xterm -e ./scriptB.sh &
But if you want to use the default OSX Terminal.app, this should work:
open -a Terminal.app ./scriptA.sh; open -a Terminal.app ./scriptB.sh
I tried passing arguments to the scripts using the --args parameter to open, but for some reason it didn't work for me.
Update
The more OSX idiomatic way to do this is seems to be to start Terminal.app from an Applescript snippet with the osascript command:
osascript -e "tell application \"Terminal\" to do script \"scriptA.sh <args>\""
osascript -e "tell application \"Terminal\" to do script \"scriptB.sh <args>\""
The quotes are escaped so you can e.g. do variable expansions from the calling bash for the script args.

Related

Launch Emacsclient with GUI (from Dock) on Mac OS X

How can I launch Emacsclient with GUI from the Dock (or maybe also from the terminal) on Mac OS X?
The EmacsWiki describes how to create an "Emacs from Dock" app using Automator. It worked for me but I don't want to launch Emacs but Emacsclient. So, I tried replacing /Applications/Emacs.app/Contents/MacOS/Emacs with both /Applications/Emacs.app/Contents/MacOS/bin/emacsclient and /Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c but both didn't work.
From the terminal
You can find the appropriate path to emacsclient using type in your shell (assuming emacsclient -c works from said shell):
$ type emacsclient
emacsclient is /usr/local/bin/emacsclient
Then we can add the appropriate emacsclient flags (see $ man emacsclient for details) to open the GUI:
/usr/local/bin/emacsclient -n -c -a ""
From macOS GUI
To launch emacsclient from eg the Dock or Spotlight, it's easy to use Automator. Automator is built in to macOS.
Choose to make an "Application", then choose "Run Shell Script", and add a modified version of the above call to emacsclient:
/usr/local/bin/emacsclient -n -c -a "" -- "$#"
Then change "Pass input": use "as arguments" instead of "to stdin".
The added "$#" is where any optional arguments passed to this shell script will be placed. Here, this allows you to pass a filename to open with emacsclient. Automator automates passing this filename in when, eg, you click to open a file with the application we've just made. This also allows you to set the application to be the default application for certain file types.
From anywhere, flexibly
Another way to run the above shell command is with skhd (link). skhd is far more involved to learn, but ultimately makes it much easier to set up a large number of shell commands with rapid access.
For example, you could make "Ctrl-o" from anywhere in macOS enter a mode you name open_app, from which you could press "e" to open emacsclient, "d" to open emacs --debug-init, "t" to run emacs --adv-timers, "f" to open Firefox, "F" to open a second Firefox profile, etc.
One idea would be to create an applescript that does whatever the original poster desires, and wrap it up in an application using something like platypus or automator. See https://superuser.com/questions/685111/basic-setup-of-emacs-server-under-osx for additional ideas such as using the --daemon command-line argument instead of placing (server-start) within the user-configuration file.
Here is an example applescript:
# (server-start) must be inside `init.el` or `.emacs` file.
#
# This script can also be used in the terimal: osascript path-to-script arguments
# Terminal Example:
# osascript /absolute/path/to/applescript/file "-e '(progn (dired \"/Applications\") (message \"Hello-World\!\"))'"
on run argv
set arg to item 1 of argv
set emacs to application "Emacs"
set appIsRunning to emacs is running
if appIsRunning then
say "Emacs is already running."
do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient " & arg
else
tell application "/Applications/Emacs.app/Contents/MacOS/Emacs" to activate
say "Please wait five seconds for Emacs to load."
delay 5
do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient " & arg
end if
end run

How to create a shell script to launch 3 terminals and execute a set of commands in each?

Currently, I open 3 terminals ('openocd session','telnet session' & 'gdb session') and execute 'a set of commands' in each terminal every time I flash my image on target.
Is is possible to put this in one shell script file and run at once? (Commands on each terminal are NOT dependent on others; except that the terminals should be opened in the order mentioned above)
Thanks in advance !
Which terminal are you using?
A terminal like xterm can start a program instead of a shell. Simply run
xterm -e 'command args ...'
I use this to start my email client:
xterm -e mutt
If you use a different terminal, consult its manual page. I'm pretty sure there's an equivalent for -e.

AppleScript: execution error -10810 when launching certain applications from shebang'ed scripts

I'm running OS X 10.10.2. I'm facing a weird issue where AppleScript won't launch applications from shebang'ed scripts while working fine everywhere else (Script Editor, piping to osascript, etc.). Specifically, consider the following example script named launch-app:
#!/usr/bin/osascript
launch application "TextEdit"
When TextEdit is not running and I do
./launch-app
I get
./launch-app:0:29: execution error: An error of type -10810 has occurred. (-10810)
When I do
<launch-app osascript
Well, it works just fine; which means the following Bash script will also work:
#!/usr/bin/env bash
osascript <<EOF
launch application "TextEdit"
EOF
Really weird. (By the way, a tell ... activate ... end tell block results in the same error. I'm using launch here just to keep to example minimal.)
I have some old scripts that involve activating an application (well, practically all my old scripts involve tell ... activate ... end tell) that definitely worked in the past. I can't tell when things began to fall apart because when I run those scripts, most often the applications to activate are already launched. I have the impression that the issue dates back at least to 10.10.1.
I have looked at several related posts here on SO, e.g., this one, but they don't help. I also tried to understand error -10810 by reading articles like this one, but my problem definitely doesn't look like a filled process table (otherwise why does directly calling osascript works while running osascript from a shebang doesn't?).
Update: The bug has been fixed in OSX 10.10.3.
Just to provide a state-of-the-union post:
The behavior observed is a bug in OSX 10.10 still unresolved as of OSX 10.10.2 (as of 10 Mar 2015):
Anyone interested in getting this fixed should file their own bug at http://bugreport.apple.com.
It applies to executable scripts that are directly or indirectly passed to osascript - whether:
explicitly (osascript launch-app)
or implicitly, via the shebang line, by direct invocation (./launch-app)
The specific form of the shebang line is irrelevant (whether #!/usr/bin/osascript or #!/usr/bin/env osascript or #!/usr/bin/env osascript -l JavaScript or ...), what matters is whether the file has the executable bit (permission) set (e.g., via chmod +x).
Workarounds:
As suggested by the OP, feed the file to osascript via stdin: osascript < launch-app
This has side effects; for instance, name of me will report msng instead of the name of the script.
Remove the executable bit from the script and invoke it explicitly with osascript:
chmod -x launch-app # a one-time operation
osascript launch-app # with the executable bit unset, this should work
Looking at the man page for osascript, when you send lines of applescript code you should put the "-e" option infront of each separate line.
So here's what I tested. I made a bash script with the -e option...
#!/bin/bash
osascript -e 'launch application "TextEdit"'
And one without.
#!/bin/bash
osascript 'launch application "TextEdit"'
The one without the -e option does not run. As such I think this could be a cause of your problem... there's no -e option in your code.
Note that I tested your code too and got the same error as you. There's a command line utility "/usr/bin/macerror" and I entered your error code into that. Here's the result.
Unknown error (-10810) at /usr/bin/macerror5.18 line 40, <DATA> line 1.
Good luck.
There is no need for using osascript to launch applications. There is a built in command line utility named open, that will open your app from the terminal commandline, or a shebanged script. For doucumentation, enter "man open" in a terminal window. It is a really nifty utility, with a lot of options. :)
The open utility, will lauch applications that are not running, but I also wonder out of curiosity: have you tried "tell application appname to run", or just "tell application appname to activate"?
The osascript below, works for me, on 10.9
#!/usr/bin/osascript
tell application "TextEdit" to launch
I guess you'll have to commmand the app to do something, and not just try to "launch" it. Maybe "tell me to launch application appname also works".
Edit
I prefer to use open -b "com.apple.textedit", because then I also get the front window of textEdit, brought to front.
By the way, with the open -e command, you can open a textfile directly into TextEdit from the commandline. open is not totally as good as the plumb utility of plan-9, but it is really nifty.

Automate Everyday Tasks in Mac Applications with Aliases in the ~/.bash_profile

I know that you can create aliases in the ~/.bash_profile that automate bash commands like:
alias fly="ssh username#ip_address -p22"
But I was wondering if it was possible to automate tasks within applications on the Mac. So, for example, I know that you can make an alias to open the System Preferences:
alias sys="open /Applications/System\ Preferences.app/"
But how do you automate navigating within the System Preferences app? For example, I find myself opening the System Preferences, typing 'network', and then pressing enter frequently to navigate to the Network settings.
Is there a way to automate these series of steps with an alias? Or are aliases limited to bash commands?
Shell aliases are limited to shell commands. But many OS X applications can be controlled by AppleScript commands, and those can be issued with the shell command osascript. Quoting gets a little tricky, though, because your AppleScript commands often contain quotes, which then need to be wrapped in another layer of quotes in the shell command, which then need to be wrapped in another layer of quotes when you define the alias.
See macosxautomation.com for notes on using AppleScript (and this page for System Preferences specifically). To get System Preferences to show the Network pane, you could use this AppleScript:
tell application "System Preferences"
reveal pane id "com.apple.preference.network"
activate
end tell
...which can be issued with this osascript command (note that each line of the script corresponds to a -e argument, and the AppleScript double-quotes are wrapped in single-quotes for the shell):
osascript -e 'tell application "System Preferences"' -e 'reveal pane id "com.apple.preference.network"' -e 'activate' -e 'end tell'
And you could create an alias for this:
alias networkprefs="osascript -e 'tell application \"System Preferences\"' -e 'reveal pane id \"com.apple.preference.network\"' -e 'activate' -e 'end tell'"
Note that I had to escape the double-quotes that're intended for AppleScript consumption... rather confusing. I'd use a shell function instead (similar to an alias, but without the quoting weirdness):
networkprefs() {
osascript -e 'tell application "System Preferences"' -e 'reveal pane id "com.apple.preference.network"' -e 'activate' -e 'end tell'
}

open programs with applescript

2 part question:
I'm simply trying to run programs using applescript from the terminal, so I tried:
$ osascript tell application "iTunes" to activate
and get the error:
osascript: tell: No such file or directory
Giving the full path to the program did not work either. What am I missing? The second part of the question is what I eventually want to use applescript for. I would like to use it to open an application I built using py2app. Can applescript open any mac app or just certain ones that are already compatible.
Thanks
Try this. Notice you use "-e" when you are writing the command. Without "-e" you would give a path to an applescript to run. Also notice the string command must be in quotes.
osascript -e "tell application \"iTunes\" to activate"
And if you have a multi-line applescript you use "-e" before each line like this...
osascript -e "tell application \"iTunes\"" -e "activate" -e "end tell"
If you want to open an application just use the unix "open" command...
open "/path/to/application"
If you wanted to open an application using applescript and the "activate" command doesn't work (it should work for almost everything though) then tell the Finder to open it. Remember that applescript uses colon delimited paths...
osascript -e "tell application \"Finder\" to open file \"path:to:application\""
In a bash shell (like in Terminal), you can send multiple lines to osascript by using a "here document".
osascript -e "tell application \"iTunes\"" -e "activate" -e "end tell"
becomes
osascript <<EOF
tell application "iTunes"
activate
end tell
EOF
As an old-skool Unix hacker, I save these little snippets in my $HOME/bin directory and call them from the command line. Still learning the particulars, though.
Alan
an alternative to osascript:
open -a Calendar
close by:
pkill Calendar
Try:
do shell script "open /Applications/iTunes.app"
you need to put single quotes around the tell:
osascript -e 'tell app "iTunes" to activate'
otherwise you're defining a variable when you run -e
I'am new to script too.
I am confused to so I scan an essay named AppleScript Language Guide
and when I go through script commands items, I learn that if you want to activate an application in mac os with applescript editor you should type beneath code in your editor and then compile and run them! may this answer will help you, here's code:
// applescript editor code
----------
activate application "iTunes" line 1
----------
tell application "iTunes" to activate line 2

Resources