Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to see if there is a tool or script available that I can use to modify clipboard content on the fly.

Often when I'm writing code, I'd like to take a string of text and make it a slug. Instead of re-typing the content into slug format, it would be awesome if it were changed into a slug when copying it.

For example: "BBQ with Friends" → "bbq-with-friends"

Know of any apps or Applescripts that can help with this? Much appreciated.

share|improve this question

3 Answers 3

I've combined the scripts from here and here to create one script that will slugify whatever is in the clipboard.

set theclip to the clipboard contents
on normalize(the_string)
    set p_script to ¬
        "# -*- coding: utf-8 -*-
import unicodedata, sys

def normalize(x):
    normal_form_1 = 'NFKD'
    normal_form_2 = 'NFC'
    x = unicodedata.normalize(normal_form_2, x)
    x = x.lower()
    x = x.replace(u'ß', u'ss')
    x = x.replace(u'å', u'aa')
    x = unicodedata.normalize(normal_form_1, x)
    x = u''.join([c for c in x if not unicodedata.combining(c)])
    x = x.encode('utf-8')
    return x
arg = sys.argv[1].decode('utf-8')
x = normalize(arg)
print x"

    set p_script to quoted form of p_script
    set the_string to quoted form of the_string

    return (do shell script ("python -c " & p_script & " " & the_string))
end normalize
on change_case(this_text)
    set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    set the source_string to "abcdefghijklmnopqrstuvwxyz"
    set the new_text to ""
    repeat with this_char in this_text
        set x to the offset of this_char in the comparison_string
        if x is not 0 then
            set the new_text to (the new_text & character x of the source_string) as string
        else
            set the new_text to (the new_text & this_char) as string
        end if
    end repeat
    return the new_text
end change_case
on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

set theresult to normalize(theclip)
set theresult to replace_chars(theresult, " ", "-")
set theresult to change_case(theresult)

edit 1 If you want to slugify the string for use as a URL, add this to the end of that script:

set theresult to replace_chars(theresult, "%", "%25")
set theresult to replace_chars(theresult, "<", "%3C")
set theresult to replace_chars(theresult, ">", "%3E")
set theresult to replace_chars(theresult, "#", "%23")
set theresult to replace_chars(theresult, "{", "%7B")
set theresult to replace_chars(theresult, "}", "%7D")
set theresult to replace_chars(theresult, "|", "%7C")
set theresult to replace_chars(theresult, "\\", "%5C")
set theresult to replace_chars(theresult, "^", "%5E")
set theresult to replace_chars(theresult, "~", "%7E")
set theresult to replace_chars(theresult, "[", "%5B")
set theresult to replace_chars(theresult, "]", "%5D")
set theresult to replace_chars(theresult, "`", "%60")
set theresult to replace_chars(theresult, ";", "%3B")
set theresult to replace_chars(theresult, "/", "%2F")
set theresult to replace_chars(theresult, "?", "%3F")
set theresult to replace_chars(theresult, ":", "%3A")
set theresult to replace_chars(theresult, "@", "%40")
set theresult to replace_chars(theresult, "=", "%3D")
set theresult to replace_chars(theresult, "&", "%26")
set theresult to replace_chars(theresult, "$", "%24")
share|improve this answer

I use functions like this:

f() {
  (. ~/Scripts/f; "$@")
}

ff() {
  pbpaste | f "$@" | pbcopy
}

(I use the functions in ~/Scripts/f from emacs and from OS X text views more often though.)

~/Scripts/f could contain a function like this:

slug() {
  LC_ALL=C tr A-Z a-z | LC_ALL=C tr -cs a-z0-9 - | sed 's/^-//;s/-$//'
}

tr -c is complement and tr -s squeezes multiple hyphens into single hyphens.

share|improve this answer

I have a script that performs a similar action on file names, though I replace spaces with underscores. You could use something like:

pbpaste | sed -e 's/ /-/g' | awk '{print tolower($0)}' | pbcopy 

combined with a tiny bit of applescript to form:

do shell script "pbpaste | sed -e 's/ /-/g' | awk '{print tolower($0)}' | pbcopy" 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.