Instantly share code, notes, and snippets.

Embed
What would you like to do?
using AppKit;
using Foundation;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using ScriptingBridge;
namespace NowPlayingTest
{
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
public AppDelegate()
{
}
public override void DidFinishLaunching(NSNotification notification)
{
// Insert code here to initialize your application
var center = NSDistributedNotificationCenter.DefaultCenter as NSNotificationCenter;
center.AddObserver(new NSString("com.swinsian.Swinsian-Track-Playing"), async obj =>
{
var dict = obj.UserInfo;
await Task.Run(() =>
{
Song song = new Song();
song.Album = dict["album"] as NSString;
song.Artist = dict["artist"] as NSString;
song.Title = dict["title"] as NSString;
var apath = dict["thumbnailPath"] as NSString;
song.AlbumArt = new NSImage(Path.GetDirectoryName(apath) + "/cover_512.png");
Song.LastPlayedSong = song;
dict.ToList().ForEach((item) =>
{
Debug.WriteLine($"{item.Key} : {item.Value}");
});
});
});
center.AddObserver(new NSString("com.apple.iTunes.playerInfo"), async obj =>
{
var dict = obj.UserInfo;
await Task.Run(() =>
{
Song song = new Song();
song.Album = dict["Album"] as NSString;
song.Artist = dict["Artist"] as NSString;
song.Title = dict["Name"] as NSString;
dict.ToList().ForEach((item) =>
{
Debug.WriteLine($"{item.Key} : {item.Value}");
});
//get album art
var itunes = SBApplication.FromBundleIdentifier("com.apple.iTunes");
var ctrack = itunes.ValueForKey(new NSString("currentTrack"));
var front_artwork = ctrack.ValueForKey(new NSString("artworks")) as SBElementArray;
if (front_artwork.Count > 0)
{
var aitem = front_artwork.Get()[0];
var raw_image = aitem.ValueForKey(new NSString("rawData")) as NSData;
song.AlbumArt = new NSImage(raw_image);
}
Song.LastPlayedSong = song;
Debug.WriteLine(ctrack.ToString());
});
});
}
public override void WillTerminate(NSNotification notification)
{
// Insert code here to tear down your application
}
}
}
using System;
using AppKit;
namespace NowPlayingTest
{
public class Song
{
public static Song LastPlayedSong;
public String Artist { get; set; }
public String Title { get; set; }
public String Album { get; set; }
public NSImage AlbumArt { get; set; }
}
}
using System;
using AppKit;
using Foundation;
using ObjCRuntime;
using ScriptingBridge;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;
namespace NowPlayingTest
{
public partial class ViewController : NSViewController
{
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Do any additional setup after loading the view
//change text
deheheButton.Title = "でへへ〜";
deheheButton.Activated += (sender, e) =>
{
var alert = new NSAlert();
alert.MessageText = "でへへ〜";
if (Song.LastPlayedSong != null)
{
var song = Song.LastPlayedSong;
alert.Icon = song.AlbumArt;
alert.InformativeText = $"{song.Title}\n{song.Artist}\n{song.Album}";
}
else
{
alert.InformativeText = "取得できなかった〜でへへ〜";
}
alert.RunSheetModal(this.View.Window);
};
}
public override NSObject RepresentedObject
{
get
{
return base.RepresentedObject;
}
set
{
base.RepresentedObject = value;
// Update the view, if already loaded.
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment