osascript_rs/
lib.rs

1pub use macros::__applescript;
2use std::process::Command;
3
4pub fn run_applescript(lines: &[&str]) {
5    println!("Executing script lines: {:?}", lines);
6    
7    let mut cmd = Command::new("osascript");
8    for line in lines {
9        cmd.arg("-e").arg(line);
10    }
11
12    let process = cmd.spawn().expect("Failed to run AppleScript");
13    println!("{:?}", process.wait_with_output().unwrap());
14}
15
16#[macro_export]
17macro_rules! applescript {
18    ($($script: tt)*) => {{
19        use $crate::{run_applescript, __applescript};
20
21        __applescript!($($script)*)
22    }};
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28
29    #[test]
30    fn test_open_url() {
31        applescript! {
32            tell application "Safari" to open location "https://www.rust-lang.org"
33        }
34    }
35
36    /* IMPORTANT: multiline requires each line end with ; */
37    #[test]
38    fn test_multiline_works() {
39        applescript! {
40            tell application "Safari";
41                activate;
42                open location "https://www.rust-lang.org";
43                set bounds of front window to {100, 100, 1000, 800};
44            end tell;
45        }
46    }
47
48    #[test]
49    fn test_list_notes() {
50        applescript! {
51            tell application "Notes";
52                set noteList to "";
53                    repeat with aNote in notes;
54                        set noteList to noteList & name of aNote & linefeed;
55                    end repeat;
56                return noteList;
57            end tell;
58        }
59    }
60
61}