basic/
basic.rs

1extern crate osascript;
2#[macro_use] extern crate serde_derive;
3
4use osascript::JavaScript;
5
6#[derive(Serialize)]
7struct AlertParams {
8    title: String,
9    message: String,
10    alert_type: String,
11    buttons: Vec<String>,
12}
13
14#[derive(Deserialize)]
15struct AlertResult {
16    #[serde(rename="buttonReturned")]
17    button: String,
18}
19
20fn main() {
21    let script = JavaScript::new("
22        var App = Application('Finder');
23        App.includeStandardAdditions = true;
24        return App.displayAlert($params.title, {
25            message: $params.message,
26            'as': $params.alert_type,
27            buttons: $params.buttons,
28        });
29    ");
30
31    let rv: AlertResult = script.execute_with_params(AlertParams {
32        title: "Shit is on fire!".into(),
33        message: "What is happening".into(),
34        alert_type: "critical".into(),
35        buttons: vec![
36            "Show details".into(),
37            "Ignore".into(),
38        ]
39    }).unwrap();
40
41    println!("You clicked '{}'", rv.button);
42}