Jump to content

[SOLVED] Workflow to append current browser URL to another URL


Recommended Posts

This would make for workflow that anyone could reuse for various use cases (similar to the default Alfred Web Search)

 

Workflow Logic:

1) Copy front browser url (automatically)
2) Append copied url to predetermined url
3) Open appended url into current browser window

 

Use Cases:

Annotate Current Webpage

https://via.hypothes.is/{frontmost-browser-url}

WaybackMachine Search

https://web.archive.org/web/*/{frontmost-browser-url}

 

 

 

Link to post
  • vitor changed the title to [SOLVED] Workflow to append current browser URL to another URL
On 10/7/2020 at 5:57 AM, vitor said:

There you go. It uses the Wayback Machine URL but you can add more.

 

This is great, thanks @vitor! How might one edit the AppleScript to strip the URL to just the domain + extension (i.e. alfredapp.com without the protocol prefix or any path information)? 

Link to post
51 minutes ago, deanishe said:

 

Extract everything between the second and third slash.

 

Ok, well, as I hope you know, I'm not a programmer, but was able to get closer... but for some reason a '%0A' is being appended to the output. Can you see what I'm doing wrong?

 

const frontmost_app_name = Application('System Events').applicationProcesses.where({ frontmost: true }).name()[0]
const frontmost_app = Application(frontmost_app_name)

const chromium_variants = ['Google Chrome', 'Chromium', 'Opera', 'Vivaldi', 'Brave Browser', 'Microsoft Edge']
const webkit_variants = ['Safari', 'Webkit']

if (chromium_variants.some(app_name => frontmost_app_name.startsWith(app_name))) {
  var url = frontmost_app.windows[0].activeTab.url();
  var urlParts = url.replace('http://','').replace('https://','').split(/[/?#]/);
  var domain = urlParts[0];
  query = domain;
} else if (webkit_variants.some(app_name => frontmost_app_name.startsWith(app_name))) {
  var url = frontmost_app.documents[0].url();
  var urlParts = url.replace('http://','').replace('https://','').split(/[/?#]/);
  var domain = urlParts[0];
  query = domain;
} else {
  throw new Error('You need a supported browser as your frontmost app')
}

 

Link to post
12 hours ago, heyJoeCampbell said:

Thanks @vitor one small request - how do I go to the destination in the same/current window (instead of opening a new window)

 

It already opens in the same window. Or it should; if it’s not doing it, either your browser has atypical behaviour, or you’ve configured it differently, or you have more than one instance open (in which case there’s not much we can do as AppleScript doesn’t allow us the ability to differentiate between instances of the same app). If you mean opening in the same tab, replace the code with this (only lines 8 and 10 changed) and delete the Open URL node.

 

const frontmost_app_name = Application('System Events').applicationProcesses.where({ frontmost: true }).name()[0]
const frontmost_app = Application(frontmost_app_name)

const chromium_variants = ['Google Chrome', 'Chromium', 'Opera', 'Vivaldi', 'Brave Browser', 'Microsoft Edge']
const webkit_variants = ['Safari', 'Webkit']

if (chromium_variants.some(app_name => frontmost_app_name.startsWith(app_name))) {
  frontmost_app.windows[0].activeTab.url = 'https://web.archive.org/web/*/' + frontmost_app.windows[0].activeTab.url()
} else if (webkit_variants.some(app_name => frontmost_app_name.startsWith(app_name))) {
  frontmost_app.documents[0].url = 'https://web.archive.org/web/*/' + frontmost_app.documents[0].url()
} else {
  throw new Error('You need a supported browser as your frontmost app')
}

 

6 hours ago, chris said:

How might one edit the AppleScript to strip the URL to just the domain + extension (i.e. alfredapp.com without the protocol prefix or any path information)?

 

Changes are, again, on lines 8 and 10.

 

const frontmost_app_name = Application('System Events').applicationProcesses.where({ frontmost: true }).name()[0]
const frontmost_app = Application(frontmost_app_name)

const chromium_variants = ['Google Chrome', 'Chromium', 'Opera', 'Vivaldi', 'Brave Browser', 'Microsoft Edge']
const webkit_variants = ['Safari', 'Webkit']

if (chromium_variants.some(app_name => frontmost_app_name.startsWith(app_name))) {
  frontmost_app.windows[0].activeTab.url().replace(/(.*?\/\/)(.+?)\/.*/, '$2')
} else if (webkit_variants.some(app_name => frontmost_app_name.startsWith(app_name))) {
  frontmost_app.documents[0].url().replace(/(.*?\/\/)(.+?)\/.*/, '$2')
} else {
  throw new Error('You need a supported browser as your frontmost app')
}

 

5 hours ago, chris said:

but for some reason a '%0A' is being appended to the output.

 

That’s a newline character from the output. Stick a Transform Utility with Trim Whitespace between the Run Script and the Open URL.

 

5 hours ago, chris said:

query = domain;

 

There’s no point to that. query is just a generic name Alfred uses, there’s no reason a variable with that name has to be used.

Link to post
26 minutes ago, vitor said:

That’s a newline character from the output. Stick a Transform Utility with Trim Whitespace between the Run Script and the Open URL.

 

Woohoo! Thanks!

 

Re: the `query` variable... I wasn't sure how to pass back the outcome of the `if..then` statement back to the Workflow...

Link to post
9 hours ago, chris said:

I wasn't sure how to pass back the outcome of the `if..then` statement back to the Workflow...

 

Not by storing it in a variable, but by showing its output. In your case, something like:

 

var url = frontmost_app.windows[0].activeTab.url();
var urlParts = url.replace('http://','').replace('https://','').split(/[/?#]/);
urlParts[0];

 

Or chaining everything, with no need for any variable:

 

frontmost_app.windows[0].activeTab.url().replace('http://','').replace('https://','').split(/[/?#]/)[0]

 

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