How do I decode a URL using KBM?

I am writing up a macro to extract a youtube URL from a google search URL by decoding it and then using regular expressions on the decoded url. However, I have not been able to deduce how to decode a URL using KBM and store to a variable.
How do I go about it? Thanks in advance.

I eventually got an applescript that can decode a URL on the clipboard and copy the result back to the clipboard

set theSampleURL to the clipboard
set theText to theSampleURL
set the clipboard to urldecode(theText) of me
on urldecode(theText)
  set sDst to ""
  set sHex to "0123456789ABCDEF"
  set i to 1
  repeat while i ≤ length of theText
    set c to character i of theText
    if c = "+" then
      set sDst to sDst & " "
    else if c = "%" then
      if i > ((length of theText) - 2) then
        display dialog ("Invalid URL Encoded string - missing hex char") buttons {"Crap..."} with icon stop
        return ""
      end if
      set iCVal1 to (offset of (character (i + 1) of theText) in sHex) - 1
      set iCVal2 to (offset of (character (i + 2) of theText) in sHex) - 1
      if iCVal1 = -1 or iCVal2 = -1 then
        display dialog ("Invalid URL Encoded string - not 2 hex chars after % sign") buttons {"Crap..."} with icon stop
        return ""
      end if
      set sDst to sDst & (ASCII character (iCVal1 * 16 + iCVal2))
      set i to i + 2
    else
      set sDst to sDst & c
    end if
    set i to i + 1
  end repeat
  return sDst
end urldecode

Hey Mirizzi,

This is yet another reason I swear by the Satimage.osax AppleScript Extension.

One line unescape-url.
Regular Expression support organically added to AppleScript.

set _url to "https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&sqi=2&ved=0CB0QtwIwAA&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DqBNmY7S0BG8&ei=4DxYVdSUHsmuUe7SgKAE&usg=AFQjCNEgN5MbWpylUTE_RpKsxqBZ9jnOnQ&sig2=1y6Qn53VIdPZ8jvSNjIY6Q&bvm=bv.93564037,d.d24"

set newURL to unescapeURL _url

set newURL to change "^https?://(?:www\\.)google\\.com/.+?url=(https?://(?:www\\.)youtube.com/.+)" into "\\1" in newURL with regexp without case sensitive

--> "http://www.youtube.com/watch?v=qBNmY7S0BG8&ei=4DxYVdSUHsmuUe7SgKAE&usg=AFQjCNEgN5MbWpylUTE_RpKsxqBZ9jnOnQ&sig2=1y6Qn53VIdPZ8jvSNjIY6Q&bvm=bv.93564037,d.d24"

You can send the result of an Execute AppleScript action to a variable directly.

Or you can do it from within an AppleScript:

tell application "Keyboard Maestro Engine"
  try
    set value of variable myVar to newURL
  on error
    make new variable with properties {name:"myVar", value:newURL}
  end try
end tell

-Chris

On Mon, May 18, 2015 at 1:38 PM, ccstone kmforum@forum.keyboardmaestro.com wrote:

This is yet another reason I swear by the Satimage.osax
http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html
AppleScript Extension.

One line unescape-url.

Where has this been all my life? So many scripts I need to rewrite (aka
simplify) now...

c

Chris Lott chris@chrislott.org

Hey Chris,

I've been using it since 2003. :sunglasses:

I used the very first regex osax back around '95-96.

-Chris

Here’s how to unescape a URL using Perl:

#! /usr/bin/perl 
use v5.010; use strict; use warnings; use URI::Escape;
print uri_unescape($ENV{KMVAR_myURL});

KMVAR_myURL gets your variable myURL into the script.

Vanilla Python is more complicated:

#!/usr/bin/python
import re, os
def unquote(url):
	return re.compile('%([0-9a-fA-F]{2})',re.M).sub(lambda m: chr(int(m.group(1),16)), url)
print unquote(os.environ['KMVAR_myURL']);

Although you can import a library and make it a simple one-liner.

Here’s Ruby:

#!/usr/bin/ruby
require 'uri'
puts URI.unescape(ENV['KMVAR_myURL']);

I reckon Peter will add a decode URL text filter in KM7 though.

-Chris

Thanks. It worked beautifully and shrank the code size.

And don't forget that Yosemite JavaScript for Applications (JXA) already provides a decodeURI() function which can be called from KeyBoard Maestro in various ways, for example in an Execute Shell Script action:

osascript -l JavaScript  -e "decodeURI(\"$KMVAR_rawURL\")"

1 Like

Or perhaps more simply just:

osascript -l JavaScript  -e "decodeURI('$(pbpaste)')"

or

osascript -l JavaScript  -e "decodeURI('$(pbpaste)')" | pbcopy

As in:

1 Like

I'll add it, but the reason I don't have it is because it doesn't really make sense to unencode a whole URL - after you have unencoded it you're left with something that no longer has any defined shape. You really need to break it apart first, and then unencode the bits if necessary.

I'll add it anyway so you can use it in cases like the above and so you can use it on the parts afterwards.

Been trying this but I can't get it to work.

It uses Javascript for Applications, so it needs Yosemite.

Here is a macro which toggles the clipboard encoded ⇄ decoded:

toggle Clipboard encoding.kmmacros (5.8 KB)

An unencoded URL has a lot more shape than an encoded one — to me anyway.    :smile:

-Chris

Perhaps the difference between the two Javascript functions:

provides a useful distinction ?

( both already there in JXA JavaScript for Automation )