summaryrefslogtreecommitdiff
path: root/mac/.config/sketchybar/plugins/music/Get-Artwork.applescript
blob: ca3ee31f8b5ed51f53e51fca201072381f71cf7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--get current path
tell application "Finder" to set current_path to container of (path to me) as alias

--get artwork
tell application "Music"
	try
		if player state is not stopped then
			set alb to (get album of current track)
			tell artwork 1 of current track
				if format is JPEG picture then
					set imgFormat to ".jpg"
				else
					set imgFormat to ".png"
				end if
			end tell
			set rawData to (get raw data of artwork 1 of current track)
		else
			return
		end if
	on error
		return POSIX path of ((current_path as text) & "Cover-Default.png")
		return
	end try
end tell

--create path to save image as jpg or png
set newPath to ((current_path as text) & "tmp" & imgFormat) as text

try
	--create file
	tell me to set fileRef to (open for access newPath with write permission)
	--overwrite existing file
	write rawData to fileRef starting at 0
	tell me to close access fileRef
	
	delay 1
	
	--resize image
	set the target_length to 500
	try
		tell application "Image Events"
			-- start the Image Events application
			launch
			-- open the image file
			set this_image to open newPath
			-- perform action
			scale this_image to size target_length
			-- save the changes
			save this_image with icon
			-- purge the open image data
			close this_image
		end tell
	on error
		return POSIX path of ((current_path as text) & "Cover-Default.png")
	end try
	
	return POSIX path of newPath
on error m number n
	log n
	log m
	try
		tell me to close access fileRef
	end try
	return ((current_path as text) & "Cover-Default.png") as text
end try