Jump to content

Comma-separated, build and open multiple URLs


Recommended Posts

I have a workflow (download it here: https://cl.ly/3C0n0e1n3W2N) that works and allows me to select some text like "12345" and use a hotkey to launch a URL of

https://example.com/issues/{query} -> https://example.com/issues/12345

However, I want to enhance it to be able to support selecting text like "12345,281928", do the hotkey, then open two browser tabs:

{query} ->
A = 12345
B = 281928

1: open https://example.com/issues/12345
2: also open https://example.com/issues/281928

last one?
then we're all done.

I know PHP well so I could do explode( ',' {query} ), but I'm not sure how to use {query} (is it usable in a PHP script?) and I'm not sure how to tell it to loop through all the items in the array.

If Alfred can't use PHP to do this, that's fine, but I'm not well versed in bash, AppleScript, and the rest.

 

Thanks for any help! :)

Link to post

Haven’t tested it much, but this should work. It’s basically "{query}".split(',').each { |v| system('open', "https://example.com/issues/#{v}") } (ruby). So split on commas and for each one just use the system’s open command, that will interpret the URL as something to open in the default web browser.

Edited by vitor
Link to post

Thank you very much. It worked!

 

However, I see there's no longer any regex logic to remove non-numeric characters.

For example:

"12345,281928" works fine

"12345, 281928" does not work (does not build a valid URL) because of the leading space in the 2nd item ("281928", not " 281928")

"12c345,281928" typo with "c" character in first item (should result in numbers only: "12345")

 

Would you be able to include that in there for me please or explain what I should add?

 

Also, does Alfred's PHP not have functionality to open the browser like your Ruby script does?

 

Thanks!

Link to post
20 minutes ago, Cliff said:

Would you be able to include that in there for me please or explain what I should add?

 

"{query}".gsub(/[^\d,]/, '').split(',').each { |v| system('open', "https://example.com/issues/#{v}") }

 

22 minutes ago, Cliff said:

Also, does Alfred's PHP not have functionality to open the browser like your Ruby script does?

 

open is a system command (it’s in /usr/bin/open); you can call it from any language. I’m just not a php user, so I don’t know how to do that one-liner without researching it.

Link to post

Well I tried this in Alfred's PHP scripting and it didn't do anything:

https://gist.github.com/cliffordp/a0ce681f51504649cd75cc9c22a53b2a (the opening PHP tag is there only to enable syntax highlighting; it's not actually added in my script)

 

I'd guess that the syntax for PHP is the same as your Ruby because of PHP's system(). I also tried exec() and passthru() but none of these 3 worked. :(

 

If someone could please advise how-to in PHP, I'd surely appreciate it just for learning's sake.

 

However, @vitor, you've been a great help. Thank you so much for that.

Link to post
1 hour ago, deanishe said:

If you look at the relevant docs, system() takes the command as a single string. You need system("open $url");

 

As a side note, in ruby one can also give system a single string, but that launches a shell to interpret the command. In the multiple arguments version, no shell is launched. That’s a good thing as ones doesn't have to worry about shell command injection and escaping.

Link to post
2 hours ago, vitor said:

and yet in php it indeed does not work

 

That is the expected behaviour, tbh. When you run a .php file, the interpreter only executes what's between <?php … ?> tags, and so GitHub highlights only these parts as PHP. Anything outside the PHP tags is passed straight through by the interpreter and is treated as plaintext by GitHub unless it detects HTML.

Link to post
17 hours ago, Cliff said:

I feel empowered! (Although I'm impressed with Ruby's brevity!)

 

You should probably think about learning Ruby or Python instead of PHP. They're both much better (sane) languages. Here's the TL;DR of PHP: a fractal of bad design:

 

Quote

I can’t even say what’s wrong with PHP, because— okay. Imagine you have uh, a toolbox. A set of tools. Looks okay, standard stuff in there.

 

You pull out a screwdriver, and you see it’s one of those weird tri-headed things. Okay, well, that’s not very useful to you, but you guess it comes in handy sometimes.

 

You pull out the hammer, but to your dismay, it has the claw part on both sides. Still serviceable though, I mean, you can hit nails with the middle of the head holding it sideways.

 

You pull out the pliers, but they don’t have those serrated surfaces; it’s flat and smooth. That’s less useful, but it still turns bolts well enough, so whatever.

 

And on you go. Everything in the box is kind of weird and quirky, but maybe not enough to make it completely worthless. And there’s no clear problem with the set as a whole; it still has all the tools.

 

Now imagine you meet millions of carpenters using this toolbox who tell you “well hey what’s the problem with these tools? They’re all I’ve ever used and they work fine!” And the carpenters show you the houses they’ve built, where every room is a pentagon and the roof is upside-down. And you knock on the front door and it just collapses inwards and they all yell at you for breaking their door.

 

That’s what’s wrong with PHP.

 

Edited by deanishe
paragraphs
Link to post

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...