Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator/AppleScript: Change track's "media kind"

Hello there,


it seems that there's a limitation in AppleScript when it comes to change a iTunes track's media kind. Is there any workaround using a recorded action in Automator to get this done?


I tried to make a workflow-droplet, that


  • Adds a mp3-file to iTunes
  • Selects this file in the iTunes library
  • Opens the Information pane (cmd+I)
  • Goes to "Options"
  • Then choses "Audiobook" as media kind


It didn't work, but perhaps I made an mistake. Or is it just impossible to do jobs like this with recorded actions?


Thanks for your answers!

Peter

MacBook Pro (13-inch Mid 2010), Mac OS X (10.7.3)

Posted on May 2, 2012 9:29 AM

Reply
15 replies

Aug 29, 2012 4:02 PM in response to Burinsky

Hi,


This AppleScript script works well here, try this :

-----------------------------------------------------

setinputtochoose fileof type "public.mp3" withmultiple selections allowed

tellapplication "iTunes"

activate

setlibPlaylistto (firstplaylistwhosespecial kindisMusic)

reveallibPlaylist

repeatwithiininput

set dataID to database ID of (add i)

reveal (firsttrackoflibPlaylistwhosedatabase IDisdataID)

mychangeTrackMedia("Audiobook") --"Audiobook" as media kind (on english system)

endrepeat

endtell


onchangeTrackMedia(t)

tell application "System Events"

tell process "iTunes"

keystroke "i" usingcommand down

tell front window

tell tab group 1

click radio button 5

delay 0.1

click pop up button 2

delay 0.1

tell menu item t of menu 1 of pop up button 2 to if exists then

click

else

keystroke return

end if

end tell

click value of attribute "AXDefaultButton"

delay 0.3

end tell

end tell

endtell

endchangeTrackMedia

-----------------------------------------------------

Sep 1, 2012 8:29 AM in response to Jacques Rioux

Great! Your script works well for me.


But now I have problems to integrate it in a Automator application (or folder action?). Can you tell me what I have to do to


  1. make your script not asking for input files but using the files that are pulled to the Automator-application
  2. to reuse the files that where proceeded by your AppleScript in other Automator actions like change tracks info or change tracks options


In addition I am now looking for a script to embed a image as albumart to the proceeded files. I tried this, but got an error (something like "unknown object type", in German: "„iTunes“ hat einen Fehler erhalten: Unbekannter Objekttyp.")


----


tell application "iTunes"

set data of artwork 1 of (current track) to read file "Users:admin:Music:Cover:myartwork.png" as picture

end tell

Sep 1, 2012 10:38 AM in response to PeterPeterPeterPeter

PeterPeterPeterPeter wrote:


In addition I am now looking for a script to embed a image as albumart to the proceeded files. I tried this, but got an error (something like "unknown object type", in German: "„iTunes“ hat einen Fehler erhalten: Unbekannter Objekttyp.")

Use parentheses like this --> (read (file "Users:admin:Music:Cover:myartwork.png") as picture)


Important : the current track and the selected track are not always the same.



PeterPeterPeterPeter wrote:


Great! Your script works well for me.


But now I have problems to integrate it in a Automator application (or folder action?). Can you tell me what I have to do to


  1. make your script not asking for input files but using the files that are pulled to the Automator-application
  2. to reuse the files that where proceeded by your AppleScript in other Automator actions like change tracks info or change tracks options


Here is the script that you can use in Automator :

--------------------------

onrun {input}

set newTracks to {}

tell application "iTunes"

activate

setlibPlaylistto (firstplaylistwhosespecial kindisMusic)

reveal libPlaylist

repeat with i in input

set dataID to database ID of (add i)

set tTrack to (first track of libPlaylist whose database ID is dataID)

--set data of artwork 1 of tTrack to (read (file "Users:admin:Music:Cover:myartwork.png") as picture)

reveal tTrack

mychangeTrackMedia("Audiobook") --"Audiobook" as media kind (on english system)

set end of newTracks to tTrack

end repeat

end tell

returnnewTracks -- return the added tracks, to reuse the tracks in other Automator actions (iTunes only, because it's a track object, not a file path)

endrun


onchangeTrackMedia(t)

tell application "System Events"

tell process "iTunes"

keystroke "i" usingcommand down

tell front window

tell tab group 1

click radio button 5

delay 0.1

click pop up button 2

delay 0.1

tell menu item t of menu 1 of pop up button 2 to if exists then

click

else

keystroke return

end if

end tell

click value of attribute "AXDefaultButton"

delay 0.3

end tell

end tell

end tell

endchangeTrackMedia

--------------------------

Sep 2, 2012 10:54 AM in response to Jacques Rioux

Thank you, you already helped me a lot.


One thing that doesn't work for me is to add other iTunes actions after your script. In the log it says: "The action "Change tracks options" couldn't command the necessary data.". But in the results it shows that it knows the track:


{file track id 70050 of user playlist id 39328 of source id 76 of application "iTunes"}


In addition I am now looking for a action/script that just openes the information window (cmd + i) for the added tracks to manually insert the missing data.

Sep 2, 2012 11:22 AM in response to PeterPeterPeterPeter

Ok, I think I have a solution but still need a little help. I now put the other actions in front of your script. The workflow is looking like this:


  • Import files to iTunes
  • Change tracks information
  • Change tracks options
  • Your AppleScript


Can you tell me how to change the script if I don't want it to import files by itself but use the tracks imported and changed by the actions that where proceeded before the script?


(I already managed to make the script go to the information tab in the end. For some reason I can't edit my post above to delete the question ...)

Sep 2, 2012 3:58 PM in response to PeterPeterPeterPeter

PeterPeterPeterPeter wrote:


Ok, I think I have a solution but still need a little help. I now put the other actions in front of your script. The workflow is looking like this:


  • Import files to iTunes
  • Change tracks information
  • Change tracks options
  • Your AppleScript


Can you tell me how to change the script if I don't want it to import files by itself but use the tracks imported and changed by the actions that where proceeded before the script?

Replace the on run ... end run block by this

--------------------------

onrun {input}

tell application "iTunes"

activate

repeat with thisTrack in input

--set data of artwork 1 of thisTrack to (read (file "Users:admin:Music:Cover:myartwork.png") as picture)

reveal thisTrack

mychangeTrackMedia("Audiobook") --"Audiobook" as media kind (on english system)

end repeat

end tell

endrun

Sep 2, 2012 4:30 PM in response to PeterPeterPeterPeter

Hi,

PeterPeterPeterPeter wrote:


One thing that doesn't work for me is to add other iTunes actions after your script. In the log it says: "The action "Change tracks options" couldn't command the necessary data.". But in the results it shows that it knows the track:


{file track id 70050 of user playlist id 39328 of source id 76 of application "iTunes"}

I just think why it does not work.


The reference to the track is not good (source id and playlist id), it was changed, because the track was moving from Music library to AudioBooks library.

Sep 3, 2012 3:14 AM in response to Jacques Rioux

I think I'm close to the perfect solution, but theres still one error: After editing tracks information and tracks options I need to tell the script to select the imported tracks in the library. Otherwise it hits "cmd+i" at the place I left iTunes (e.g. with a playlist selected, then the playlis information window openes; or with some piece of music selected, then it changes the media type of this track instead of the imported audiobooks).


The result of the action before the script:


{file trackid 77117 of library playlistid 26164 of sourceid 76 of application "iTunes"}



The script:


on run {input}

tell application "iTunes"


activate

repeat with thisTrack in input

set data of artwork 1 of thisTrack to (read (file "Users:admin:Music:Cover:Artwork.png") as picture)


revealthisTrack

my changeTrackMedia("Hörbuch") --"Audiobook" as media kind (on english system)

end repeat

end tell

end run


on changeTrackMedia(t)

tell application "System Events"

tell process "iTunes"


keystroke "i" usingcommand down

tell front window

tell tab group 1


clickradio button 5

delay 0.1


clickpop up button 2

delay 0.1

tell menu item t of menu 1 of pop up button 2 to if exists then

click

else


keystrokereturn

end if

end tell


-- click value of attribute "AXDefaultButton"


-- delay 0.3

tell tab group 1 -- this brings the main information tab to the front to manually edit additional information


clickradio button 2

end tell

end tell

end tell

end tell

end changeTrackMedia

Sep 3, 2012 9:36 AM in response to PeterPeterPeterPeter

PeterPeterPeterPeter wrote:


I think I'm close to the perfect solution, but theres still one error: After editing tracks information and tracks options I need to tell the script to select the imported tracks in the library. Otherwise it hits "cmd+i" at the place I left iTunes (e.g. with a playlist selected,

Ok, this seems to be a bug, in my first script the script took the database id of the imported track, I used it to select the track because the reveal command do nothing on the added file, it seems to be the same problem with the Automator action.


The reveal command can select one track only.


Workaround to select multiple tracks is :

The script create a temporary playlist, add imported tracks to it, select all track (command + "a").

Here is the script :

------------------------

onrun {input}

tell application "iTunes"

setptomakenewuser playlist -- make temporary playlist

repeat with thisTrack in input

set data of artwork 1 of thisTrack to (read (file "Users:admin:Music:Cover:Artwork.png") as picture)

duplicatethisTracktop -- add track to temporary playlist

end repeat

activate

revealp -- select temporary playlist

end tell

mychangeTrackMedia("Hörbuch") --"Audiobook" as media kind (on english system)

endrun


onchangeTrackMedia(t)

tell application "System Events"

tell process "iTunes"

keystroke "a" usingcommand down -- select all tracks in temporary playlist

delay 0.3

keystroke "i" usingcommand down

delay 0.3

tell front window

if not (exists tab group 1) then

click value of attribute "AXDefaultButton" -- close the dialog (accept to modify multiple items)

delay 0.3

end if

tell tab group 1

click radio button 5

delay 0.1

tell pop up button 2

click

delay 0.1

tell menu item t of menu 1 to if exists then

click

else

keystroke return

end if

delay 0.3

end tell

clickradio button 2 --this brings the main information tab to the front to manually edit additional information

end tell

end tell

end tell

end tell

endchangeTrackMedia

------------------------


-

If you import one file at a time, use this :

--------------------------

onrun {input}

tell application "iTunes"

activate

set thisTrack to item 1 of input

set data of artwork 1 of thisTrack to (read (file "Users:admin:Music:Cover:Artwork.png") as picture)

set dataID to database ID of thisTrack

reveal (firsttrackof (firstplaylistwhosespecial kindisMusic) whosedatabase IDisdataID)

mychangeTrackMedia("Hörbuch") --"Audiobook" as media kind (on english system)

end tell

endrun


onchangeTrackMedia(t)

tell application "System Events"

tell process "iTunes"

keystroke "i" usingcommand down

tell front window

tell tab group 1

click radio button 5

delay 0.1

click pop up button 2

delay 0.1

tell menu item t of menu 1 of pop up button 2 to if exists then

click

else

keystroke return

end if

end tell

-- click value of attribute "AXDefaultButton"

-- delay 0.3

telltab group 1 -- this brings the main information tab to the front to manually edit additional information

click radio button 2

end tell

end tell

end tell

end tell

endchangeTrackMedia

Sep 3, 2012 9:49 AM in response to turingtest2

turingtest2 wrote:


I don't know what tools are avialable to you over in OS X, but in the WIndows COM there is the ability to read from a track object its "Library Persistent ID" and then use that value to get a new object which is the same track within the library playlist. This version of the track won't "vanish" when you update its properties.


tt2

Yes, it's possible with the database ID property of the track

Sep 5, 2012 2:31 AM in response to Jacques Rioux

I use the script for one file since there are several problems with the script for more files. Because the track information window looks different when more tracks are selected, the script can't hit the right tab to go to tracks media type. It would be necessary to ad a if-clause to seperate between one and more tracks. In addition I don't like to have a new playlist every time when I add a audiobook. The solution here could be to let the script delete the playlist after changing the media kind, but that conflicts with the need to open the information window in the end to manually change some things ...


I wrote this for everybody who could make use of the scripts posted by Jacques Rioux. For me the script he posted last works fine even though I can import only one file at a time. Thank you a lot, Jacques Rioux, for your help!

Automator/AppleScript: Change track's "media kind"

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.