0

I am using Ruby on a mac to open a dozen URLs one at a time with Nokogiri etc.

For each URL I need to let my ruby program know whether to keep the URL window for further inspection or close it.

But I cannot see the terminal window and its prompt, it is hidden behind the last URL window.

I have to click on the terminal window in order to bring it to the front, in order to enter my decision on the keyboard.

puts "close webpage?"
if gets =~ /^y/i then 1 ; else; 0; end;

I would like the terminal window to come to the front before it prompts me for an answer.

I think the question is two fold

  1. Is there a terminal command that tells a terminal window to become the active one (the one in the front) that would work with mac iTerm.

The Apple script "bringiTermtofront" works in the applescript editor.

tell application "iTerm" to activate
  1. Is there a way to execute a terminal command from ruby. the ruby code

    system "osascript bringiTermtofront.scpt"

brings the iTerm to the front.

Anne G
  • 1
  • 2
  • Welcome to Stack Overflow! Please see "[ask]" and the linked pages and "[mcve](https://stackoverflow.com/help/minimal-reproducible-example)". We'd like to know what things you tried to open a terminal window or to execute a command. Without that information it looks like you haven't tried and want us to locate or write tutorials for you, which is off-topic. – the Tin Man Jul 26 '19 at 19:00

1 Answers1

0

For Question 1, one approach would be to write an Applescript to handle the switch, and then use the Terminal command osascript to run it from your Ruby code. You could also check if rb-appscript is still usable (it's no longer supported, but might work).

For Question 2, you have a few choices. Using backticks around the command will let you capture the output of a brief command, if you want to store the result of the command in a variable. (E.g. use grep or something similar).

The system method in Kernel is probably your best choice, though, as it will execute shell commands as if at the terminal.

Per the edit showing what script you're using, you need to execute the script as Applescript, not as a terminal script. You don't even need a separate file as it's just one line. This would be :

command = %q[osascript -e "tell application \"iTerm\" to activate"]
system(command)

You could also put the Applescript in a file and execute it using just

system("osascript bringiTermtofront")

See "Running shell commands from Ruby" for a little more help on how to interact with these methods.

  • Explain how to use `grep` if you think that's a valid option to store a result in a variable since `grep` is a command in the OS and a method in Ruby. – the Tin Man Jul 26 '19 at 18:57
  • I merely meant it as an example that gives results that **could** be stored in a variable, as opposed to a terminal command where capturing the output wouldn't be as useful. – Michael Winterstein Jul 26 '19 at 19:08
  • For more control over the I/O to external commands from Ruby see the [open3](https://ruby-doc.org/stdlib-2.6.3/libdoc/open3/rdoc/index.html) documentation. It's very powerful. – the Tin Man Jul 26 '19 at 20:16