Jump to content

How to run Ruby (.rb) files without Terminal window?


Sanaa

Recommended Posts

Hello,

 

I am using fairly often Ruby (.rb) files to automate things, and currently they're associated with Terminal, and basically I am typing the start of their name in Alfred, and Alfred is launching them inside a Terminal window. I am wondering if Alfred can launch them in the background, without showing a Terminal window?

 

Link to comment

Hello,

 

I am using fairly often Ruby (.rb) files to automate things, and currently they're associated with Terminal, and basically I am typing the start of their name in Alfred, and Alfred is launching them inside a Terminal window. I am wondering if Alfred can launch them in the background, without showing a Terminal window?

 

To do this with a workflow, instead of using the terminal command workflow action, try using the Run Script.  This will allow you to run bash, ruby, perl, python, php, etc scripts in the background without opening a terminal window.

Link to comment

To do this with a workflow

 

Thanks, but given I have 30+ Ruby files, I cannot really afford to encapsulate each one of them in an individual Alfred workflow. Also, in case I ever rename or delete one of those Ruby files, I would have to rename or delete the corresponding Alfred workflow. It would be a nightmare. I was hoping there is some way to just type the filename of the script, and make Alfred run it in the background. Is this possible? If not, the only solution I see is to write a generic Alfred workflow called, say, "rb" and to type "rb <filename>". But I have no experience writing workflows (and the <filename> would have to be auto-completed, which looks complicated to do). Does such workflow exist already maybe?

Link to comment

Thanks, but given I have 30+ Ruby files, I cannot really afford to encapsulate each one of them in an individual Alfred workflow. Also, in case I ever rename or delete one of those Ruby files, I would have to rename or delete the corresponding Alfred workflow. It would be a nightmare. I was hoping there is some way to just type the filename of the script, and make Alfred run it in the background. Is this possible? If not, the only solution I see is to write a generic Alfred workflow called, say, "rb" and to type "rb <filename>". But I have no experience writing workflows (and the <filename> would have to be auto-completed, which looks complicated to do). Does such workflow exist already maybe?

 

It should be pretty easy to create a workflow to find you ruby files (with auto-complete) and then open them in the background. If you could fill in your Powerpack email address in your forum profile (only visible to me and Vero), somebody can help you out with creating this :)

 

Cheers,

Andrew

Link to comment

You could do something like this. I haven't checked this code, so it might be a bit buggy, but you should be able to port the logic to another language if you want.

 

Right now the script wouldn't take arguments if you need them, but you can modify the code below to do so. So, create a new workflow with an input that's a script filter with php as the language. Make the argument required "with space" then put in an action run script with the language as bash. Connect the script filter to the run script action. Copy/paste the contents below. Change the $dirs array to the directories that the scripts are located.

 

You're done.

 

You might have to add some escaping to the code.

 

Input: Script Filter (php)

// Download from http://dferg.us/workflows-class/
// Put a copy in the workflow folder
require_once('workflows.php');

// Instantiate workflow object
$w = new Workflows();

// A list of the directories where the scripts are
$dirs = array('/path/to/dir1' , '/path/to/dir2' , '/path/to/dir3');


$tmp = array();
$scripts = array(); 

// We want to ignore these files
$ignore = array('.','..','.DS_Store');

// Cycle through the contents of each directory to put together a list of scripts
foreach ($dirs as $dir) {
	$tmp = scandir($dir);
	foreach ($tmp as $file) {
		if (! in_array($ignore, $file) {
			$info = pathinfo("$dir/$file");
			if ($info['extension'] == 'rb' ) {
				$scripts[$info['filename']] = "$dir/$file";
			}
		}
		unset($info);
	}
	unset($tmp);
}

foreach ($scripts as $name => $script) {
	$w->result = ( $name , $script , "Run $name" , $script , 'fileicon:/Applications/Alfred.app', 'yes' , $name );
}

echo $w->toxml();

Action: Run Script (bash)

rb "{query}"
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...