I've got a macro that writes the values of every variable that fits certain criteria to a TXT file. Now I want to go the opposite way and import the contents of a folder full of TXT files (about 33 files) to the variables they match. For instance, I have the following files:
KM Var - HHHShowDistroURL.txt
KM Var - HHHShowMixcloudDescription.txt
KM Var - HHHShowMixcloudDescriptionMusicOnly.txt
KM Var - HHHShowMIxCloudEmbed.txt
and they match variables named
HHHShowDistroURL
HHHShowMixcloudDescription
HHHShowMixcloudDescriptionMusicOnly
HHHShowMIxCloudEmbed
I assume I would do a For Each, but how would I tell KM to match the filename KM Var - %VarName%.txt to the correct variable?
This AppleScript should do it (it overwrites existing variables and creates new ones if necessary).
Set the variable that holds the path to your folder in property theFolderPathVariable .
-- Set Keyboard Maestro variable from text file
property theFolderPathVariable : "" -- Set variable that holds a POSIX path (e.g. "/Users/USER/Desktop/Test/")
tell application id "com.stairways.keyboardmaestro.engine"
set theFolderPath to getvariable theFolderPathVariable
end tell
tell application "System Events"
try
set theFiles to files of folder theFolderPath whose name extension = "txt"
repeat with thisFile in theFiles
set thisKMVarName to (characters 10 thru -5 in (name of thisFile as string)) as string
set thisText to read thisFile as «class utf8»
tell application id "com.stairways.keyboardmaestro.engine"
setvariable thisKMVarName to thisText
end tell
end repeat
on error error_message number error_number
if the error_number is not -128 then display alert "System Events" message error_message as warning
return
end try
end tell
Thank you for this. I'm getting an error, though. I can't figure out why.
021-01-27 12:17:20 Execute macro “[SUB] Variables Load From Metadata Files” from trigger Editor
2021-01-27 12:17:28 Execute an AppleScript failed with script error: text-script:254:263: execution error: The variable File_Path is not defined. (-2753)
2021-01-27 12:17:28 Execute an AppleScript failed with script error: text-script:254:263: execution error: The variable File_Path is not defined. (-2753). Macro “[SUB] Variables Load From Metadata Files” cancelled (while executing Execute AppleScript).
In future please post the actual macro file(s) or relevant action(s) – and any other puzzle pieces necessary to making it/them work.
Asking people to eyeball your macros/scripts without giving them the means to see the actual macro/code on their system and to properly test leads to few offers of help.
Remember – the easier you make it for people to help you – the more likely you'll get quality help.
Using the right syntax helps...
Your syntax:
set theFolderPath to getvariable File_Path
Correct syntax:
set theFolderPath to getvariable "File_Path"
Don't forget to quote your Keyboard Maestro variable names.
Don't worry – I've made this mistake myself a time or two.
If I'd had the actual code it would have taken me much less time to figure out.
As it was I had to poke and prod for a while, and that's time-expensive.
In the past, I've been asked to only post a screenshot rather than a macro file. I suppose that was because the macros I tend to have problems with are so specific to my system and workflow. Believe me, I want to make it easy for people to help. I'll try to find a better balance between when to post code and when to post screenshots.
Thank you for taking the time to poke around in this. I appreciate it.