Read File to Variable with Matching Filename

This should be simple, but it's eluding me.

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?

Here's how I create the files if it helps:

Hey @iampariah,

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


Hi, @pete31

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).

Here's the macro:

And here's the folder. I verified the path is correct with an Open action (not shown).

Hey Pariah,

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...  :sunglasses:

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.

-Chris

1 Like

Thanks, Chris.

That fixed it. A basic mistake. :grinning:

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.

Best,

Pariah

You didn't do what I wrote as instructions :wink: Glad it's working now.

1 Like