Wednesday, December 8, 2021

#1 2021-10-17 08:23:27 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 917

iTunes Artwork to NSWindow

Example of a script that take data from iTunes (Artwork Cover) and display it in new NSWindow.

In my example I have the movie "Home alone" (in swedish its Ensam hemma).
I ask iTunes to get the data and use ASObjC to convert it to NSImage format.
To be able to display it in NSWindow.

Applescript:

use framework "Foundation"
use framework "AppKit"
use scripting additions

-- Get the artwork from the track with name "Ensam hemma" (eng. Home alone)
tell application "iTunes"
   set theTrack to track "Ensam hemma"
   set artworkData to data of artwork 1 of theTrack
end tell

-- Create NSImage from iTunes data
set theData to (current application's NSArray's arrayWithObject:artworkData)'s firstObject()'s |data|()
set theImage to current application's NSImage's alloc()'s initWithData:theData
set {theWidth, theHeight} to {theImage's |size|()'s width, theImage's |size|()'s height}

-- Run the handler
its imageViewData:theImage windowSize:{theWidth, theHeight}

on imageViewData:theImageData windowSize:{theWidth, theHeight}
   set arguments to {imageData:theImageData, width:theWidth, height:theHeight}
   
   if my NSThread's isMainThread() as boolean then
       set arguments to current application's NSDictionary's dictionaryWithDictionary:arguments
       my performImageView:arguments
   else
       my performSelectorOnMainThread:"performImageView:" withObject:arguments waitUntilDone:true
   end if
end imageViewData:windowSize:

on performImageView:arguments
   set {width, height} to {arguments's valueForKey:"width", arguments's valueForKey:"height"}
   -- Create the image view.
   set theImageView to createImageView(10, 20, (width as integer) - 20, (height as integer) - 20, arguments's valueForKey:"imageData")
   -- Create the window.
   set theWindow to createWindowWithRect(0, 0, width, height)
   theWindow's contentView()'s addSubview:theImageView
   theWindow's setTitle:"Example with ImageView..."
   theWindow's |center|()
   theWindow's makeKeyAndOrderFront:me
end performImageView:

on createImageView(x, y, width, height, imageView)
   set imageViewSize to current application's NSMakeRect(x, y, width, height)
   set theImageView to current application's NSImageView's alloc()'s initWithFrame:imageViewSize
   theImageView's setImage:imageView
   return theImageView
end createImageView

on createWindowWithRect(x, y, width, height)
   set windowSize to current application's NSMakeRect(x, y, width, height)
   set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer)
   set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:true
   return theWindow
end createWindowWithRect

Here is a script to automate the output of artwork from media kind: movie from iTunes library.
It will get the name of the track and output the pictures to desktop.

Applescript:

use framework "Foundation"
use scripting additions

tell application "iTunes"
   repeat with anTrack in tracks
       if (anTrack's media kind) = movie then
           set theName to name of anTrack
           set artworkData to data of artwork 1 of (track theName)
           set theData to (current application's NSArray's arrayWithObject:artworkData)'s firstObject()'s |data|()
           (theData's writeToFile:(POSIX path of (path to desktop) & theName & ".jpg") atomically:true)
       end if
   end repeat
end tell

Last edited by Fredrik71 (2021-10-17 01:29:58 pm)


if you are the expert, who will you call if its not your imagination.


Filed under: iTunes, nswindow

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)