Skip to content

Instantly share code, notes, and snippets.

@ToniWonKanobi
Last active February 15, 2018 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToniWonKanobi/a1a761a95fcc32625370 to your computer and use it in GitHub Desktop.
Save ToniWonKanobi/a1a761a95fcc32625370 to your computer and use it in GitHub Desktop.
Creates a URL-safe string of text from otherwise unsafe text. If you wanted to create a service from this, open an Automator project that grabs the clipboard content, then run this AppleScript on said clipboard content, then re-copy the output the clipboard again.
# What is this?
# This AppleScript takes as input the clipboard content [that is potentially unsafe-for-URLs string of text] (such as a blog post in Title Case)
# The script then creates a URL-safe version
# For example: `This Is a Title of a Post!` --> `this-is-a-title-of-a-post`
# Boomshakalaka
# This is from https://superuser.com/questions/635351/process-clipboard-content-on-mac-os
# You can create an Automater service as well: https://d.pr/i/xtX7kN+
# The workflow:
# 1) Select text
# 2) Copy to clipboard
# 3) Run Slugify.workflow (as a Service in macOS `~/Library/Services`)
# 4) Paste converted text
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
# Alex had a bunch of special characters getting converted to their URL-safe versions. Since my desire was to just create clean filenames, I actually wanted to remove those characters entirely
set theresult to normalize(theclip)
set theresult to replace_chars(theresult, " ", "-")
set theresult to change_case(theresult)
set theresult to replace_chars(theresult, "%", "")
set theresult to replace_chars(theresult, "<", "")
set theresult to replace_chars(theresult, ">", "")
set theresult to replace_chars(theresult, "#", "")
set theresult to replace_chars(theresult, "{", "")
set theresult to replace_chars(theresult, "}", "")
set theresult to replace_chars(theresult, "|", "")
set theresult to replace_chars(theresult, "\\", "")
set theresult to replace_chars(theresult, "^", "")
set theresult to replace_chars(theresult, "~", "")
set theresult to replace_chars(theresult, "[", "")
set theresult to replace_chars(theresult, "]", "")
set theresult to replace_chars(theresult, "`", "")
set theresult to replace_chars(theresult, ";", "")
set theresult to replace_chars(theresult, "/", "")
set theresult to replace_chars(theresult, "?", "")
set theresult to replace_chars(theresult, ":", "")
set theresult to replace_chars(theresult, "@", "")
set theresult to replace_chars(theresult, "=", "")
set theresult to replace_chars(theresult, "&", "")
set theresult to replace_chars(theresult, "$", "")
# I added the following three replacements
set theresult to replace_chars(theresult, ",", "")
set theresult to replace_chars(theresult, "'", "")
set theresult to replace_chars(theresult, "\"", "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment