# Add to library (and download) button for iTunes

**URL:** <https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628>\
**Category:** Setup/Preset Sharing\
**Tags:** touch-bar, widget\
**Created:** [March 31, 2019, 12:55am UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628 "2019-03-31T00:55:52Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mingwei](https://community.folivora.ai/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@mingwei](https://community.folivora.ai/u/mingwei)\
**Post date:** [March 31, 2019, 12:55am UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/1 "2019-03-31T00:55:52Z")

</div>

This is a preset for adding and downloading Apple Music tracks to your iTunes library: [https://share.folivora.ai/sharedPreset/a1c03ee1-9554-438a-a3f4-7795b8e644c4](https://share.folivora.ai/sharedPreset/a1c03ee1-9554-438a-a3f4-7795b8e644c4)

EDIT: Here's an updated version; the previous one treats tracks that haven't been added to the library as though they have when they're opened from the library tab. Also there's no tick anymore for 'added and downloaded' because it seemed superfluous.  
[https://share.folivora.ai/sharedPreset/26bd207c-e1a3-4619-91ba-a445ab5c242d](https://share.folivora.ai/sharedPreset/26bd207c-e1a3-4619-91ba-a445ab5c242d)

add to library:  
 ![01%20pm](https://community.folivora.ai/uploads/default/original/2X/c/cddd59434256de69e5f3ac26b608ea2880613504.png)

download:  
 ![06%20pm](https://community.folivora.ai/uploads/default/original/2X/9/902d21525e3fe94f88c7147f453dc1598a45c47b.png)

added and downloaded:  
 ![27%20pm](https://community.folivora.ai/uploads/default/original/2X/0/0a7926a5ea77f481f6554a50021fc93cce7bb5c8.png)

(now playing widget not included in the preset, it's just the default one included in BTT)

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [March 31, 2019, 9:03am UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/2 "2019-03-31T09:03:10Z")

</div>

Really handy, really nice preset! I just have the little problem that I won't get the arrow (last screenshot) once I downloaded the song to my library 😕 The widget just disappears 😯

---

<div class="post-metadata">

**Author:** ![GoldenChaos](https://community.folivora.ai/user_avatar/community.folivora.ai/goldenchaos/32/23617_2.png) [@GoldenChaos](https://community.folivora.ai/u/GoldenChaos)\
**Post date:** [March 31, 2019, 10:13am UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/3 "2019-03-31T10:13:15Z")

</div>

This seems VASTLY superior to my version! I will definitely replace the one I'm using with a version of this 🙂

---

<div class="post-metadata">

**Author:** ![mingwei](https://community.folivora.ai/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@mingwei](https://community.folivora.ai/u/mingwei)\
**Post date:** [March 31, 2019, 6:38pm UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/4 "2019-03-31T18:38:17Z")

</div>

sorry here's a third version that's fixed another problem with it -- some track properties in iTunes keep changing depending on where you're playing them from so it's tricky to detect whether it's been added to library etc.

[https://share.folivora.ai/sharedPreset/4445cd13-3971-4c48-a0d2-e883029c1b32](https://share.folivora.ai/sharedPreset/4445cd13-3971-4c48-a0d2-e883029c1b32)

this should be the last one! should have used this for longer to look for problems before uploading in the first place

---

<div class="post-metadata">

**Author:** ![mingwei](https://community.folivora.ai/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@mingwei](https://community.folivora.ai/u/mingwei)\
**Post date:** [March 31, 2019, 6:39pm UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/5 "2019-03-31T18:39:58Z")

</div>

yeah I removed that after the first version because it seemed superfluous -- I couldn't figure out how to get iTunes to remove tracks from the library so I couldn't add a function to tick button. Feel free to add it back in; you just have to copy and paste the icon data from for the tick from the first version.

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [April 1, 2019, 4:55am UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/6 "2019-04-01T04:55:04Z")

</div>

Got it! Works perfectly!  
Do you mind if I add it to my [Caliguvara C](https://community.folivora.ai/t/caliguvara-c2-my-first-update-great-new-calendar-widgets-a-totally-new-way-to-share-your-music-design-fixes-and-more/) Preset?

---

<div class="post-metadata">

**Author:** ![yuuiko](https://community.folivora.ai/user_avatar/community.folivora.ai/yuuiko/32/31891_2.png) [@yuuiko](https://community.folivora.ai/u/yuuiko)\
**Post date:** [April 1, 2019, 8:31am UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/7 "2019-04-01T08:31:03Z")

</div>

Me to for [AquaTouch](https://community.folivora.ai/t/aquatouch-functionally-minimal-clean-and-proactive-preset-version-2-1-updated-24-september-2018/1704)!  
Full credit within code and on the changelog. XD you know you've made something good when we all want it 😛

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [April 2, 2019, 6:26am UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/8 "2019-04-02T06:26:57Z")

</div>

I'm still a bit new to Apple script.  
Could someone explain me how to include my own image into a return? If I have a png file how can I display it? How can I find the text to enter into my script?  
Thank you guys 🙂

---

<div class="post-metadata">

**Author:** ![mingwei](https://community.folivora.ai/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@mingwei](https://community.folivora.ai/u/mingwei)\
**Post date:** [April 3, 2019, 1:30am UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/9 "2019-04-03T01:30:23Z")

</div>

here's um a fourth version that fixes a few more problems with identifying the status of a track: [https://share.folivora.ai/sharedPreset/9e37c748-736f-45c6-a2f1-8aeb94a04126](https://share.folivora.ai/sharedPreset/9e37c748-736f-45c6-a2f1-8aeb94a04126)  
I've used this for a few days without issue now so, fingers crossed, this one covers all the bases?

Feel free to use this wherever!

---

<div class="post-metadata">

**Author:** ![mingwei](https://community.folivora.ai/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@mingwei](https://community.folivora.ai/u/mingwei)\
**Post date:** [April 3, 2019, 2:05am UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/10 "2019-04-03T02:05:26Z")

</div>

convert the image into a base64 string here: [https://www.base64-image.de](https://www.base64-image.de)  
and then follow instructions here: [https://github.com/folivoraAI/BetterTouchTool/issues/2038](https://github.com/folivoraAI/BetterTouchTool/issues/2038)

---

<div class="post-metadata">

**Author:** ![GoldenChaos](https://community.folivora.ai/user_avatar/community.folivora.ai/goldenchaos/32/23617_2.png) [@GoldenChaos](https://community.folivora.ai/u/GoldenChaos)\
**Post date:** [April 3, 2019, 12:53pm UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/11 "2019-04-03T12:53:15Z")

</div>

Sweet! Will update the one I'm using in my local dev version 🙂

EDIT: I can verify that this version has killed off all remaining bugs and functions correctly!!!

EDIT2: Might've spoken too soon, the download button shows up for certain songs even if it's already downloaded o\_O weird. I'll troubleshoot it a bit later.

---

<div class="post-metadata">

**Author:** ![GoldenChaos](https://community.folivora.ai/user_avatar/community.folivora.ai/goldenchaos/32/23617_2.png) [@GoldenChaos](https://community.folivora.ai/u/GoldenChaos)\
**Post date:** [April 19, 2019, 5:06pm UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/12 "2019-04-19T17:06:57Z")

</div>

Turns out this widget is what's causing the iTunes launching bug - whenever iTunes is currently playing music and the user quits the app, if this widget is active iTunes will automatically relaunch.

I can't figure out why this is happening. @Andreas_Hegenberg do you have any idea how to fix this? I've looked over the script again and again and I don't see anything wrong.

---

<div class="post-metadata">

**Author:** ![mingwei](https://community.folivora.ai/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@mingwei](https://community.folivora.ai/u/mingwei)\
**Post date:** [April 19, 2019, 6:12pm UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/13 "2019-04-19T18:12:37Z")

</div>

hey, I'm not sure why it's happening either, but I've not been having problems with it mainly because I have it in a conditional activation group that only becomes visible when iTunes is the currently playing app -- setting it up that way (or setting it such that it's only visible when the iTunes process is running) could be a way around this.

**EDIT:** turns out using 'tell application' always launches the application ([source](https://stackoverflow.com/questions/16064957/how-to-check-in-applescript-if-an-app-is-running-without-launching-it-via-osa/30996223)). Likewise, using `if application "iTunes" is running` also itself launches iTunes.

One way around this is to wrap the whole applescript inside something like `tell application "System Events" / if application process "iTunes" exists then...` but that seems to take a long time to run because it essentially checks the name of every process currently running. I think using conditional activation groups basically achieves the same thing without lag.

---

<div class="post-metadata">

**Author:** ![GoldenChaos](https://community.folivora.ai/user_avatar/community.folivora.ai/goldenchaos/32/23617_2.png) [@GoldenChaos](https://community.folivora.ai/u/GoldenChaos)\
**Post date:** [April 19, 2019, 7:58pm UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/14 "2019-04-19T19:58:14Z")

</div>

Aha, I fixed it! I was able to stop iTunes from relaunching by wrapping the entire inner contents of the script in a try statement.

```auto
if application "iTunes" is running then
	tell application "iTunes"
		try
			set trackName to name of current track
			set libraryCheck to tracks in playlist "Music" whose name is trackName
			set playlistClass to class of current playlist
			try
				set trackClass to class of item 1 of libraryCheck
			end try
			
			if libraryCheck is {} or trackClass is URL track then
				return "add to library"
			else if libraryCheck is {} and playlistClass is not user playlist then
				return "add to library"
			else
				try
					get location of item 1 of libraryCheck
					return ""
				on error
					return "download"
				end try
			end if
		on error
			return ""
		end try
	end tell
else
	return ""
end if

```

---

<div class="post-metadata">

**Author:** ![GKAW](https://community.folivora.ai/user_avatar/community.folivora.ai/gkaw/32/17635_2.png) [@GKAW](https://community.folivora.ai/u/GKAW)\
**Post date:** [January 29, 2021, 10:27am UTC](https://community.folivora.ai/t/add-to-library-and-download-button-for-itunes/7628/15 "2021-01-29T10:27:26Z")

</div>

Hi, this seems to have stopped working in Big Sur... Anyone got a fix for this yet? Was one of my favourite features 😕
