Skip to main content

things_mcp/core/applescript/
script.rs

1//! Pure AppleScript render functions for tag-admin ops. No I/O — each
2//! function takes the inputs the tool surface accepts and returns the
3//! AppleScript source as a `String`. The driver (`OsascriptDriver`) and
4//! the facade (`TagAdmin`) are the layers that actually run the script.
5
6/// Escape a user-supplied string for safe inclusion inside an AppleScript
7/// double-quoted literal. AppleScript's escape rules: backslash escapes
8/// itself and a literal double quote.
9pub fn escape_applescript_string(s: &str) -> String {
10    s.replace('\\', "\\\\").replace('"', "\\\"")
11}
12
13pub fn render_create_tag(name: &str, parent: Option<&str>) -> String {
14    let name_q = escape_applescript_string(name);
15    match parent {
16        Some(p) => {
17            let p_q = escape_applescript_string(p);
18            format!(
19                r#"tell application "Things3"
20    set newTag to make new tag with properties {{name:"{name_q}"}}
21    set parent tag of newTag to tag "{p_q}"
22end tell"#,
23            )
24        }
25        None => format!(
26            r#"tell application "Things3"
27    make new tag with properties {{name:"{name_q}"}}
28end tell"#,
29        ),
30    }
31}
32
33pub fn render_rename_tag(old: &str, new: &str) -> String {
34    let old_q = escape_applescript_string(old);
35    let new_q = escape_applescript_string(new);
36    format!(
37        r#"tell application "Things3"
38    set name of tag "{old_q}" to "{new_q}"
39end tell"#,
40    )
41}
42
43/// Reassign every to-do that carries `source` to also carry `target`, then
44/// delete the `source` tag. AppleScript surface: `to dos of tag "source"`
45/// enumerates the tasks; we add the target tag to each and then remove the
46/// source tag from the global tag list.
47pub fn render_merge_tags(source: &str, target: &str) -> String {
48    let s_q = escape_applescript_string(source);
49    let t_q = escape_applescript_string(target);
50    format!(
51        r#"tell application "Things3"
52    set sourceTag to tag "{s_q}"
53    set targetTag to tag "{t_q}"
54    repeat with t in (to dos of sourceTag)
55        set tag names of t to (tag names of t) & "{t_q}"
56    end repeat
57    delete sourceTag
58end tell"#,
59    )
60}
61
62pub fn render_delete_tag(name: &str) -> String {
63    let name_q = escape_applescript_string(name);
64    format!(
65        r#"tell application "Things3"
66    delete tag "{name_q}"
67end tell"#,
68    )
69}
70
71pub fn render_move_tag(name: &str, new_parent: Option<&str>) -> String {
72    let name_q = escape_applescript_string(name);
73    match new_parent {
74        Some(p) => {
75            let p_q = escape_applescript_string(p);
76            format!(
77                r#"tell application "Things3"
78    set parent tag of tag "{name_q}" to tag "{p_q}"
79end tell"#,
80            )
81        }
82        // `missing value` is AppleScript's null; assigning it promotes the
83        // tag to the root of the tag tree.
84        None => format!(
85            r#"tell application "Things3"
86    set parent tag of tag "{name_q}" to missing value
87end tell"#,
88        ),
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn escape_handles_quotes_and_backslashes() {
98        assert_eq!(escape_applescript_string("plain"), "plain");
99        assert_eq!(escape_applescript_string("he said \"hi\""), "he said \\\"hi\\\"");
100        assert_eq!(escape_applescript_string("path\\to"), "path\\\\to");
101    }
102
103    #[test]
104    fn create_tag_no_parent_renders_make_tag() {
105        let s = render_create_tag("Work", None);
106        assert!(s.contains("tell application \"Things3\""));
107        assert!(s.contains("make new tag with properties {name:\"Work\"}"));
108        assert!(!s.contains("parent tag"));
109    }
110
111    #[test]
112    fn create_tag_with_parent_renders_parent_link() {
113        let s = render_create_tag("Urgent", Some("Work"));
114        assert!(s.contains("make new tag with properties {name:\"Urgent\"}"));
115        assert!(s.contains("set parent tag of newTag to tag \"Work\""));
116    }
117
118    #[test]
119    fn create_tag_escapes_quotes_in_name() {
120        let s = render_create_tag("She said \"yes\"", None);
121        assert!(s.contains("name:\"She said \\\"yes\\\"\""));
122    }
123
124    #[test]
125    fn rename_tag_renders_set_name() {
126        let s = render_rename_tag("Old", "New");
127        assert!(s.contains("set name of tag \"Old\" to \"New\""));
128    }
129
130    #[test]
131    fn rename_tag_escapes_quotes() {
132        let s = render_rename_tag("a\"b", "c\"d");
133        assert!(s.contains("set name of tag \"a\\\"b\" to \"c\\\"d\""));
134    }
135
136    #[test]
137    fn merge_tags_renders_loop_and_delete() {
138        let s = render_merge_tags("Source", "Target");
139        assert!(s.contains("set sourceTag to tag \"Source\""));
140        assert!(s.contains("set targetTag to tag \"Target\""));
141        assert!(s.contains("repeat with t in (to dos of sourceTag)"));
142        assert!(s.contains("delete sourceTag"));
143    }
144
145    #[test]
146    fn merge_tags_escapes_quotes_in_target_inside_loop_body() {
147        let s = render_merge_tags("A", "B \"quoted\"");
148        // The loop body assigns the target name via concatenation; the
149        // escaped form must appear in both the binding line and the loop.
150        assert!(s.contains("set targetTag to tag \"B \\\"quoted\\\"\""));
151        assert!(s.contains("& \"B \\\"quoted\\\"\""));
152    }
153
154    #[test]
155    fn delete_tag_renders_delete() {
156        let s = render_delete_tag("Stale");
157        assert!(s.contains("delete tag \"Stale\""));
158    }
159
160    #[test]
161    fn delete_tag_escapes_quotes() {
162        let s = render_delete_tag("Bad\"name");
163        assert!(s.contains("delete tag \"Bad\\\"name\""));
164    }
165
166    #[test]
167    fn move_tag_under_parent_renders_set_parent() {
168        let s = render_move_tag("Urgent", Some("Work"));
169        assert!(s.contains("set parent tag of tag \"Urgent\" to tag \"Work\""));
170    }
171
172    #[test]
173    fn move_tag_to_root_uses_missing_value() {
174        let s = render_move_tag("Urgent", None);
175        assert!(s.contains("set parent tag of tag \"Urgent\" to missing value"));
176    }
177
178    #[test]
179    fn move_tag_escapes_quotes_in_both_names() {
180        let s = render_move_tag("a\"b", Some("c\"d"));
181        assert!(s.contains("set parent tag of tag \"a\\\"b\" to tag \"c\\\"d\""));
182    }
183}