Thursday, February 25, 2021

#1 2020-11-01 11:52:09 am

MrCee
Member
Registered: 2016-07-01
Posts: 12

Query joined/matching NSArray's. Return lookup value at same index

I'm looking for ways to query/filter/lookup/update all music ID3 tags from Music.app (iTunes). Essentially I'd like to be able to query all arrays like a database without a repeat loop, working with 20+ arrays which all have this same count/index.

I'm thinking that coercing back to AppleScript as text & list should come last, after I have completed every evaluation and updated arrays before I write to file.

If anyone can provide some suggestions as how to go about this while remaining optimised as ASOC NSArray (without repeat loops if possible) that would be greatly appreciated.

In my code below, I wasn't sure if I needed to first join arrays, using one as the predicate to return the value of other, or if there is a more straight forward way. As an example, I'm trying to find 'theID' of all files where 'thePosixPath' is NSNull.

Applescript:

tell application "Music"
   set theID to current application's NSArray's arrayWithArray:(get persistent ID of every file track in library playlist 1)
   set theLocation to current application's NSArray's arrayWithArray:(get location of every file track in library playlist 1)
   set theName to current application's NSArray's arrayWithArray:(get name of every file track in library playlist 1)
   set theArtist to current application's NSArray's arrayWithArray:(get artist of every file track in library playlist 1)
end tell

--create custom arrays    
set thePosixPath to theLocation's valueForKey:"path"
set theParentFolder to theLocation's valueForKeyPath:"URLByDeletingLastPathComponent.lastPathComponent"

-- work in progress, trying to join a few NSArrays....
set joinedArray to {theID:theID, thePosixPath:thePosixPath, theName:theName, theArtist:theArtist}

its itemsWhose:"thePosixPath" isEqualTo:"" inList:joinedArray

on itemsWhose:theProperty isEqualTo:theValue inList:theList
   set theArray to current application's NSArray's arrayWithArray:theList
   set thePred to current application's NSPredicate's predicateWithFormat:"%K = %@" argumentArray:{theProperty, theValue}
   return (theArray's filteredArrayUsingPredicate:thePred)
end itemsWhose:isEqualTo:inList:

Last edited by MrCee (2020-11-01 11:56:40 am)

Offline

 

#2 2020-11-03 06:59:42 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6565

Re: Query joined/matching NSArray's. Return lookup value at same index

You'll have to build an array of dictionaries, and that's going to require a repeat loop*. Assuming the data doesn't change too often, it might make sense to do that and write it to a file, assuming you want to search it regularly.

* There are some shortcuts in my BridgePlus script library, using columnsToRowsIn:error: and subarraysIn:asDictionariesUsingLabels:error:, which would do it quite fast.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-11-22 09:39:06 pm

technomorph
Member
Registered: 2017-12-14
Posts: 178

Re: Query joined/matching NSArray's. Return lookup value at same index

You may want to look into using the ITLibrary Framework.
it's super fast.

https://developer.apple.com/documentati … guage=objc

You can get all the Media Items from the library.
Then it's recommended to filter their tracks array example to only "Songs"

Applescript:

-(void)loadAllTracks {
NSArray* tracks = self.iTunesLib.allMediaItems; // <- NSArray of ITLibMediaItem
NSPredicate *songPred = [NSPredicate predicateWithFormat:@"mediaKind == %d", 2];
self.allTracks = [tracks filteredArrayUsingPredicate:songPred];
}

You can then apply any predicate to the allTracks array.
You now have a much smaller filteredTracks array. 
Which your going to have to enumerate.

The ITLibMediaEntity Class has a function (private?):

Applescript:

- (void)enumerateValuesForProperties:(id)arg1 usingBlock:(CDUnknownBlockType)arg2;

I use it (code in OBJC like this for each track):

Applescript:

-(NSMutableDictionary*)onlyTheseProperties:(NSArray*)onlyProps forMediaItem:(id)aTrack {
ITLibMediaItem* aMediaItem = aTrack;

__block NSMutableDictionary* mediaDict = [NSMutableDictionary new];
NSSet* onlyProperties = [NSSet setWithArray:onlyProps];
[aMediaItem enumerateValuesForProperties:onlyProperties
usingBlock:^(NSString * _Nonnull property, id _Nonnull value, BOOL * _Nonnull stop) {
NSString* curKey = property;
id curValue = value;
[mediaDict setValue:curValue forKey:curKey];
}];
return mediaDict;
}

The properties and Keys can be found here:
https://developer.apple.com/documentati … guage=objc

Offline

 

#4 2020-11-22 09:49:42 pm

technomorph
Member
Registered: 2017-12-14
Posts: 178

Re: Query joined/matching NSArray's. Return lookup value at same index

PS I'm not sure if it's just using the ITLibrary Framework or just OS X security scopes.
But I had difficulty accessing the location.  As I believe it's a security scoped book mark.
I had to do things like setting up sandbox privileges and then access like this:

Applescript:

-(NSURL*)libLocationOfMediaItem:(id)aMediaItem {
ITLibMediaItem* aITLibMITrack = aMediaItem;

NSURL* testURL1A = aITLibMITrack.location;
[testURL1A startAccessingSecurityScopedResource];
NSLog(@"testURL1A is :%@", testURL1A);

NSString* tempLocSting = [[testURL1A path] copy];
NSLog(@"tempLocSting is :%@", tempLocSting);

NSURL* testURL1A2 = [NSURL URLWithString:tempLocSting];
[testURL1A stopAccessingSecurityScopedResource];

NSLog(@"testURL1A2 after security is :%@", testURL1A2);
return testURL1A2 ;
}

it was finickey.  I found the best way was to use this (again a property not publicly documented)

Applescript:

-(NSURL*)libLocationOfMediaItem:(id)aMediaItem {
ITLibMediaItem* aITLibMITrack = aMediaItem;

NSURL* testURL3A = [aITLibMITrack valueForKey:@"URL"];
NSLog(@"testURL3A is :%@", testURL3A);

return testURL3A ;
}

But also I'm not sure how that will work out if the returned value is NULL on the URL property.

Offline

 

#5 2020-11-22 09:52:21 pm

technomorph
Member
Registered: 2017-12-14
Posts: 178

Re: Query joined/matching NSArray's. Return lookup value at same index

Shane Stanley wrote:


* There are some shortcuts in my BridgePlus script library, using columnsToRowsIn:error: and subarraysIn:asDictionariesUsingLabels:error:, which would do it quite fast.



When I was doing straight appleScript OBJC stuff. 
Shane's BridgePlus script library and the Functions he listed were super valuable and easy to use!!!

Offline

 

#6 2020-11-22 10:27:32 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6565

Re: Query joined/matching NSArray's. Return lookup value at same index

Interesting about using the ITLibrary framework. The documents say you app must be code-signed to access it, and that was indeed the case when I last tried it, which was probably when it came out (10.13?). But trying just now on 10.15, that seems no longer to be the case. Even for location:

Applescript:

use framework "Foundation"
use framework "iTunesLibrary"
use scripting additions
set {theLib, theError} to current application's ITLibrary's libraryWithAPIVersion:"1.0" |error|:(reference)
set theFile to theLib's allPlaylists()'s firstObject()'s |items|()'s firstObject()'s location() as «class furl»

The ITLibMediaEntity Class has a function (private?):



It's not private (it's in ITLibMediaEntity), but being block-based it's useless from AppleScript.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)