Playbox.widget icon indicating copy to clipboard operation
Playbox.widget copied to clipboard

Problem with AppleScript File

Open mdjhnson opened this issue 1 year ago • 17 comments

I had a problem with the AppleScript file. I had to combine lines of code for it to work and remove some special characters.

I used Script Editor to help me identify all the errors.

mdjhnson avatar May 25 '23 20:05 mdjhnson

Same here, from what I can recall anything that says "iTunes" must be changed to "music". Then again I can't insure that will help solve the problem completely.

Nxthxn-cyber avatar May 31 '23 12:05 Nxthxn-cyber

I had a problem with the AppleScript file. I had to combine lines of code for it to work and remove some special characters.

I used Script Editor to help me identify all the errors.

Could you tell me wat you've changed? Can't get it to work. (macOS 14.3, iMac Pro 2017)

etrahretep avatar Mar 06 '24 17:03 etrahretep

@etrahretep, let me know if method of sharing it doesn't work. My code is on the right. https://www.diffchecker.com/N1SMnRqi/

Also, just in case, here's a zip copy of my modified "get current track.applescript" get current track.applescript.zip

mdjhnson avatar Mar 06 '24 18:03 mdjhnson

I'll try to make a PR later today with the update.

mdjhnson avatar Mar 06 '24 18:03 mdjhnson

Thanks! but unfortunately only working in Spotify. When playing in M music scriptditor gives me error: error "De variabele currentPosition is niet gedefinieerd." number -2753 from "currentPosition" (Dutch) (Variable currentPostition not defined) Same error in the original get current track.applescript

etrahretep avatar Mar 06 '24 21:03 etrahretep

@etrahretep, let me know if method of sharing it doesn't work. My code is on the right. https://www.diffchecker.com/N1SMnRqi/

Also, just in case, here's a zip copy of my modified "get current track.applescript" get current track.applescript.zip

Actually it is macOS 14.4 latest dev beta, iMac Pro 2017. Could that be the problem?

etrahretep avatar Mar 06 '24 21:03 etrahretep

Thanks for pointing that out. I don't use Music so it was a lazy fix. However, I just tested a fix, and artwork for Apple Music is working for me now. Here's the updated file.

I'll update the PR later.

Get Current Track.applescript.zip

mdjhnson avatar Mar 06 '24 23:03 mdjhnson

Thanks for pointing that out. I don't use Music so it was a lazy fix. However, I just tested a fix, and artwork for Apple Music is working for me now. Here's the updated file.

I'll update the PR later.

Get Current Track.applescript.zip

Hero!

Never would have found out "favorited"

etrahretep avatar Mar 06 '24 23:03 etrahretep

Glad to help! Needed a "win" from some of my other projects.

mdjhnson avatar Mar 07 '24 15:03 mdjhnson

Glad to help! Needed a "win" from some of my other projects.

Other question then. Spotify uses a different method for retrieving albumart. Music doesn’t work flawlessly. Many times a previous album is used for a different song. Or stays grey. Especially when quick changing songs/albums

etrahretep avatar Mar 07 '24 17:03 etrahretep

From what I could tell as I debugged the code yesterday, they work differently. @Pe8er used a Spotify API to get the URL of the album artwork. That's why Spotify works more reliably. For Music, the AppleScript retrieves the raw data of the artwork, converts it into a file, and temporarily saves it to your computer. The AppleScript then almost immediately deletes the image. For both, this happens when the widget recognizes the song has changed.

I'll add that I can read and debug this widget, but I wonder if I can make any improvements. I'm just not that familiar with AppleScript.

mdjhnson avatar Mar 07 '24 17:03 mdjhnson

After this discussion started up, it made me want to jump in and fix my fork of this widget. @mdjhnson, I appreciate you getting the script to work again. I didn't realize those unknown characters were in the script file.

The problem with Music album art showing previous covers that you've downloaded, is that Ubersicht caches the images. It may not be Ubersicht itself, but probably the browser rendering engine it uses underneath. To fix it, the name of the cover needs to be different every single time to force it to update.

I came up with the following to fix the problem. I have it name the cover file to the current Epoch Timestamp in seconds, which is how many seconds have passed since January 1st, 1970, so it is guaranteed always to be different from the previous. There is a wonky fix here to get the seconds out of scientific notation (example: 1.XXXXE+9), which applescript defaults to for some reason, by converting the number to inches and then to a string, which changes something like 1.709835265E+9 to 1709835265. This change goes somewhere around line 157.

	-- get epoch time in seconds to use for filename since ubersicht caches the artwork
	set currentEpochTime to (current date) - (date "Thursday, January 1, 1970 at 12:00:00 AM")
	set currentEpochTimeInSeconds to currentEpochTime as inches as string 
	
	set fileName to (mypath as POSIX file) & currentEpochTimeInSeconds & ext as string -- get the filename to ~/my path/cover.ext

Additionally, the logic in the if isMusicPlaying() is true then at the top of the script needs to be adjusted a little bit to improve how it removes previous cover art and updates to the new one. I adjusted it as follows. This goes somewhere around line 25.

if isMusicPlaying() is true then
	getSongMeta()
	writeSongMeta({"currentPosition" & "##" & currentPosition})
	writeSongMeta({"darkMode" & "##" & checkDarkMode()})
	
	if didSongChange() is true then
		pruneCovers()
		if didCoverChange() is true then
			set savedCoverURL to my readSongMeta({"coverURL"})
			set currentCoverURL to grabCover()
			if savedCoverURL is not currentCoverURL then writeSongMeta({"coverURL" & "##" & currentCoverURL})
		end if
		delay 1
		writeSongMeta({"artistName" & "##" & artistName, "songName" & "##" & songName, "albumName" & "##" & albumName, "songDuration" & "##" & songDuration, "isLoved" & "##" & isLoved, "songChanged" & "##" & true})
	else
		writeSongMeta({"songChanged" & "##" & false, "isLoved" & "##" & isLoved})
	end if
else
	return
end if

Feel free to take my changes and add them to your PR if you want.

dionmunk avatar Mar 07 '24 18:03 dionmunk

Unmappable character(s) detected when saving. Get Current Track.applescript:155: The character “ ” (unicode 0x202F) cannot be represented in the “Western (Mac OS Roman)” encoding

Scherm­afbeelding 2024-03-07 om 20 27 34

etrahretep avatar Mar 07 '24 19:03 etrahretep

Invalid date in Script editor Scherm­afbeelding 2024-03-07 om 20 31 12

etrahretep avatar Mar 07 '24 19:03 etrahretep

@etrahretep You absolutely can not edit the script file in any editor except for the Script Editor app or it will mess up the encoding and go back to what started this problem in the first place. Redownload @mdjhnson's script, open it in Script Editor, and make the changes there and save it.

Additionally, depending on your locale and how dates are formatted on your specific system, you may need to change the date that I inserted. I am in the US, so the date is US formatted.

dionmunk avatar Mar 07 '24 19:03 dionmunk

Thanks. Needed that input. Should be (date "donderdag 1 januari 1970 om 00:00:00") (Dutch) Working like a charm.

etrahretep avatar Mar 07 '24 19:03 etrahretep

@dionmunk, I see what you're talking about. This original code doesn't always guarantee unique filenames. That's a good solution that you employed. Thanks for improving the removal and saving of the artwork! I just changed the code and updated the PR.

set fileName to (mypath as POSIX file) & "cover" & (random number from 0 to 9) & ext as string -- get the filename to ~/my path/cover.ext

mdjhnson avatar Mar 08 '24 03:03 mdjhnson