alfred_workflow_rust_project/
alfred.rs

1use serde::{Deserialize, Serialize};
2use crate::alfred_logger::Logger;
3
4#[derive(Serialize, Deserialize)]
5pub struct Alfred {}
6
7impl Alfred {
8    pub fn init() -> Alfred {
9        Logger::init();
10        Alfred {}
11    }
12
13    pub fn search(&self, query: &str) {
14        let script = format!("Application(\"{}\").search(\"{}\");", self.get_app_name(), query);
15        let mut _command = std::process::Command::new("/usr/bin/osascript")
16            .args(["-l", "JavaScript", "-e", script.as_str()])
17            .spawn();
18    }
19    pub fn action(&self, query: &str) {
20        let script = format!("Application(\"{}\").action(\"{}\");", self.get_app_name(), query);
21        let mut _command = std::process::Command::new("/usr/bin/osascript")
22            .args(["-l", "JavaScript", "-e", script.as_str()])
23            .spawn();
24    }
25    pub fn action_with_types(&self, query: &str, action_type: &str) {
26        let script = format!("Application(\"{}\").action(\"{}\",\"{}\");", self.get_app_name(), query, action_type);
27        let mut _command = std::process::Command::new("/usr/bin/osascript")
28            .args(["-l", "JavaScript", "-e", script.as_str()])
29            .spawn();
30    }
31    pub fn browse(&self, query: &str) {
32        let script = format!("Application(\"{}\").browse(\"{}\");", self.get_app_name(), query);
33        let mut _command = std::process::Command::new("/usr/bin/osascript")
34            .args(["-l", "JavaScript", "-e", script.as_str()])
35            .spawn();
36    }
37    pub fn set_theme(&self, theme: &str) {
38        let script = format!("Application(\"{}\").setTheme(\"{}\");", self.get_app_name(), theme);
39        let mut _command = std::process::Command::new("/usr/bin/osascript")
40            .args(["-l", "JavaScript", "-e", script.as_str()])
41            .spawn();
42    }
43    pub fn reload(&self, workflow: &str) {
44        let script = format!("Application(\"{}\").reloadWorkflow(\"{}\");", self.get_app_name(), workflow);
45        let mut _command = std::process::Command::new("/usr/bin/osascript")
46            .args(["-l", "JavaScript", "-e", script.as_str()])
47            .spawn();
48    }
49    pub fn trigger(&self, p1: &str, p2: &str) {
50        let script = format!("Application(\"{}\").runTrigger(\"{}\",\"{}\");", self.get_app_name(), p1, p2);
51        let mut _command = std::process::Command::new("/usr/bin/osascript")
52            .args(["-l", "JavaScript", "-e", script.as_str()])
53            .spawn();
54    }
55    pub fn set_config(&self, bundle: &str, query: &str, query2: &str) {
56        let script = format!("\
57            tell application id \"{}\"
58                 set configuration \"{}\" to value \"{}\" in workflow \"{}\"
59            end
60        ", self.get_app_name(), query, query2, bundle);
61        let mut _command = std::process::Command::new("/usr/bin/osascript")
62            .args(["-e", script.as_str()])
63            .spawn();
64    }
65    pub fn remove_config(&self, bundle: &str, query: &str) {
66        let script = format!("\
67            tell application id \"{}\"
68                 remove configuration \"{}\" in workflow \"{}\"
69            end
70        ", self.get_app_name(), query, bundle);
71        let mut _command = std::process::Command::new("/usr/bin/osascript")
72            .args(["-e", script.as_str()])
73            .spawn();
74    }
75    pub fn get_app_name(&self) -> &str {
76        "com.runningwithcrayons.Alfred"
77    }
78}
79
80#[test]
81fn test_alfred_search() {
82    Alfred::init().browse("c");
83    Alfred::init().action("c");
84}
85
86#[test]
87fn test_set_config() {
88    Alfred::init().set_config("com.christ.alfred.rust.demo", "test_rust_keu", "ccc")
89}
90
91#[test]
92fn test_remove_config() {
93    Alfred::init().remove_config("com.christ.alfred.rust.demo", "test_rust_keu")
94}
95pub trait AlfredEnv {
96    fn get_preference_path(&self) -> String;
97    fn get_preference_hash_path(&self) -> String;
98    fn get_theme(&self) -> String;
99    fn get_version(&self) -> String;
100    fn get_version_build(&self) -> String;
101    fn get_workflow_bundle_id(&self) -> String;
102    fn get_workflow_cache_path(&self) -> String;
103    fn get_workflow_data_path(&self) -> String;
104    fn get_workflow_name(&self) -> String;
105    fn is_debug_mode(&self) -> bool;
106    fn get_workflow_uuid(&self) -> String;
107    fn get_workflow_version(&self) -> String;
108    fn get_theme_background(&self) -> String;
109    fn get_theme_selection_background(&self) -> String;
110    fn get_theme_subtext(&self) -> String;
111}
112
113impl AlfredEnv for Alfred {
114    fn get_theme_subtext(&self) -> String {
115        return std::env::var("alfred_theme_subtext").unwrap_or_default();
116    }
117    fn get_preference_path(&self) -> String {
118        return std::env::var("alfred_preferences").unwrap_or_default();
119    }
120
121    fn get_preference_hash_path(&self) -> String {
122        return std::env::var("alfred_preferences_localhash").unwrap_or_default();
123    }
124
125    fn get_theme(&self) -> String {
126        return std::env::var("alfred_theme").unwrap_or_default();
127    }
128    fn get_version(&self) -> String {
129        return std::env::var("alfred_version").unwrap_or_default();
130    }
131    fn get_version_build(&self) -> String {
132        return std::env::var("alfred_version_build").unwrap_or_default();
133    }
134
135    fn get_workflow_bundle_id(&self) -> String {
136        return std::env::var("alfred_workflow_bundleid").unwrap_or_default();
137    }
138    fn get_workflow_cache_path(&self) -> String {
139        return std::env::var("alfred_workflow_cache").unwrap_or_default();
140    }
141    fn get_workflow_data_path(&self) -> String {
142        return std::env::var("alfred_workflow_data").unwrap_or_default();
143    }
144    fn get_workflow_name(&self) -> String {
145        return std::env::var("alfred_workflow_name").unwrap_or_default();
146    }
147    fn is_debug_mode(&self) -> bool {
148        return std::env::var("alfred_debug").unwrap_or_default().eq("1");
149    }
150    fn get_workflow_uuid(&self) -> String {
151        return std::env::var("alfred_workflow_uid").unwrap_or_default();
152    }
153
154    fn get_workflow_version(&self) -> String {
155        return std::env::var("alfred_workflow_version").unwrap_or_default();
156    }
157
158    fn get_theme_background(&self) -> String {
159        return std::env::var("alfred_theme_background").unwrap_or_default();
160    }
161    fn get_theme_selection_background(&self) -> String {
162        return std::env::var("alfred_theme_selection_background").unwrap_or_default();
163    }
164}