How to use global variables in MidiPipe AppleScript Triggers

Hi there, I am a long-time fan of MidiPipe, and a new subscriber to your forum. As a result of perusing the coding examples at this forum, today I have written my first (successful) AppleScript Trigger, to condition misbehaving damper pedal msgs from my keyboard…

on runme(message)
if item 1 of message = 176 then
if item 2 of message = 64 then
if item 3 of message < 64 then
set item 3 of message to 0
else
set item 3 of message to 127
end if
end if
end if
return message
end runme

I have used MidiPipe for decades, but I have never learned how to code an AppleScript Trigger. And as a result I have never used more than one MidiPipe at a time. Because I have a very simple setup, midi flows through all pipes at the same time, and there was no way for me to conditionally filter the flow through different pipes.

But now that I can utilise AppleScript Triggers, I should be able to do just that, if only I had access to a global variable that scripts in different pipes could talk through.

The concept of global variables appeared in the thread entitled ‘Store/Retrieve more than one variable in Clipboard using an Array?’ (https://macscripter.net/viewtopic.php?id=45832). Here I found mention of hidden global variables that I could co-opt for my own use…

my minutes
my hours
my days
my weeks
my linefeed
my quote
my text item delimiters
my pi

But I can’t seem to make them work. And I have been unable to find an example of how to make them work. Would somebody do me the kindness of posting some examples of how these host variables can be used?

Model: iMac (Retina 5K, 27-inch, 2019)
AppleScript: 1.1.2
Browser: Firefox 98.0
Operating System: macOS 10.14

In appleScript, Global variables refer to a variable who value being available to all handlers within the same script.

It sounds like for your purposes you want values from the app made available to multiple scripts.

Is that correct? That’s doable, but it’s a little tricky.

Not exactly. I just want access to variables that are persistently available across multiple AppleScript Trigger calls, so that I can use those variable to allow or disallow the flow of midi through different MidiPipe pipes. Then I can process different pipes based on a variable that lives beyond my last AppleScript Trigger. Still doable?

Hi gcc@macscripter. Welcome to MacScripter.

I’d quite forgotten the other thread you mention. The problem it addresses is that it’s not possible to share variables between scripts in a MidiPipe “pipe” (or at least it wasn’t five years ago).

The hack solution was to use AppleScript constants instead. Although they’re described as “constants”, scripts can in fact set them to values other than the defaults:

display dialog hours --> 3600 (default value: the number of seconds in an hour)

set hours to "Rhubarb"
display dialog hours --> Rhubarb

Except in the case of ‘text item delimiters’, changing constants’ values is normally something to avoid. But since their default values are unlikely to be needed in MidiPipe scripts, and the constants themselves belong to the instance of ‘AppleScript’ assigned to MidiPipe when it launches rather than to the scripts themselves, they could conceivably be hacked to act as pseudo-globals here.

The idea of course is that if a script in the pipe produces a value which needs to be used by another script further down the pipe, it sets one of the AppleScript constants to the value. The other script, written in the knowledge of which constant will have been used, reads the value from that when it in turn is executed. I think the word ‘my’ needed to be inserted before the constant’s name in the reading script to ensure there was no confusion. I don’t remember if this was necessary in the setting script too.

I’ve not had time to refamiliarise myself with MidiPipe, nor to download the latest version to see if it still works in the same way. Probably the main thing to remember, if the hack still works, is that any change to a constant’s value is immediate and global throughout MidiPipe until the application’s quit. This may have ramifications for other pipes or an earlier script in the same one.

Hi Nigel Garvey, thank you for your generous reply.

Since I wrote to the forum, I have discovered that there were two reasons I was struggling to learn how to use MidiPipe’s AppleScript Trigger function:

First, I could not get stuff to work because I had not given MidiPipe Accessibility permission in Security & Privacy in System Preferences. Without it, all of MidiPipe’s built-in functions worked perfectly, and some AppleScript Trigger functionality also worked. But none of the common debugging techniques worked. MidiPipe could not speak to Finder.

But the second reason was more insidious and pernicious. I couldn’t understand why code I wrote and compiled in AppleScript Editor, would sometimes refuse to ‘compile’ when pasted into MidiPipe. The reason was that the [compiled | not compiled] indicator in the MidiPipe AppleScript Trigger window, apparently doesn’t update itself until a midi message flows though it!

I’ve been a Mac user most of my life, but never learned how make use of AppleScript. I am grateful to MidiPipe for finally giving me a reason compelling enough to learn it. And also to MacScripter for providing a vehicle to make it happen.

Cheers