0

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?

Adam Stewart
  • 2,001
  • 1
  • 16
  • 16
  • 1
    Scripting the user interface only targets the frontmost application, so you might try activating the Terminal first. – red_menace May 22 '19 at 18:58
  • And there's no way to script a background application? I don't want the Terminal to steal focus every time I add a mark. – Adam Stewart May 23 '19 at 19:31
  • 1
    The `keystroke` command only goes to the front most app! There is no other way with basic vanilla AppleScript to simulate a keystroke to a background app, maybe there is a way with AppleScriptObjC to make the "mark", but that is beyond my knowledge if there is. – user3439894 May 23 '19 at 21:06
  • Cmd+U is only a shortcut to insert a mark. Is there a way to navigate the Edit->Marks->Mark menu to insert a mark without using the `keystroke` command? – Adam Stewart May 23 '19 at 21:24
  • This looks promising: https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AutomatetheUserInterface.html – Adam Stewart May 23 '19 at 21:27
  • 1
    _Any_ GUI scripting (including menus), require the target application to be frontmost. The target application doesn't neccessarily need to be frontmost if it has the desired functionality via terminology in its scripting dictionary, but **Terminal** doesn't expose the mark feature. – red_menace May 23 '19 at 23:25
  • For the record, I did manage to do this without using keystroke: `osascript -e 'tell application "System Events" to tell process "Terminal" to click menu item "Mark" of menu "Marks" of menu item "Marks" of menu "Edit" of menu bar item "Edit" of menu bar 1'`. However, as you noted, it still doesn't work when the Terminal is in the background. – Adam Stewart May 24 '19 at 01:08

1 Answers1

1

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'
wch1zpink
  • 3,026
  • 1
  • 8
  • 19
  • This does indeed work, but I'm looking for a solution that doesn't steal focus from the current application, sorry for not making that clear. – Adam Stewart May 23 '19 at 19:32
  • When using `osascript` to bring Terminal frontmost, all one needs is: `osascript -e 'activate app "Terminal"'` - Which is 38 characters that does the same thing as the 115 characters in the code of this answer. In a 118 characters, `osascript -e 'activate app "Terminal"' -e delay 0.25 -e 'tell app "System Events" to keystroke "u" using command down'` does the complete process in 3 more characters than the code of this answer. Granted the OP whats it done in the background but `keystrokes` go to the frontmost app. – user3439894 May 28 '19 at 15:06