Hey Folks,
For posterity here's the final script.
NOTE – This script will ONLY deal with QuicKeys shortcuts of the specific type Bill has posted, and it does NOTHING to filter those shortcuts from other kinds.
Import QuicKeys Type Text ShortCuts into Keyboard Maestro 8.scpt.zip (13.8 KB)
----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/11/18 18:30
# dMod: 2018/11/19 03:31
# Appl: Finder, Keyboard Maestro, System Events
# Task: Import QuicKeys Type Text ShortCuts into Keyboard Maestro 8.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Keyboard_Maestro, @System_Events, @Import, @QuicKeys, @Insert, @Text, @ShortCuts
----------------------------------------------------------------
set shortcutsFolderAlias to alias "Mercury:Users:myUser:Downloads:QuicKeys Shortcuts 2:Shortcuts:"
set shortcutsFolderPath to POSIX path of shortcutsFolderAlias
set errorLog to {}
set exportList to {}
set xmlHeader to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<array>
"
set end of exportList to xmlHeader
set xmlTemplate to "
<dict>
<key>Activate</key>
<string>Normal</string>
<key>Macros</key>
<array>
<dict>
<key>Actions</key>
<array>
<dict>
<key>Action</key>
<string>ByPasting</string>
<key>MacroActionType</key>
<string>InsertText</string>
<key>Paste</key>
<false/>
<key>Text</key>
<string>%Variable%Text%</string>
</dict>
</array>
<key>Name</key>
<string>%Variable%Name%</string>
<key>Triggers</key>
<array>
<dict>
<key>Case</key>
<string>Exact</string>
<key>DiacriticalsMatter</key>
<true/>
<key>MacroTriggerType</key>
<string>TypedString</string>
<key>OnlyAfterWordBreak</key>
<false/>
<key>SimulateDeletes</key>
<true/>
<key>TypedString</key>
<string>%Variable%Trigger%</string>
</dict>
</array>
</dict>
</array>
<key>Name</key>
<string>Text Expansion</string>
</dict>
"
set xmlFooter to "
</array>
</plist>
"
set shCMD to "find " & quoted form of shortcutsFolderPath & " -iname 'ExportedShortcutData.plist'"
set plistFileList to do shell script shCMD
set plistFileList to paragraphs of plistFileList
repeat with thePath in plistFileList
set contents of thePath to alias POSIX file thePath
end repeat
set AppleScript's text item delimiters to linefeed
repeat with theAlias in plistFileList
try
tell application "System Events"
set plData to (get text of property list item 1 of property list file (theAlias as text)) as text
set thePlist to make property list item with properties {text:plData}
set plistRecord to value of thePlist
set textExpansion to TypeTextArchiveTextToType of ShortcutStepArchivePluginStep of item 1 of ShortcutArchiveSteps of plistRecord
set AppleScript's text item delimiters to ".qkshortcutd"
tell application "Finder"
set abbrevName to text item 1 of (get name of theAlias's parent's parent)
end tell
copy xmlTemplate to tempXmlTemplate
set tempXmlTemplate to kmReplace("%Variable%Name%", abbrevName, tempXmlTemplate, true, false, false) of me
set tempXmlTemplate to kmReplace("%Variable%Text%", textExpansion, tempXmlTemplate, true, false, false) of me
set tempXmlTemplate to kmReplace("%Variable%Trigger%", "", tempXmlTemplate, true, false, false) of me
set end of exportList to tempXmlTemplate
end tell
on error
set end of errorLog to contents of theAlias
end try
end repeat
set end of exportList to xmlFooter
set AppleScript's text item delimiters to ""
set exportXML to exportList as text
set exportXML to kmReplace("&", "%26", exportXML, true, false, false) of me
tell application "Keyboard Maestro"
importMacros exportXML
end tell
----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on kmReplace(findPattern, replacePattern, dataStr, regExBool, caseBool, tokensBool)
tell application "Keyboard Maestro Engine"
set foundDataList to search dataStr for findPattern replace replacePattern ¬
regex regExBool case sensitive caseBool process tokens tokensBool
end tell
end kmReplace
----------------------------------------------------------------
Bill's zipped QuicKeys export file looks like this:
QuicKeys Shortcuts.qkmx.zip
And if unzipped with the Mac's built-in unzip tool, it actually decodes the export file as well:
The script above requires the user to give it an alias to the “Shortcuts” folder as seen in the above folder structure.
On my system the script when run from Script Debugger took about 30 seconds to import 450+ QK type-text shortcuts into Keyboard Maestro.
I cribbed from the second of Peter's posted macros above to write the import section of the script.
Keyboard Maestro balked initially on the import, because the XML contained unencoded ampersands (“&”) in some URLs – but I was able to encode them to “%26” to get around that.
-Chris