Search code examples
macosautomatorosx-elcapitanosx-services

OSX Services - character substitution + for _


I've created a service to use primarily in a browser for searching IMDB, so instead of copying and pasting the film's name I just select the text and run the service I created which will automatically search IMDB for the selected text. This works perfect.

enter image description here

I was hoping to do the same for Wikipedia and so i've been trying to modify the same service but have come across a problem. The search query places a "+" between each word.

enter image description here

This doesn't work for Wikipedia. They place underscores between their words.

enter image description here

I wondered how I can substitute the + for _? This is what I currently have

open "https://en.wikipedia.org/wiki/$(ruby -rcgi -e 'print CGI.escape $<.read.chomp')"

Kinds regards and Merry Christmas :)


Solution

  • You can substitute every space for underscore with the gsub() command before the escape command.

    open "https://en.wikipedia.org/wiki/$(ruby -rcgi -e 'print CGI.escape $<.read.chomp.gsub(" ", "_")')"