Sunday, December 4, 2022

#1 2022-10-08 11:14:05 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Get Artist and Track Name from Shazam to AppleScript

Make a shortcut with the name: Get the Artist and Track Name
Inside this shortcuts you will use:
1. Shazam it
2. Copy Shazam Media to clipboard (action)

Save it..

Next play a song from any media.

Run this script and you will return the Artist and Track Name of the song.

Applescript:

tell application "Shortcuts Events"
   run shortcut "Get the Artist and Track Name"
   return (the clipboard) as text
end tell

Or if we like to use record instead.

Applescript:

set shazam to getArtistAndTrackCurrentPlaying()

log artist of shazam
log track of shazam

on getArtistAndTrackCurrentPlaying()
   tell application "Shortcuts Events"
       run shortcut "Get the Artist and Track Name"
   end tell
   set artistTrack to (the clipboard) as text
   
   set ASTID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {" - "}
   set theList to every text item of artistTrack
   set AppleScript's text item delimiters to ASTID
   return {artist:item 1 of theList, track:item 2 of theList}
end getArtistAndTrackCurrentPlaying

Last edited by Fredrik71 (2022-10-08 02:38:07 pm)


Node-RED makes it easy to automate IoT

Offline

 

#2 2022-10-09 12:08:17 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Re: Get Artist and Track Name from Shazam to AppleScript

So could we make something more useful and automate a script to change artist - track
from audio files. This simple example after trying to use and old script ASObjC that didn't
work for me. Google made it not simple becouse playing a file and use ASObjC should work.

So I used afplay instead. I couldn't use do shell script becouse I need 2 action to happen...

1. Play the song.
2. Run the shortcuts action.

Some ideas to a better solution is always welcome.

The script ask for audio file... play 10s and if shazam find the artist and track.
It will rename it with application "Finder".

This version only accept 1 selection and quit after 10s

Applescript:

set theFile to POSIX path of (choose file)
tell application "Terminal" to do script "afplay -t 10 " & quoted form of theFile

set shazam to getArtistAndTrackCurrentPlaying()

delay 10

set target to POSIX file theFile as alias
set theFile to target

tell application "Finder"
   set name of theFile to (artist of shazam) & "-" & (track of shazam)
end tell

on getArtistAndTrackCurrentPlaying()
   tell application "Shortcuts Events"
       run shortcut "Get the Artist and Track Name"
   end tell
   set artistTrack to (the clipboard) as text
   
   set ASTID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {" - "}
   set theList to every text item of artistTrack
   set AppleScript's text item delimiters to ASTID
   return {artist:item 1 of theList, track:item 2 of theList} as record
end getArtistAndTrackCurrentPlaying


Node-RED makes it easy to automate IoT

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)