Use a list to set variables

I am passing a list from Applescript to Keyboard Maestro and need to set variables based on this list.

I am using the %TriggerValue% to set a variable at the beginning of a macro. I will call this variable ‘AppleScriptList’. As an example the list might have two items such as:

Acme Company
/Volumes/ServerName/Folder A/Acme Archive.pdf

How can I assign ‘Acme Company’ to a variable called ‘Company’ and the variable ‘Path’ to the 2nd line of the AppleScriptList?

I'm not sure I understand your process. It would be best to post your AppleScript and your KM macro.

Having said that, you have the most control over setting KM variables using AppleScript list variables in the AppleScript code.

To set KM variables from AppleScript, see:

For JXA Functions:
Get & Set Functions for KM Variables JXA.js

Hey Stephen,

In fact it seems you have to pass to the AppleScript the list:

'AppleScriptList', 'Company', 'Path'

'AppleScriptList' =>
'Company' <=
'Path' <=

--Alain

Hey Stephen,

JM is absolutely right. The best way to do this is from AppleScript.

If you have trouble sorting that out holler at me.

-Chris

Just for giggles.

Here's how to create new variables on the fly via Keyboard Maestro:

-Chris


Creating New Variables on the Fly.kmmacros (2.4 KB)

Sorry if it was not as clear as I thought it was when posting. Here is what is taking place.

I use a program called Hazel to watch a folder for new pdfs. Based on what Hazel finds in the PDFs it runs an applescript that runs a Keyboard Maestro macro. When applescript runs the macro it passes along two lines of text as set in the HazelList applescript variable.

=============================================================================

Here is the Applescript

-- theFile is what Hazel passes to this script
-- inputAttributes is a list of other items Hazel is passing to this script

set newFilePath to (the POSIX path of theFile) as text
set HazelList to inputAttributes & return & theFile as text

tell application "Keyboard Maestro Engine"
	do script "94453F78-F5C4-4F3A-B79A-866D24087810" with parameter HazelList
end tell

=============================================================================

Here is what is being passed to the Keyboard Maestro macro via HazelList:

Acme Company
/Volumes/ServerName/Folder A/Acme Archive.pdf

The image below shows how I am setting the variable HazelList in the first step in Keyboard Maestro based on the passed parameter. My problem is trying to then set two separate variables in Keyboard Maestro based on the first and second line of the variable HazelList.

I want the variable COMPANY to be set to Acme Company
I want the variable FILEPATH to be set to /Volumes/ServerName/Folder A/Acme Archive.pdf

Hey Stephen,

Oh, well that's simple enough.

Your regex is problematic.

(?s) won't do anything for you. (What are you trying to do there?)

You don't really want zero or more linefeeds. You want a fixed number or one or more.

Your parameter may or may not be using a linefeed to separate the lines, so we're covering bases by using linefeed OR carriage return.

-Chris

Generic-Test 01.kmmacros (2.1 KB)

1 Like

Chris,

Thanks for the help! That got me going and I have finished my Keyboard Maestro macro. Should save me many many hours a year :wink:

. . . Stephen