Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with AppleScript File #45

Open
mdjhnson opened this issue May 25, 2023 · 17 comments
Open

Problem with AppleScript File #45

mdjhnson opened this issue May 25, 2023 · 17 comments

Comments

@mdjhnson
Copy link

mdjhnson commented May 25, 2023

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.

@Nxthxn-cyber
Copy link

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.

@etrahretep
Copy link

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)

@mdjhnson
Copy link
Author

mdjhnson commented Mar 6, 2024

@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
Copy link
Author

mdjhnson commented Mar 6, 2024

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

@etrahretep
Copy link

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
Copy link

@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?

@mdjhnson
Copy link
Author

mdjhnson commented Mar 6, 2024

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

@etrahretep
Copy link

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"

@mdjhnson
Copy link
Author

mdjhnson commented Mar 7, 2024

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

@etrahretep
Copy link

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

@mdjhnson
Copy link
Author

mdjhnson commented Mar 7, 2024

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.

@dionmunk
Copy link

dionmunk commented Mar 7, 2024

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.

@etrahretep
Copy link

etrahretep commented Mar 7, 2024

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
Copy link

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

@dionmunk
Copy link

dionmunk commented Mar 7, 2024

@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.

@etrahretep
Copy link

etrahretep commented Mar 7, 2024

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

@mdjhnson
Copy link
Author

mdjhnson commented Mar 8, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants