macos-open 0.0.3

A clone of macOS /usr/bin/open, in the programmatically way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! A simple way to use /usr/bin/open features in the programmatically way.
//! This is a wrapper around Core Foundation, Launch Services and File Metadata frameworks.

extern crate core_foundation;
extern crate core_foundation_sys;
extern crate fast_escape;
#[macro_use]
extern crate fast_fmt;
extern crate launch_services;
extern crate void;
extern crate url;

use core_foundation::array::CFArray;
use core_foundation::base::TCFType;
use core_foundation::string::{CFString, CFStringRef};
use core_foundation::url::{CFURLRef, CFURL};
use core_foundation_sys::base::{kCFAllocatorDefault, CFAllocatorRef};
use launch_services::{
    application_urls_for_bundle_identifier, application_urls_for_url, can_url_accept_url,
    default_application_url_for_url, open_from_url_spec, open_url,
    LSAcceptanceFlags, LSLaunchURLSpec, LSRolesMask,
};

pub use launch_services::LSLaunchFlags;

use std::io::{Error, ErrorKind, Result};
use std::path::{Path, PathBuf};

pub use core_foundation_sys::base::OSStatus;

use fast_escape::Escaper;
use file_metadata::mditem::attributes;
use file_metadata::mdquery::{MDQuery, MDQueryOptionFlags};
use void::ResultVoidExt;

use url::{Url, ParseError};

#[link(name = "CoreServices", kind = "framework")]
extern "C" {
    fn CFURLCreateWithString(
        allocator: CFAllocatorRef,
        urlString: CFStringRef,
        baseURL: CFURLRef,
    ) -> CFURLRef;
}

/// A type implementing this trait can may be transformed in a CFURL and so opened.
pub trait Openable {
    /// Transform this type in a CFURL (Core Foundation URL).
    fn into_openable(&self) -> Option<CFURL>;
}

fn url(value: &str) -> Option<CFURL> {
    match Url::parse(value) {
        Ok(u) => _url(&u.into_string()),
        Err(ParseError::RelativeUrlWithoutBase) => {
            let path = Path::new(value);
            if path.exists() {
                Openable::into_openable(&path)
            } else {
                _url(value)
            }
        },
        Err(_) => _url(value),
    }
}

fn _url(value: &str) -> Option<CFURL> {
    let url = CFString::new(value);

    let ptr = unsafe {
        CFURLCreateWithString(
            kCFAllocatorDefault,
            url.as_concrete_TypeRef(),
            std::ptr::null(),
        )
    };

    if ptr.is_null() {
        None
    } else {
        Some(unsafe { TCFType::wrap_under_create_rule(ptr) })
    }
}

impl Openable for &str {
    fn into_openable(&self) -> Option<CFURL> {
        url(self)
    }
}

impl Openable for str {
    fn into_openable(&self) -> Option<CFURL> {
        url(self)
    }
}

impl Openable for &String {
    fn into_openable(&self) -> Option<CFURL> {
        url(self)
    }
}

impl Openable for String {
    fn into_openable(&self) -> Option<CFURL> {
        url(self)
    }
}

impl Openable for &Path {
    fn into_openable(&self) -> Option<CFURL> {
        if self.is_relative() {
            match self.canonicalize() {
                Ok(path) => Openable::into_openable(&path),
                Err(_) => None,
            }
        } else {
            CFURL::from_path(self, self.is_dir())
        }
    }
}

impl Openable for Path {
    fn into_openable(&self) -> Option<CFURL> {
        if self.is_relative() {
            match self.canonicalize() {
                Ok(path) => Openable::into_openable(&path),
                Err(_) => None,
            }
        } else {
            CFURL::from_path(self, self.is_dir())
        }
    }
}

impl Openable for &PathBuf {
    fn into_openable(&self) -> Option<CFURL> {
        if self.is_relative() {
            match self.canonicalize() {
                Ok(path) => Openable::into_openable(&path),
                Err(_) => None,
            }
        } else {
            CFURL::from_path(self, self.is_dir())
        }
    }
}

impl Openable for PathBuf {
    fn into_openable(&self) -> Option<CFURL> {
        if self.is_relative() {
            match self.canonicalize() {
                Ok(path) => Openable::into_openable(&path),
                Err(_) => None,
            }
        } else {
            CFURL::from_path(self, self.is_dir())
        }
    }
}

/// A type implementing this trait can may be transformed in a CFArray<CFURL> and so opened.
pub trait MultiOpenable {
    /// Transform this type in a CFArray (Core Foundation array) of CFURL (Core Foundation URL).
    fn into_openable(&self) -> Option<CFArray<CFURL>>;
}

macro_rules! def_multiopenable_vec {
    ( $type:ty ) => {
        impl MultiOpenable for Vec<$type> {
            fn into_openable(&self) -> Option<CFArray<CFURL>> {
                let mut res: Vec<CFURL> = Vec::new();

                for el in self.iter() {
                    match Openable::into_openable(el) {
                        None => return None,
                        Some(url) => res.push(url),
                    };
                }

                Some(CFArray::<CFURL>::from_CFTypes(&res[..]))
            }
        }

        impl MultiOpenable for &[$type] {
            fn into_openable(&self) -> Option<CFArray<CFURL>> {
                let mut res: Vec<CFURL> = Vec::new();

                for el in self.iter() {
                    match Openable::into_openable(el) {
                        None => return None,
                        Some(url) => res.push(url),
                    };
                }

                Some(CFArray::<CFURL>::from_CFTypes(&res[..]))
            }
        }

        impl MultiOpenable for [$type] {
            fn into_openable(&self) -> Option<CFArray<CFURL>> {
                let mut res: Vec<CFURL> = Vec::new();

                for el in self.iter() {
                    match Openable::into_openable(el) {
                        None => return None,
                        Some(url) => res.push(url),
                    };
                }

                Some(CFArray::<CFURL>::from_CFTypes(&res[..]))
            }
        }
    };
}

macro_rules! def_multiopenable_type {
    ( $type:ty ) => {
        impl MultiOpenable for $type {
            fn into_openable(&self) -> Option<CFArray<CFURL>> {
                let v = vec![Openable::into_openable(self)?];
                Some(CFArray::<CFURL>::from_CFTypes(&v[..]))
            }
        }
    };
}

macro_rules! def_multiopenable {
    ( $type:ty ) => {
        def_multiopenable_vec!($type);
        def_multiopenable_type!($type);
    };
}

def_multiopenable!(&str);
def_multiopenable_vec!(&String);
def_multiopenable_type!(str);
def_multiopenable!(String);
def_multiopenable!(&Path);
def_multiopenable_vec!(&PathBuf);
def_multiopenable_type!(Path);
def_multiopenable!(PathBuf);

/// Open an Openable value with default handler
pub fn open<T: Openable + ?Sized>(url: &T) -> Result<Option<PathBuf>> {
    if let Some(openable) = Openable::into_openable(url) {
        match open_url(&openable) {
            Ok(path) => Ok(path.to_path()),
            Err(code) => Err(Error::new(
                ErrorKind::Other,
                format!("return code {}", code),
            )),
        }
    } else {
        Err(Error::new(ErrorKind::Other, "Provided url is not openable"))
    }
}

#[inline]
fn remap_app(app: Option<&Path>) -> Result<Option<CFURL>> {
    if let Some(app) = app {
        match CFURL::from_path(app, true) {
            None => Err(Error::new(
                ErrorKind::Other,
                "Provided app url is not valid",
            )),
            res => Ok(res),
        }
    } else {
        Ok(None)
    }
}

#[inline]
fn remap_multiopenable<T: MultiOpenable + ?Sized>(
    urls: Option<&T>,
) -> Result<Option<CFArray<CFURL>>> {
    if let Some(urls) = urls {
        match MultiOpenable::into_openable(urls) {
            None => Err(Error::new(ErrorKind::Other, "Provided urls are not valid")),
            res => Ok(res),
        }
    } else {
        Ok(None)
    }
}

/// Open the app if no urls provided, open the urls in app if both provided and open urls in
/// default handlers if no app is provided.
pub fn open_complex<T: MultiOpenable + ?Sized>(
    app: Option<&Path>,
    urls: Option<&T>,
    flags: LSLaunchFlags,
) -> Result<Option<PathBuf>> {
    let spec = LSLaunchURLSpec {
        app: remap_app(app)?,
        urls: remap_multiopenable(urls)?,
        flags,
        ..Default::default()
    };

    match open_from_url_spec(spec) {
        Ok(path) => Ok(path.to_path()),
        Err(code) => Err(Error::new(
            ErrorKind::Other,
            format!("return code {}", code),
        )),
    }
}

/// Get all the app that can handle the given scheme
pub fn apps_for_scheme(scheme: &str) -> Option<Vec<PathBuf>> {
    let scheme = Openable::into_openable(&format!("{}://", scheme))?;
    Some(
        application_urls_for_url(&scheme, LSRolesMask::VIEWER)?
            .iter()
            .filter_map(|v| v.to_path())
            .collect::<Vec<_>>(),
    )
}

/// Get the default app handler for defined scheme
pub fn app_for_scheme(scheme: &str) -> Option<PathBuf> {
    let scheme = Openable::into_openable(&format!("{}://", scheme))?;
    match default_application_url_for_url(&scheme, LSRolesMask::VIEWER) {
        Ok(url) => url.to_path(),
        Err(_) => None,
    }
}

/// Get all the app's paths matching the given bundle identifier
pub fn apps_for_bundle_id(bundle_id: &str) -> Option<Vec<PathBuf>> {
    let bundle_id = CFString::new(bundle_id);
    match application_urls_for_bundle_identifier(&bundle_id) {
        Ok(apps) => Some(apps.iter().filter_map(|v| v.to_path()).collect()),
        Err(_) => None,
    }
}

/// Get first app's paths matching the given bundle identifier
pub fn app_for_bundle_id(bundle_id: &str) -> Option<PathBuf> {
    let mut apps = apps_for_bundle_id(bundle_id)?;
    if apps.is_empty() {
        None
    } else {
        Some(apps.remove(0))
    }
}

const MQ_STRING_SPECIAL_CHARS: [char; 4] = ['?', '*', '\\', '"'];

/// Get all the app's paths matching the given name in current locale
pub fn apps_for_name(app_name: &str) -> Option<Vec<PathBuf>> {
    let mut query_string = String::new();
    let escaper: Escaper<&[char]> = Escaper::new('\\', &MQ_STRING_SPECIAL_CHARS);
    fwrite!(
        &mut query_string,
        "kMDItemContentTypeTree == \"com.apple.application\"c && kMDItemDisplayName == \"",
        app_name.transformed(escaper),
        "\"cd"
    )
    .void_unwrap();
    let query_cfstring = CFString::new(&query_string);
    let query = MDQuery::new(query_cfstring, None, None)?;
    query.execute(MDQueryOptionFlags::SYNC | MDQueryOptionFlags::ALLOW_FS_TRANSLATION);
    query.stop();

    let res = query
        .iter()
        .filter_map(|v| {
            v.get(attributes::Path)
                .map(|a| PathBuf::from(a.to_string()))
        })
        .collect::<Vec<_>>();
    if res.len() == 0 {
        None
    } else {
        Some(res)
    }
}

/// Get first app's paths matching the given name in current locale
pub fn app_for_name(name: &str) -> Option<PathBuf> {
    let mut apps = apps_for_name(name)?;
    if apps.is_empty() {
        None
    } else {
        Some(apps.remove(0))
    }
}

/// Check if the app can handle the given url
pub fn app_accept_url<T: Openable + ?Sized>(app: &Path, url: &T) -> bool {
    if let Some(app) = CFURL::from_path(app, true) {
        match Openable::into_openable(url) {
            None => return false,
            Some(url) => match can_url_accept_url(&url, &app, LSRolesMask::VIEWER, LSAcceptanceFlags::DEFAULT) {
                Err(_) => false,
                Ok(res) => res
            },
        }
    } else {
        false
    }
}

/// Check if the app can handle all the given urls
pub fn app_accept_urls<T: MultiOpenable + ?Sized>(app: &Path, urls: &T) -> bool {
    if let Some(app) = CFURL::from_path(app, true) {
        match MultiOpenable::into_openable(urls) {
            None => return false,
            Some(urls) => !urls
                .iter()
                .map(|v| {
                    match can_url_accept_url(
                        &*v,
                        &app,
                        LSRolesMask::VIEWER,
                        LSAcceptanceFlags::DEFAULT,
                    ) {
                        Err(_) => false,
                        Ok(res) => res,
                    }
                })
                .any(|v| !v),
        }
    } else {
        false
    }
}

/// Get all the apps matching the name in current locale that can open the given urls
pub fn apps_for_name_accepting_urls<T: MultiOpenable + ?Sized>(
    name: &str,
    urls: &T,
) -> Option<Vec<PathBuf>> {
    let res: Vec<PathBuf> = apps_for_name(name)?
        .into_iter()
        .filter(|v| app_accept_urls(v, urls))
        .collect();

    if res.is_empty() {
        None
    } else {
        Some(res)
    }
}

/// Get the first app matching the name in current locale that can open the given urls
pub fn app_for_name_accepting_urls<T: MultiOpenable + ?Sized>(
    name: &str,
    urls: &T,
) -> Option<PathBuf> {
    let mut apps = apps_for_name_accepting_urls(name, urls)?;

    if apps.is_empty() {
        None
    } else {
        Some(apps.remove(0))
    }
}

/// Get all the apps matching the bundle identifier that can open the given urls
pub fn apps_for_bundle_id_accepting_urls<T: MultiOpenable + ?Sized>(
    bundle_id: &str,
    urls: &T,
) -> Option<Vec<PathBuf>> {
    let res: Vec<PathBuf> = apps_for_bundle_id(bundle_id)?
        .into_iter()
        .filter(|v| app_accept_urls(v, urls))
        .collect();

    if res.is_empty() {
        None
    } else {
        Some(res)
    }
}

/// Get the first app matching the bundle identifier that can open the given urls
pub fn app_for_bundle_id_accepting_urls<T: MultiOpenable + ?Sized>(
    bundle_id: &str,
    urls: &T,
) -> Option<PathBuf> {
    let mut apps = apps_for_bundle_id_accepting_urls(bundle_id, urls)?;

    if apps.is_empty() {
        None
    } else {
        Some(apps.remove(0))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_open_default() {
        assert!(open("https://www.google.com/").is_ok());
    }

    #[test]
    fn test_open_default_non_ascii() {
        assert!(open("http://github.com?dummy_query1=0&dummy_query2=nonascii").is_ok());
    }

    #[test]
    fn test_open_complex_safari() {
        assert!(open_complex(
            Some(Path::new("/Applications/Safari.app")),
            Some(&["https://news.ycombinator.com/", "https://www.google.com/"][..]),
            LSLaunchFlags::DEFAULTS,
        ).is_ok());
    }

    #[test]
    fn test_get_safari_by_bundle_id() {
        assert!(apps_for_bundle_id("com.apple.safari").is_some());
        assert!(app_for_bundle_id("com.apple.safari").is_some());
    }

    #[test]
    fn test_get_safari_by_name_accepting_google_url() {
        assert!(app_for_name_accepting_urls("Safari", &["http://www.google.com/"][..]).is_some());
    }

    #[test]
    fn test_get_safari_by_bundle_id_accepting_google_url() {
        assert!(app_for_bundle_id_accepting_urls("com.google.chrome", &["http://www.google.com/"][..]).is_some());
    }
}