Can't Get a KM Variable to Work in AppleScript

I'm trying to change a Logic Pro setting to a number held in a Keyboard Maestro variable, but I can't get it to work. Here is the code:

tell application "Keyboard Maestro Engine"
set _FadeLength to getvariable "FadeLength"
end tell

tell application "Logic Pro X"
	tell application "System Events"
		tell process "Logic Pro X"
			set value of slider 1 of row 11 of outline 1 of scroll area 1 of group 1 of list 1 of group 2 of window "test - Tracks" to _FadeLength
		end tell
	end tell
end tell

When I run this, nothing happens. The setting doesn't change. The code seems to be right because I can substitute a number for _FadeLength and it works. And it should be retrieving the variable correctly because this works:

tell application "Keyboard Maestro Engine"
	set _FadeLength to getvariable "FadeLength"
end tell

display notification _FadeLength

What am I doing wrong?

Answering my own question! KM provides the variable as a string, and I needed to convert it to an integer, with the following code:

set _FadeLength to _FadeLength as integer

2 Likes

Hi. Were you making a script that accesses Logic's inspector parameters directly without the need for clicking at a found image? If so, I'd be interested to know how you got on. Cheers!

Yes that's exactly what I was doing! Unfortunately, Logic doesn't have proper AppleScript support, so you have to 'brute force' it with UI scripting (i.e. telling it exactly where in the user interface to change parameters, e.g. 'first box on the left, third line down, change this parameter'. This works, but it's not quite as instant, and every time they update Logic there's a chance it will break (parameters will move slightly etc).

Anyway, here's my script for fade-out which works on Logic 10.6.1. (for me anyway!):

# get length of fade from Keyboard Maestro
tell application "Keyboard Maestro Engine"
	set _FadeLength to getvariable "FadeLength"
	set _FadeLength to _FadeLength as integer
end tell


set _GainRow to 8
set _FadeInRow to 11
set _FadeOutRow to 13
set _FadeTypeRow to 14
set _MoreRow to 9

tell application "Logic Pro X"
	activate
end tell

tell application "System Events"
	tell process "Logic Pro X"
		
		-- Check if control bar is present
		set _TracksWindow to title of first window whose title contains "- Tracks"
		if (description of group 1 of window _TracksWindow = "Control Bar") then
			set _GroupNumber to 2
		else
			set _GroupNumber to 1
		end if
		
		-- Check if Movie mini-window is present
		if (value of static text 1 of group 1 of list 1 of group _GroupNumber of window _TracksWindow is "Movie") then
			set _SubGroupNumber to 2
		else
			set _SubGroupNumber to 1
		end if
		
		-- Open the "Region" disclosure triangle, if it isn't already
		if value of UI element 2 of group _SubGroupNumber of list 1 of group _GroupNumber of window _TracksWindow is not equal to 1 then
			click UI element 1 of group _SubGroupNumber of list 1 of group _GroupNumber of window _TracksWindow
			delay 0.15
		end if
		
		-- Open the "More" disclosure triangle, if it isn't already
		if value of UI element 1 of group 1 of row _MoreRow of outline 1 of scroll area 1 of group _SubGroupNumber of list 1 of group _GroupNumber of window _TracksWindow is not equal to 1 then
			click UI element 1 of group 1 of row _MoreRow of outline 1 of scroll area 1 of group _SubGroupNumber of list 1 of group _GroupNumber of window _TracksWindow
		end if
		
		-- Set crossfade type to Out, if it isn't already
		if value of pop up button 1 of row _FadeTypeRow of outline 1 of scroll area 1 of group _SubGroupNumber of list 1 of group _GroupNumber of window _TracksWindow is not "Out" then
			tell pop up button 1 of row _FadeTypeRow of outline 1 of scroll area 1 of group _SubGroupNumber of list 1 of group _GroupNumber of window _TracksWindow
				click
				click menu item "Out" of menu 1
			end tell
		end if
		
		-- Set fade length
		tell row _FadeOutRow of outline 1 of scroll area 1 of group _SubGroupNumber of list 1 of group _GroupNumber of window _TracksWindow
			set value of slider 1 to _FadeLength
		end tell
		
	end tell
end tell

You can tweak this script to alter other things like fade in, gain etc.

I made variables ("GainRow" etc) for the position of the UI element that is being edited, to make it easier to change if a Logic update moves something. This is literally just how many rows you have to count down in the inspector box on the left.

FadeLength is a variable containing how long you want the fade to be. I have buttons on my Stream Deck which change the fadeLength variable, so I press a button with how long the fade should be, then a button with what sort of fade. I made a load of if-then statements on the FadeLength buttons so it changes the colour of the selected Fade Length button:

I'm not an expert at this stuff so it was lots of googling and trial and error (and FAR too much time designing icons) but it's massively sped up my workflow! I just wish Apple would put proper AppleScript support in their own apps. The possibilities for things like Stream Decks would be endless.

Awesome!!!

Ok seems like we're in similar frames of mind.. I also have a Stream Deck and have set up fades but I know zilch about scripting so I just used found images. Here's how I have mine set up:

image

The top row is fade in/out; the bottom also includes EqP fades. "?" prompts me to enter my own fade length and the other four buttons on each row pass along the value entered into the KMLink Parameter box to be pasted via KM.

Direct parameter access is preferable, but without knowing what I'm doing I think I'm better sticking to found images. At least I understand what each action in a KM macro is doing; scripts are a foreign language to me and I wouldn't know how to troubleshoot if things go awry. I'm currenly using a similar script I found on a forum somewhere for region gain, but if anything changes in an LP update, I'll be screwed!

"I made a load of if-then statements on the FadeLength buttons so it changes the colour of the selected Fade Length button"

Could you explain this a bit? Do you mean if/then within Stream Deck or within KM? Did you figure out a way to get SD button feedback triggered from KM macros? I'm probably misreading that, as I think it's a one-way conversation between SD and KM.

Among other things, I've set up Insert Plugin buttons on my SD too. See this post if you're interested in how: How to select a virtual instrument in Logic Pro X. Is it possible?

I'd love to be able to directly access plugin slots via scripts as my current macros are necessarily complex. Soundflow manages it, so it must be possible, but I don't think it's as simple as tabbing down UI lines, like in your script. I could be wrong.

Also, gosh darn it, your icons are so nice. I might have to up my game!

Yes, if you search for Stream Deck in the Keyboard Maestro actions, you'll see "Set Image" and "Set Title" so you can actually change what the buttons are showing. So my script basically says:

If the user presses the "100" button, set the variable to 100, then change the "100" button to the red image, and change all the other buttons to the green image"

The one slightly clunky thing is that you have to specify the button you want to change the image on by row and column number, so you can't just move the button in Stream Deck later unless you go into your Keyboard Maestro macro and change the references.

So every time I press one of the buttons to select a fade length, it's actually sending a fresh background image to all 8 buttons. But in practice it just looks like the button I pressed has changed to red, and now I know whichever type of fade button I press (the ones in yellow), it will do it to that length.

Oh, and regarding icons, there are a couple of great free resources care of Apple

The first is you can Right click the Logic app and Show Package Contents, and then navigate to Contents > Frameworks > MAResources.Framework > Versions A > Resources, and you'll find all Logic's icons etc. Make sure to copy them rather than move them! But then you can put them in your image editor of choice and they are a good head start for some things. Obviously only use these for your own stream deck.

There is also an SF Symbols Mac app which you can download from Apple's website (you can find the location on Google) which has loads of random icons.

The green/red/yellow fades on my screenshots are just fades I made in Logic then screenshotted (the numbers on top are just the SD's own title text).

I'd love to be able to directly access plugin slots via scripts as my current macros are necessarily complex. Soundflow manages it, so it must be possible, but I don't think it's as simple as tabbing down UI lines, like in your script. I could be wrong.

There's a Mac app called PlugSearch - might be worth a look in case you can manipulate it with KM somehow

Brilliant. I'm going to look into the Set Image thing. Thanks for that! Looks like it might be nice way to switch between play and stop icons on the transport control. Not sure I understand why you need to send a fresh image to the buttons that don't change, but I'm sure it'll make sense once I get stuck in.

EDIT: dammit, looks like it only works with the KM plugin, not KMLink, which is what most of my buttons use.

My fade icons were made in photoshop from overlaid shapes, but yours look so much better.

I don't know if you checked out the link in my previous post, but I use Plugsearch as part of my Insert Plugin macro. Works great, but mouse positioning is tricky and requires some very very very precise screenshots. Maybe Apple will allow some built-in macro functionality at some point. I live in hope!

Thanks again for the insights. :slight_smile:

You only really need to send a fresh image to two buttons - the newly selected one, and the one which will no longer be selected (which I guess you could identify based on the starting value of the Fade Length variable). I just brute forced it to refresh the status of every button every time, because I'm not a programmer and I was worried about what would happen if it got out of sync, but it was probably overkill :slight_smile:

Yes, this is why you have to specify the row and column of the buttons, which is a limitation of the KM plugin. I use KMLink where possible, because it's simpler to hook up to KM actions, and I can rearrange my Stream Deck at will just by dragging and dropping the buttons. But if you want to change the image or text of a button, you're stuck with the KM plugin at the moment.

I just made actual fades in Logic then screenshotted them - I'll try and paste the images onto this post if I can. Another good tip is to make an Automator action which automatically reduces images to 144x144 pixels - I found this to be the sweet spot for them to look sharp on the SD display (which is 72x72).

I really wish they would! Just having AppleScript support for all the basic parameters would be a game changer, and with devices like Stream Decks getting popular I think there would be huge take-up, far more than there would have been in years past.

Fade-X Orange
Fade In Orange
Fade Out Orange
Crossfade Red
Crossfade Green

So kind of you to share those icons!!! I'm gonna try them out. :slight_smile:

1 Like