Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Geeklet to display iTunes info
#!/usr/bin/osascript
##################################################################
# DON'T FORGET TO CONFIGURE THE FIRST LINE OF THIS FILE #
##################################################################
# Usage
# 1st - Run this script as a shell geeklet.
#
# 2nd. Add an image geeklet with the following path:
# $workingPath/artwork
# Thats it. Enjoy
#
set workingPath to "/Users/huevoos/Documents/Geeklets/iTunes"
set debug to false
set fo to POSIX file workingPath
--tell application "Finder" to open fo
if not (application "iTunes" is running) then
return false
end if
tell application "Finder"
if not (exists fo) then
if debug then
display dialog "Nothing exists in path " & workingPath & " Please don't use ~ as a shorthand to your home. Applescript doesn't like that."
end if
throw --I'm throwing a random error (undefined variable throw) to let GeekTools know there was an error
end if
end tell
do shell script "touch " & workingPath & "/.artworkcache"
tell application "iTunes"
try
set playingTrack to current track
on error
return true
end try
end tell
tell application "Finder"
try
set theFile to open for access file ((fo as string) & ":.artworkcache") with write permission
set previousTrack to read theFile for (get eof theFile) as text
set eof of theFile to 0
write (name of playingTrack as text) to theFile
close access theFile
if (name of playingTrack as text) = previousTrack then
--return
end if
on error
close access file ((fo as string) & ":.artworkcache")
end try
end tell
tell application "iTunes"
set songData to
{name:"name", value:name of playingTrack}, ¬
{name:"artist", value:artist of playingTrack}, ¬
{name:"album", value:album of playingTrack}, ¬
{name:"lyrics", value:lyrics of playingTrack} ¬
}
end tell
repeat with song in songData
writeToFile(fo, name of song, value of song)
end repeat
tell application "iTunes"
if (count artworks of playingTrack) 1 then
set art to item 1 of artworks of playingTrack
set dt to data of art
set tp to format of art
else
if debug then
display dialog "Track has no artwork"
end if
throw
end if
end tell
set fl to writeToFile(fo, "artwork", dt)
tell application "Finder"
set file type of fl to tp
end tell
return
to writeToFile(wFolder, wName, dataToWrite)
tell application "Finder"
if not (exists file ((wFolder as text) & ":" & wName)) then
set newFile to make new file at wFolder with properties {name:wName}
end if
set theFile to file ((wFolder as text) & ":" & wName)
try
set theHandler to (open for access (theFile as text) with write permission)
set eof of theHandler to 0
write dataToWrite to theHandler
close access theHandler
on error
if debug then
display dialog "Error writing image data to " & fl as text
end if
close access theFile
end try
end tell
return theFile
end writeToFile
####################################################################################
# Licensed under the GPL V.3 #
# Copyright (C) 2011 Javier Alejandro Parra Cervantes #
# http://www.javierparra.mx #
####################################################################################
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
####################################################################################
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
####################################################################################
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
###################################################################################
@Hum4n01d
Copy link

Hum4n01d commented Aug 19, 2021

#!/usr/bin/osascript
##################################################################
#      DON'T FORGET TO CONFIGURE THE FIRST LINE OF THIS FILE     #
##################################################################
#				Usage
#   1st - Run this script as a shell geeklet.
#
#   2nd. Add an image geeklet with the following path:
#      $workingPath/artwork
#   Thats it. Enjoy
#
set workingPath to "/Users/Kent/Documents/Local/Code/Shell/GeekTool"

set debug to false

set fo to POSIX file workingPath
--tell application "Finder" to open fo
if not (application "Music" is running) then
	return false
end if

tell application "Finder"
	if not (exists fo) then
		if debug then
			display dialog "Nothing exists in path " & workingPath & " Please don't use ~ as a shorthand to your home. Applescript doesn't like that."
		end if
		throw --I'm throwing a random error (undefined variable throw) to let GeekTools know there was an error
	end if
end tell
do shell script "touch " & workingPath & "/.artworkcache"
tell application "Music"
	try
		set playingTrack to current track
		
	on error
		return true
	end try
end tell

tell application "Finder"
	try
		set theFile to open for access file ((fo as string) & ":.artworkcache") with write permission
		
		set previousTrack to read theFile for (get eof theFile) as text
		set eof of theFile to 0
		
		write (name of playingTrack as text) to theFile
		
		close access theFile
		if (name of playingTrack as text) = previousTrack then
			--return
		end if
		
	on error
		close access file ((fo as string) & ":.artworkcache")
	end try
	
end tell
tell application "Music"
	set songData to {¬
		{name:"name", value:name of playingTrack}, ¬
		{name:"artist", value:artist of playingTrack}, ¬
		{name:"album", value:album of playingTrack}, ¬
		{name:"lyrics", value:lyrics of playingTrack} ¬
			}
end tell
repeat with song in songData
	writeToFile(fo, name of song, value of song)
end repeat
tell application "Music"
	if (count artworks of playingTrack)  1 then
		set art to item 1 of artworks of playingTrack
		set dt to data of art
		set tp to format of art
	else
		if debug then
			display dialog "Track has no artwork"
		end if
		throw
	end if
	
end tell

set fl to writeToFile(fo, "artwork", dt)

tell application "Finder"
	set file type of fl to tp
end tell
return
to writeToFile(wFolder, wName, dataToWrite)
	tell application "Finder"
		if not (exists file ((wFolder as text) & wName)) then
			set newFile to make new file at wFolder with properties {name:wName}
			
		end if
		set theFile to file ((wFolder as text) & wName)
		try
			
			set theHandler to (open for access (theFile as text) with write permission)
			set eof of theHandler to 0
			
			write dataToWrite to theHandler
			
			close access theHandler
			
		on error
			if debug then
				display dialog "Error writing image data to " & fl as text
			end if
			close access theFile
		end try
		
	end tell
	return theFile
end writeToFile

####################################################################################
#                        Licensed under the GPL V.3                                #
#              Copyright (C) 2011  Javier Alejandro Parra Cervantes                #
#                           http://www.javierparra.mx                              #
####################################################################################
#   This program is free software: you can redistribute it and/or modify           #
#   it under the terms of the GNU General Public License as published by           #
#   the Free Software Foundation, either version 3 of the License, or              #
#   (at your option) any later version.                                            #
####################################################################################
#   This program is distributed in the hope that it will be useful,                #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of                 #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                  #
#   GNU General Public License for more details.                                   #
####################################################################################
#   You should have received a copy of the GNU General Public License              #
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.          #
###################################################################################

Working version on macOS Big Sur (replaced iTunes with Music, fixed file broken url concatenation)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment