A container for various utility functions and constants util object.

interface Util {
    base64Decode(string): string;
    base64Encode(string, options?): string;
    clarify(obscuredString): any;
    constant: {
        KEY_DELETE: 51;
        KEY_DOWNARROW: 125;
        KEY_ESCAPE: 53;
        KEY_LEFTARROW: 123;
        KEY_RETURN: 36;
        KEY_RIGHTARROW: 124;
        KEY_SPACE: 49;
        KEY_TAB: 48;
        KEY_UPARROW: 126;
        MODIFIER_COMMAND: 1048576;
        MODIFIER_CONTROL: 262144;
        MODIFIER_OPTION: 524288;
        MODIFIER_SHIFT: 131072;
    };
    getRandomValues(typedArray): void;
    hmac(data, key, algorithm): Uint8Array;
    localeInfo: {
        currencyCode: string;
        currencySymbol: string;
        decimalSeparator: string;
        groupingSeparator: string;
        languageCode: string;
        localeIdentifier: string;
        regionCode: string;
    };
    localize(string): string;
    randomUuid(): string;
    timeZoneInfo: {
        abbreviation: string;
        daylightSaving: boolean;
        identifier: string;
        secondsOffset: number;
    };
}

Properties

constant: {
    KEY_DELETE: 51;
    KEY_DOWNARROW: 125;
    KEY_ESCAPE: 53;
    KEY_LEFTARROW: 123;
    KEY_RETURN: 36;
    KEY_RIGHTARROW: 124;
    KEY_SPACE: 49;
    KEY_TAB: 48;
    KEY_UPARROW: 126;
    MODIFIER_COMMAND: 1048576;
    MODIFIER_CONTROL: 262144;
    MODIFIER_OPTION: 524288;
    MODIFIER_SHIFT: 131072;
}

The constant property is a container for pre-defined constants.

Type declaration

  • Readonly KEY_DELETE: 51

    Key code for the Delete (⌫) key.

  • Readonly KEY_DOWNARROW: 125

    Key code for the Down Arrow key.

  • Readonly KEY_ESCAPE: 53

    Key code for the Escape key.

  • Readonly KEY_LEFTARROW: 123

    Key code for the Left Arrow key.

  • Readonly KEY_RETURN: 36

    Key code for the Return (↵) key.

  • Readonly KEY_RIGHTARROW: 124

    Key code for the Right Arrow key.

  • Readonly KEY_SPACE: 49

    Key code for the space bar.

  • Readonly KEY_TAB: 48

    Key code for the Tab (⇥) key.

  • Readonly KEY_UPARROW: 126

    Key code for the Up Arrow key.

  • Readonly MODIFIER_COMMAND: 1048576

    Bit mask for the Command (⌘) key.

  • Readonly MODIFIER_CONTROL: 262144

    Bit mask for the Control (⌃) key.

  • Readonly MODIFIER_OPTION: 524288

    Bit mask for the Option (⌥) key.

  • Readonly MODIFIER_SHIFT: 131072

    Bit mask for the Shift (⇧) key.

localeInfo: {
    currencyCode: string;
    currencySymbol: string;
    decimalSeparator: string;
    groupingSeparator: string;
    languageCode: string;
    localeIdentifier: string;
    regionCode: string;
}

Get information about the current locale as configures in macOS settings.

Type declaration

  • currencyCode: string
  • currencySymbol: string
  • decimalSeparator: string
  • groupingSeparator: string
  • languageCode: string
  • localeIdentifier: string
  • regionCode: string
timeZoneInfo: {
    abbreviation: string;
    daylightSaving: boolean;
    identifier: string;
    secondsOffset: number;
}

Get information about the current time zone as configured in macOS settings.

Type declaration

  • abbreviation: string
  • daylightSaving: boolean
  • identifier: string
  • secondsOffset: number

Methods

  • Decode a Base-64 string and interpret the result as a UTF-8 string.

    Accepts both standard and URL-safe variants as input. Also accepts input with or without the =/== end padding. Throws an error if the input cannot be decoded as a UTF-8 string.

    Parameters

    • string: string

    Returns string

    The decoded string

  • Encode a string as UTF-8 then Base-64 encode the result.

    Parameters

    • string: string

      The string to encode.

    • Optional options: {
          trimmed?: boolean;
          urlSafe?: boolean;
      }
      • Optional trimmed?: boolean

        Whether to trim the =/== padding from the string. Default is no.

      • Optional urlSafe?: boolean

        Whether to encode using the URL-safe variant, with - and _ substituted for + and /. Default is no.

    Returns string

  • Decipher a JSON object that has been lightly obscured to prevent constants such as API client identifiers appearing in plaintext in the source files.

    This function will ROT13 decipher the text, apply Base64 decoding, and parse the result as JSON.

    Parameters

    • obscuredString: string

    Returns any

  • Fill the provided TypedArray with cryptographically secure random values. This aims work like crypto.getRandomValues() from Web Crypto API. Internally, it is implemented using Apple's SecRandomCopyBytes.

    Parameters

    • typedArray: Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | BigInt64Array | BigUint64Array

      The array to fill with random values. This will be modified in place.

    Returns void

    Example

    const array = new Uint8Array(16); // array of 16 bytes
    util.getRandomValues(array); // array is now filled with random bytes
  • Generate hash-based message authentication code (HMAC) using the supplied data, key and algorithm. Implemented internally by Apple's CommonCrypto.

    Parameters

    • data: Uint8Array
    • key: Uint8Array
    • algorithm: "sha1" | "md5" | "sha256" | "sha384" | "sha512" | "sha224"

    Returns Uint8Array

  • Localize an English string into the current user interface language, if possible. This will work for strings which match an existing string in PopClip's user interface.

    Parameters

    • string: string

      The string to localize.

    Returns string

    The localized string, or the original string if no localized version was avaiable.

  • Generate a RFC 4122 version 4 UUID using a cryptographically secure random number generator.

    Returns string

    UUID string such as "e621e1f8-c36c-495a-93fc-0c247a3e6e5f".