Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
TextExpander Snippet to iOS/OS X Text Shortcuts. Read more: http://tmblr.co/ZFavEy-uZJ-M
set clickDelay to 0.3
set abbreviationList to {}
set snippetsList to {}
set disallowedGroups to {"AutoCorrect", "AutoCorrect Deutsch Snippets", "AlteNeue Rechtschreibung", "Deutsche Akronyme"}
set disallowedSnippetContents to {"%e", "%d", "%a", "%A", "%m", "%1m", "%b", "%B", "%y", "%Y", "%H", "%I", "%1H", "%1I", "%M", "%1M", "%S", "%1S", "%p", "%@+", "%@-", "%key:", "%clipboard", "%|", "%<", "%^", "%>", "%v", "%-", "%+", "%fill:", "%fillpart:", "%filltext:", "%fillpopup:", "%fillarea:"}
-- open the Text tab of Keyboard preferences
tell application "System Preferences"
activate
delay 5
activate
set keyboardPane to "com.apple.preference.keyboard"
set the current pane to pane id keyboardPane
--get the name of every anchor of pane id keyboardPane
-- returns: --> {"keyboardTab", "InputSources", "shortcutsTab", "Text", "keyboardTab_ModifierKeys"}
reveal anchor "Text" of pane id keyboardPane
delay 2
end tell
-- delete all existing shortcuts
tell application "System Events"
click table 1 of scroll area 1 of tab group 1 of window "Keyboard" of application process "System Preferences"
keystroke "a" using command down
click UI element 2 of group 1 of tab group 1 of window "Keyboard" of application process "System Preferences"
-- deletion takes a while
delay 3
end tell
-- pause TextExpander expansion
tell application "TextExpander" to set expansion enabled to false
-- get one specific group of TextExpander and make it an AppleScript list
tell application "TextExpander"
repeat with currentGroup in groups
if (name of currentGroup) is not in disallowedGroups then
--log name of currentGroup as rich text
set currentSnippets to snippets of currentGroup
repeat with currentSnip in currentSnippets
if ((content type of currentSnip) is not shell_script) ¬
and ((content type of currentSnip) is not apple_script) ¬
and ((abbreviation of currentSnip) is not "") then
set skipit to false
repeat with disallowedSnippetContent in disallowedSnippetContents
--log ("Snippet: " & (plain text expansion of currentSnip) as string) & " disallowed: " & disallowedSnippetContent
if ((content type of currentSnip) is rich_text) and ((rich text expansion of currentSnip) contains disallowedSnippetContent) then
set skipit to true
exit repeat
else if ((content type of currentSnip) is plain_text) and ((plain text expansion of currentSnip) contains disallowedSnippetContent) then
set skipit to true
exit repeat
end if
end repeat
if ((content type of currentSnip) = rich_text) and (skipit is false) then
set end of abbreviationList to abbreviation of currentSnip
set end of snippetsList to rich text expansion of currentSnip
else if ((content type of currentSnip) = plain_text) and (skipit is false) then
set end of abbreviationList to abbreviation of currentSnip
set end of snippetsList to plain text expansion of currentSnip
end if
end if
end repeat
end if
end repeat
end tell
log "Count of abbreviationList: " & (count of abbreviationList)
-- log snippetsList
-- create Text Shortcuts
tell application "System Events"
repeat with i from 1 to ((count of abbreviationList))
click UI element 1 of group 1 of tab group 1 of window "Keyboard" of application process "System Preferences"
delay clickDelay
keystroke (item i of abbreviationList) & "\t"
set the clipboard to (item i of snippetsList)
delay 0.2 -- putting something on the clipboard takes a while
keystroke "v" using command down
keystroke "\r"
delay clickDelay
-- wait a little longer every tenth iteration
if (i mod 10 is equal to 0) then
delay 4
-- log "10 times"
end if
-- log "current item: " & item i of abbreviationList
end repeat
end tell
-- reenable TextExpander expansion
delay 2
tell application "TextExpander" to set expansion enabled to true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment