X-Git-Url: https://git.r.bdr.sh/rbdr/lyricli/blobdiff_plain/44e7b4de4073e6dc25681bb2fa6977bf5869689a..e4117b21a5e9cfaa3278e9e25711aff363e2ac92:/src/sources/apple_music.rs diff --git a/src/sources/apple_music.rs b/src/sources/apple_music.rs index cb36f5c..6279a50 100644 --- a/src/sources/apple_music.rs +++ b/src/sources/apple_music.rs @@ -1,8 +1,15 @@ use std::ffi::CStr; use std::io::Result; -use cocoa::{base::nil, foundation::NSString}; -use objc::{class, msg_send, sel, sel_impl, runtime::Object}; +use cocoa::{ + base::{id, nil}, + foundation::NSString, +}; +use objc::{ + class, msg_send, + runtime::{Class, Object}, + sel, sel_impl, +}; use objc_id::Id; use crate::Track; @@ -18,7 +25,6 @@ impl AppleMusic { } impl LyricsSource for AppleMusic { - fn name(&self) -> String { "apple_music".to_string() } @@ -27,9 +33,19 @@ impl LyricsSource for AppleMusic { unsafe { let app: Id = { let cls = class!(SBApplication); - let bundle_identifier = NSString::alloc(nil).init_str("com.apple.Music"); - let app: *mut Object = msg_send![cls, applicationWithBundleIdentifier:bundle_identifier]; - Id::from_ptr(app) + let bundle_identifier = NSString::alloc(nil).init_str("com.apple.Music"); + + let workspace_class = Class::get("NSWorkspace").unwrap(); + let shared_workspace: id = msg_send![workspace_class, sharedWorkspace]; + let app_url: id = msg_send![shared_workspace, URLForApplicationWithBundleIdentifier:bundle_identifier]; + + if app_url != nil { + let app: *mut Object = + msg_send![cls, applicationWithBundleIdentifier:bundle_identifier]; + Id::from_ptr(app) + } else { + return None; + } }; if msg_send![app, isRunning] { @@ -44,11 +60,7 @@ impl LyricsSource for AppleMusic { let name = CStr::from_ptr(name_ptr).to_string_lossy().into_owned(); let artist = CStr::from_ptr(artist_ptr).to_string_lossy().into_owned(); - - return Some(Track { - name, - artist - }) + return Some(Track { name, artist }); } } } @@ -67,5 +79,3 @@ impl LyricsSource for AppleMusic { Ok(()) } } - -