A quick piece of JavaScript for Applications:
- See which variables contain the largest amount of material
- Optionally select and clear a few variables.
Moderator Edit:
==Macro UPDATED== below by author: 2021-02-06
(See, for example, the need to trim down which was reported here:
The script below takes a few seconds to display the size-sorted list of KM variables, so if you paste it into (Yosemite+) Script Editor and run it from there, it displays a little progress pie-chart at the bottom of the script window.
(If you save it from Script Editor as an .app, and launch the application,
it will display a progress bar.
You can then, if you want, select any particularly large variables and click OK to empty their contents.
Source code:
(function () {
// LIST KEYBOARD MAESTRO VARIABLES
// SORTED BY DESCENDING SIZE
// OPTIONALLY EMPTY THE CONTENTS OF SELECTED VARIABLES
// CAUTION: NOT UNDOABLE
/* * *
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
* * */
var kme = Application("Keyboard Maestro Engine"),
vars = kme.variables(),
lngVars = vars.length,
lngHalf = Math.floor(lngVars / 2);
Progress.totalUnitCount = lngVars;
Progress.description = 'Sizing KM Engine variables';
Progress.additionalDescription = 'This may take a little while';
var lstSized = vars.map(
function (x, i) {
Progress.completedUnitCount = i / 2;
return {
name: x.name(),
size: x.value().length
};
}
);
Progress.description = 'Sorting KM Engine variables';
lstSized.sort(
function (a, b) {
return b.size - a.size;
}
);
Progress.description = 'Listing KM Engine variables';
var lstReport = lstSized.map(
function (dct, i) {
Progress.completedUnitCount = lngHalf + (i / 2);
return dct.name + '\t' + dct.size;
}
);
var a = Application.currentApplication(),
sa = (a.includeStandardAdditions = true, a);
var strClip = lstReport.join('\n');
sa.setTheClipboardTo(strClip);
sa.activate();
var varChoice = sa.chooseFromList(lstReport, {
withTitle: 'KME variable sizes',
withPrompt: 'Clear selected variables:',
okButtonName: 'Clear contents of selected variables',
cancelButtonName: 'Cancel',
multipleSelectionsAllowed: true,
emptySelectionAllowed: true
}),
lstChoice = varChoice ? varChoice : [];
if (lstChoice.length) {
var strNames = '';
lstChoice.forEach(
function (s) {
lstParts = s.split(/\t/);
kme.variables[lstParts[0]].value = '';
strNames += lstParts[0] + ' ';
}
);
sa.activate()
sa.displayNotification(strNames, {
withTitle: "Cleared selected KM variables:"
})
}
})();