Add mark to Terminal in background using AppleScript - terminal

The macOS Terminal has a nice "mark" feature that allows you to jump between prompts using Cmd+Up/Down. I'm trying to insert my own "marks" from inside of a Python script so that I can jump to specific parts of the output (example).
Thanks to Armin Briegel, I have:
osascript -e 'tell app "System Events" to keystroke "u" using command down'
This works, but has a few problems. It doesn't add a "mark" if the Terminal is not in focus. Also, it triggers the Terminal bell alert if the Terminal is not in focus. Any way to improve this?

Use this to bring Terminal into focus first.
osascript -e 'tell application "System Events" to tell its application process "Terminal" to set frontmost to true'

Related

How to execute a command in full screen mac terminal using Apple Script?

I am trying to automate this using apple script. Let me list down the steps below;
Open a new Mac Terminal window
Make it full-screen
echo "hello world" or sh /some/script/file.sh
I am trying the script below;
tell application "Terminal"
do script " "
activate
end tell
tell application "System Events"
keystroke "k" using {command down}
keystroke "f" using {command down, control down}
end tell
Basically, this script will open a new terminal window and make it full-screen. I am stuck at step 3, which is simply echoing some message, or executing a shell script.
Could somebody please explain how to achieve this?
Thank you.

Trying to automate tabs and shell commands in Terminal?

I have a custom vim setup running inside split (GNU) screen sessions running in several tabs inside Terminal. Naturally I want to automate all that. So Ive Googled a lot and most answers talk about using osascript -e to run a bunch of AppleScript commands. My situation is slightly different: first Im using TotalTerminal, a plugin for Terminal (dont think it matters but mention it just in case) and Im writing a hashbang script and not a bash script, i.e.
#!/usr/bin/osascript
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
tell application "Terminal" to activate
tell application "Terminal" to do script with command "cd ~/Desktop/Projects && screen -d -U -R -A"
which Im running from the command-line. The tab opening works but the script/command runs in a new window instead of inside a newly-created tab.
This is how I might recommend setting things up:
#!/usr/bin/osascript
tell application "System Events"
tell process "Terminal"
set frontmost to true
end tell
end tell
tell application "Terminal"
activate
tell application "System Events" to keystroke "t" using command down
do script "cd ~/Desktop/Projects && screen -d -U -R -A" in window frontmost
do script "clear; echo 'Hello, World!'" in tab 1 of window frontmost
end tell
Note: You also can select the tab you want the next command to go into by using tab x. If you switch back to the first tab you should notice the echo sent to it after creating the new tab.
The example above is a few more lines of code perhaps, although it should get all the processes correctly in order. I think the key ingredient is having Terminal set frontmost to true which gets the current Terminal window to start interacting with the rest of the script.
EDIT: The OP came back and needed to make a few changes and this was the end result:
#!/usr/bin/osascript
tell application "System Events"
tell process "Terminal"
set frontmost to true
end tell
end tell
tell application "Terminal"
activate
do script "mosh user#someserver" in window frontmost
tell application "System Events" to keystroke "t" using command down
do script "cd ~/Desktop/Projects && screen -d -U -R -A" in tab 2 of window frontmost
end tell

OSX Terminal close current tab from command line without prompt?

Looking for a way to close the current tab via the command line, I hashed this out, but end up getting a prompt for Do you really want to close which I would like to avoid. Here's my code,
osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "w" using command down'
Why didn't use the:
Terminal.app -> Preferences -> Settings -> Shell
and for the items:
"When the shell exists:" Close if the shell exited cleanly
"Prompt before closing:" Only if there are...." (or Never)

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

Bash Script on a Mac creates a info popup

Is there a way in bash on a mac to draw a pretty info box that displays a simple message like "please save all files to /Users/......"
You can run fragments of applescript from you bash scripts.
A simple popup would look like this:
#!/bin/bash
/usr/bin/osascript <<-EOF
tell application "System Events"
activate
display dialog "Hello world"
end tell
EOF
This will feed the applescript between the EOF tags to osascript and execute it
(resulting in a Hello World popup).
An alternative to osascript "System Events" would be to install cocoaDialog.
cocoaDialog has the disadvantage that it must be installed, but it seems to be much more flexible than the "System Events".
The license is GPL, so you can freely redistribute it, since it is a separate application.
(osascript was littering my terminal with error messages (at least under Lion) and with return values, it didn't let me do popups with timeouts, and seemed to require specific quoting which made it hard to use variables in the texts.)

Resources