3 releases (breaking)

Uses old Rust 2015

0.3.0 May 24, 2017
0.2.0 Mar 24, 2017
0.1.0 Mar 24, 2017

#89 in macOS and iOS APIs

Download history 3374/week @ 2023-12-13 3104/week @ 2023-12-20 4426/week @ 2023-12-27 2697/week @ 2024-01-03 3289/week @ 2024-01-10 3309/week @ 2024-01-17 3022/week @ 2024-01-24 3841/week @ 2024-01-31 5652/week @ 2024-02-07 2718/week @ 2024-02-14 2064/week @ 2024-02-21 2808/week @ 2024-02-28 2654/week @ 2024-03-06 3246/week @ 2024-03-13 4756/week @ 2024-03-20 4122/week @ 2024-03-27

15,248 downloads per month
Used in 3 crates (2 directly)

BSD-3-Clause

6KB
88 lines

rust-osascript

This library implements a simplified wrapper around the OSA system on OS X to remote control applications with JavaScript.


lib.rs:

This library implements limited functionality for the OSA System on macOS. In particular it allows you to execute JavaScript via the OSA system to script applications. It's particularly useful if you need to tell other applications to execute certain functionality.

Currently only JavaScript is supported. Parameters passed to it show up as $params and the return value from the script (as returned with the return keyword) is deserialized later.

Example

extern crate osascript;
#[macro_use] extern crate serde_derive;

#[derive(Serialize)]
struct AlertParams {
    title: String,
    message: String,
    alert_type: String,
    buttons: Vec<String>,
}

#[derive(Deserialize)]
struct AlertResult {
    #[serde(rename="buttonReturned")]
    button: String,
}

fn main() {
    let script = osascript::JavaScript::new("
        var App = Application('Finder');
        App.includeStandardAdditions = true;
        return App.displayAlert($params.title, {
            message: $params.message,
            'as': $params.alert_type,
            buttons: $params.buttons,
        });
    ");

    let rv: AlertResult = script.execute_with_params(AlertParams {
        title: "Shit is on fire!".into(),
        message: "What is happening".into(),
        alert_type: "critical".into(),
        buttons: vec![
            "Show details".into(),
            "Ignore".into(),
        ]
    }).unwrap();

    println!("You clicked '{}'", rv.button);
}

Dependencies

~0.6–1.4MB
~32K SLoC