Jump to content
Sanaa

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

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?

 

Share this post


Link to post

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.

Share this post


Link to post

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?

Share this post


Link to post

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

Share this post


Link to post

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}"

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...