Jump to content
Sign in to follow this  
MuppetGate

Executing system commands from Ruby

Recommended Posts

Hello again.  :)

 

I wonder if someone could help out with a bit of Ruby scripting. I've written a workflow that deletes a list of files selected from Alfred. The problem is with the script; I can't get it to delete the files.

 


 

ext = "{query}"
 
fileNames = text.split("/t")
 
filesDeleted = ""
 
fileNames.each do  |fileName| 
 
    command = "rm -r " + fileName
 
    system(command)
 
    filesDeleted << fileName
 
end
 
print filesDeleted

 
Everything else works okay apart from the system call. 
 
Any ideas as to what I've forgotten? ...  :(
 

Share this post


Link to post

Hello again.  :)

 

I wonder if someone could help out with a bit of Ruby scripting. I've written a workflow that deletes a list of files selected from Alfred. The problem is with the script; I can't get it to delete the files.

 


 

ext = "{query}"
 
fileNames = text.split("/t")
 
filesDeleted = ""
 
fileNames.each do  |fileName| 
 
    command = "rm -r " + fileName
 
    system(command)
 
    filesDeleted << fileName
 
end
 
print filesDeleted

 
Everything else works okay apart from the system call. 
 
Any ideas as to what I've forgotten? ...  :(

 

Have you tried commenting out the system command and just using puts or print to get the exact output of command? Make sure its creating the string correctly? Also, in the command string where you are creating 'rm -r '+filename, you might ought to modify it so that the output of fileName will be in quotes when the command is run so it will still function correctly if the path has a space in it (unless you already have it escaped)

Share this post


Link to post

Hello again.  :)

 

I wonder if someone could help out with a bit of Ruby scripting. I've written a workflow that deletes a list of files selected from Alfred. The problem is with the script; I can't get it to delete the files.

 


 

ext = "{query}"
 
fileNames = text.split("/t")
 
filesDeleted = ""
 
fileNames.each do  |fileName| 
 
    command = "rm -r " + fileName
 
    system(command)
 
    filesDeleted << fileName
 
end
 
print filesDeleted

 
Everything else works okay apart from the system call. 
 
Any ideas as to what I've forgotten? ...  :(

 

You can use the back ticks to run system commands.

 

`rm -r "#{fileName}"`

 

Your problem might be spaces in filenames. Hence the double quotes.

Share this post


Link to post

Deja vu.  :)

 

The fixes it.

 

fileNames = text.split("    ")

 

And I would have surely tripped over the 'spaces in filenames' thing within the next hour. Thanks for the suggestions!

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.

Sign in to follow this  
×
×
  • Create New...