Jump to content

How to pass hotkey to Applescript?

Recommended Posts

I thought for sure this would be so basic as to have a simple tutorial or section in the help documents, but I couldn't find any such thing nor could I find anyone discussing how to do it.


I have an existing Applescript (one of Doug's Applescripts for iTunes) that allows for rating iTunes songs via a menu.  But I'd like to be able to rate via keyboard shortcuts, so that while working and a song comes up I can just quickly flick a key combo and bam - rated.  No mousing, selecting, or reading required.


So what I need to do I believe is set up 5 hotkeys (for ratings 1 to 5 stars) and pass the typed value to the applescript and act accordingly.  I see that by default the hotkey is apparently passed through to the workflow, but then what?  How do I access that variable in the Applescript?


Thanks in advance!

Link to comment

AFAIK, a workflow can't receive keypresses because they aren't Mac apps: the keypress events will be sent to Alfred instead.


You probably want to connect each of your 5 hotkeys to 5 Run Script actions, each of which calls your AppleScript with the appropriate arguments for that rating.

Link to comment

Well there is no workflow currently, that's what I'm trying to create.  The Applescript in question is here:




I just want to assign keyboard shortcuts so that I can instantly rate a song without having to see a dialog, choose a rating, and click a button.  One keystroke and the song is rated based on whatever I typed (like CTRL-1 through CTRL-5, or something)


Thanks for any assistance!

Link to comment

One way to do what you want is as follows:
1. Add a Hotkey Trigger for each rating, e.g. CTRL+1, CTRL+2 etc.
2. Each Hotkey should have Action set to "Pass through to workflow" and Argument set to "Text". Finally, set the Text field of each Hotkey to the appropriate rating (i.e. 1, 2, 3, 4, 5, or 0):
3. Add a Run Script action with Language set to "/usr/bin/osascript" and paste the following in the Script box:

set newRating to "{query}" as integer
tell application "iTunes"
	if player state is not playing then
		log "Nothing playing"
	end if
	set currentTrack to current track
	set currentRating to ((rating of currentTrack) / 20)
	log "Current Rating : " & (currentRating as string) & " New Rating : " & (newRating as string)
	if newRating = currentRating then
		log "Rating unchanged"
	end if
	set rating of currentTrack to (newRating * 20)
	return "Set rating to " & (newRating as string)
end tell

4. Connect all of your hotkeys to this action.
The workflow should now work as desired (but only when a track is playing).
Optionally, you can add a Post Notification Output with Title (or Text) set to "{query}" and select the Only show if passed in argument has content option. This will then show "Set rating to X" when a track's rating is changed.

Edited by deanishe
Link to comment

As an addendum: iTunes stores ratings internally as numbers between 0 and 100, which is why I divide the track's current rating by 20 and multiply the new rating by 20 before setting it. 10, 30, 50 etc. correspond to half-star ratings, i.e. 70 = 3.5 stars, 90 = 4.5 stars.


The "log" statements will show up in Alfred's debugger. Any text returned will be passed to the next action as {query} (if there is a next action).

Edited by deanishe
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...