Jump to content

Need a bit of a help with a simple bash script workflow


Recommended Posts

I'm trying to turn a frequently used Automator system service into an Alfred workflow. (It's a simple system service that uses curl to make an HTTP request from the RingCentral API, to place a phone call.)

 

I understand the basics of bash scripting but I had help in creating this workflow and I'm not sure how to re-do the script for Alfred.

 

Here's what the service looks like in Automator:

 

BnDEsFS.png

 

Here are the two scripts it runs. They're both pretty simple. I'll blank out the personal data in the latter one.

# This removes any characters except numbers from your selection
sed 's/[^0-9+]//g'

and

for f in "$@"
do
	curl -silent -o ~/.ringout.html "https://service.ringcentral.com/ringout.asp?cmd=call&username=XXXXXXXXXX&ext=X&password=XXXXXXXXXX&from=XXXXXXXXXX&to=${f}&clid=XXXXXXXXXX&prompt=1"

done

exit 0

As you can see the idea is to take the text (in this case highlighted text using OS X Services) and strip out all non-numeric characters, then plug it into that curl one-liner that ties into the RingCentral API to place a phone call.

 

Ideally I'd like to be able to call this with a keyword in Alfred so that I could either highlight text, call the keyword, and have it make a phone call to a phone number in the highlighted text, or type the number in as an argument if there's no text highlighted.

 

Could somebody help get me started? I looked over the documentation but I'm kind of running in circles.

 

Thanks!

Link to comment

Create a workflow. Add a Hotkey to trigger it, assign the keyboard shortcut and select "Pass through to workflow" as the Action and "Selection in OS X" as the Argument.
 
Then add a Run Script action with Languages set to "/bin/bash" and Escaping set to "Backquotes", "Double Quotes", "Dollars" and "Backslashes".
 
In the Script box paste:

# This removes any characters except numbers from your selection
number=$(echo "{query}" | sed 's/[^0-9+]//g')
curl -silent -o ~/.ringout.html "https://service.ringcentral.com/ringout.asp?cmd=call&username=XXXXXXXXXX&ext=X&password=XXXXXXXXXX&from=XXXXXXXXXX&to=${number}&clid=XXXXXXXXXX&prompt=1"

Finally, connect your Hotkey to the Run Script Action.

That should do the same as your Automator workflow.

Edited by deanishe
Link to comment

Oops, I updated my workflow with your "fixed" script and it stopped working  :( Foolishly, I didn't duplicate it before changing it.

 

Only the first line seems to have changed, and I can't figure out what the problem is.

 

What did you have up there before you changed it just now, so I can compare?

 

Edit: 

 

 

I think it's a problem with the query containing spaces. When I try this with a phone number formatted like this:

 

(123) 456-7890

 

I get this in the log:

[ERROR: alfred.workflow.action.script] Code 0: /bin/bash: (123): command not found

So it's getting tripped up on the space between the area code and the number.

Edited by raygan
Link to comment

:( I messed up the correction (which I've now corrected).

 

First line should read:

 

number=$(echo "{query}" | sed 's/[^0-9+]//g')

I accidentally deleted the ^ from the sed command. The ^ means "not", which is why it went wrong (i.e. it was removing numbers and + instead of everything that wasn't a number or +).

Edited by deanishe
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...