I’m unable to simply add the artwork to a track. I know this has been a widespread problem plaguing the scripters for ages but usually that revolved around embedding the artwork itself rather than making its element available for such embedding. The script reports of the existence and absence at the same time. How’s that even possible?
Select any track without a cover in iTunes and run these simple few lines of script to see what I’m talking about and take a glance at the screenshot of how it looks at https://i.imgur.com/RCQ85Po.png.
tell application "iTunes"
set theTrack to the first item of (get selection)
count theTrack's artworks
if exists theTrack's artwork 1 then get properties of theTrack's artwork 1
end tell
Creating a new artwork object is not possible with traditional means, i.e. make new artwork. Is there a workaround?
I don’t understand what do you mean. On my Catalina OSX your script returns the count and properties as expected:
tell application "Music"
set theTrack to item 1 of (get selection)
set theCount to count theTrack's artworks
if exists theTrack's artwork 1 then set theProperties to get properties of theTrack's artwork 1
end tell
return {theCount, theProperties}
As is, your code sample works without error on my system, however, a track without an image still has a limited artwork property; i.e. “{class:artwork}” is the entire entry. This should detect tracks without visible art:
set devoid to {}
tell application "iTunes"
repeat with counter from 1 to count tracks
tell track counter to try
artwork 1's format
on error
set devoid's end to its name
end try
end repeat
end tell
devoid
Thanks, I tried that, however, even this script implies there should be an artwork object. Running it with error checks still produces Parameter error -50, because when there’s no artwork it errors instead of returning an empty list. Parameter error -50 is the error of interoperability between AppleScript or the app it targets and the OS. At this point I’m convinced it’s either a bug in iTunes or the macOS Mavericks itself.
Not all applications provide the property properties. Especially, early versions of programs. If the following script returns the properties on the Maverics correctly, it means that there is no any zombie or bug. It’s just that the developer didn’t provide the property properties.
tell application "iTunes"
set theTrack to item 1 of (get selection)
set theCount to count theTrack's artworks
if exists theTrack's artwork 1 then
tell theTrack's artwork 1 to set theProperties to {class, data, downloaded, format, kind}
end if
end tell
One tip from me: you can’t add new artwork programatically (without GUI scripting). But you can replace existing artwork just fine:
set newData to read (choose file of type "public.image") as JPEG picture
tell application "iTunes"
set theTrack to item 1 of (get selection)
set theCount to count theTrack's artworks
if exists theTrack's artwork 1 then
tell theTrack's artwork 1 to set theProperties to {class, data, downloaded, format, kind}
tell theTrack's artwork 1 to set data to newData
end if
end tell
This gives some ideas. For example, keeping 1 template track with 1 (or more) embedded artwork(s) or changing the artwork of the track depending on some criteria.
Well, I entered mp4art in Terminal to see what happens. Nothing good happened. I should probably install it. But I decided not to do this, because your script is incomplete, untested and looks like spam.
“Not all applications provide” but iTunes certainly does, and its Dictionary in Mavericks defines the property properties. However, any scripting statement directed at the artwork 1 object fails with error -50 if it hasn’t been added except for commands dealing with artworks as a list (such as count) which usually is of a little practical value or checking the existence with exist. Also, the fact that it’s even impossible to create a new artwork element is the evidence that there’s something wrong with its scriptable implementation as of Mavericks.
And if it’s the case then the only way is to resort to a shell utility such ffmpeg or similar one, like Fredrick71 is suggesting. In my case, the file format is AIFF, not mp4 or its derivatives.
Defines the property properties of what object: of iTunes, of playlist, of track, of artwork?
Then, the dictionary of some application is one text, which can contain errors (instead of “dog” - “dag”). It is more useful to check real object model of your iTunes with appropriate application as Script Debugger. I have not Maverics to search further.
Wordless remarks are of little use. It was more interesting to get an answer whether the last script I proposed works on Maverics, or at least what kind of error it gives.
I’m afraid I didn’t quite understand you. iTunes is not “some application”. Its Dictionary is quite compelling and feature rich, on par with that of Mail, iCal and iWork, it’s traditionally one of the most elaborately scriptable applications. It seems you don’t get the part of my OP about error -50.
If we take your code with the control flow in the form of the conditional statement
the (do actions) part always fails with Parameter error -50 even if the check is passed. In other words, it generates the error -50 the check regardless. If you refer to artworks or every artwork then it always returns an empty list unless a cover art is added manually prior to running a code involving artworks.
I’m looking for the most straightforward solution available without the need of extra steps. If it’s not possible then I better off adding it manually. The goal of adding it programmatically was to include it in one of my scripts.
NB If you assume I’m a novice: of course, I made sure of the existence of the property property of the artwork object in Dictionary.
NB If any of you haven’t run into the problem it means that you’re on a newer or the newest macOS, which, in turn, means they at last fixed it.
The mp4art shell commands are interesting, Fredrik71. Another method for simply adding/replacing images to a target track would be:
tell application "iTunes" to set track 1's artwork 1's data to read my (choose file of type "JPG") as «class data»
Perhaps, however, if it’s a bug, it could just as easily be the application version, and you don’t appear to be running the latest iTunes for Mavericks. Unless you have a reason to avoid the later version, it might be worth testing the upgrade.
I know about XLD, been using it for years. However, I recently started using its CLI version for automated tasks too, and the problem is it gets to embed the cover art sporadically even if the image with a predefined naming scheme exists in the same folder, and I need a stable solution. Unless the developer adds an option or creates a dedicated shell command to embed artwork all that’s left - other shell utilities, like ffmpeg I’m learning currently.
I can update to iTunes 12.6 on Mavericks but I don’t want because I don’t like it. Although I have no choice but to use iTunes 12.9 on Mojave, so I’m kind of using it, and no Parameter error -50 there. I’d rather prefer to stay with iTunes 11.4, and if the bug exists in iTunes 12.6 then I’ll have a hard time reverting to 11.4.