Thursday, December 14, 2017

#1 2015-07-16 01:10:26 pm

partron22
Member
Registered: 2011-03-18
Posts: 86

Switch iTunes Playlists via Keyboard Shortcuts.

I've had this lying around a while.
May as well share it here.

Sometimes it's nice to change what's playing on iTunes without mousing around or with a Bluetooth keyboard, from across the room. This script, and a little setup in the Keyboard System Preferences will let you do that.

Looks like the internal documentation is fairly decent, so here's the code:

Applescript:

--BP 2010-2015
(*
Sometimes it's nice to change what's playing on iTunes without mousing around, or from across the room. This script, and a little setup in the Keyboard System Preferences will let you do that.

Select and play iTunes playlists via easy to use keyboard shortcuts.
Works with both wired, and bluetooth keyboards.
iTunes must be in front (active) for this script to work.
-- A Service (Automator) containing the line "Tell application "iTunes" to activate" will accomplish that
-- You can even assign a keyboard shortcut to the that service.



iTunes 10 11 12 support a Script menu in the iTunes folder of the user's library folder: at '/Users/USERNAME/Library/iTunes/Scripts'.
If you put an Applescript (.scpt) inside that folder, and restart iTunes, a Script Menu will appear in the iTunes Menu Bar between 'Window' and 'Help'. The Menu lists all the scripts found in iTunes Scripts folder. Selecting one of the scripts will cause it to run.

If you want to export menu keys en masse, "com.apple.iTunes.plist" contains keyboard shortcuts for the scripts in NSUserKeyEquivalents. That file can be copied and the NSUserKeyEquivalents
   pasted into another plist on a different Mac using Xcode another plist editor or taking care, with a simple text editor.

*)



(*
Tell iTunes to play a Playlist.
Uses the script's filename as the name of the playlist to play.

To install:
Drag this script into the iTunes Scripts folder located here:
'Your home folder/Library/iTunes/Scripts'

If there is no folder named 'Scripts' in 'Your home folder/Library/iTunes/'
Then you can create one yourself.

Once the script is in the folder, change its name to match one of your iTunes playlist names.
-You can make multiple copies of the script, and give them each a different name for each of your playlists.
-You can leave the '.scpt' extension on the filename or not. It's ignored when determining which playlist should be played.

That done, close the folder.
Quitting and restarting iTunes will bring up your new script(s) in iTune's script Menu, which is located right next to the help menu.
If you choose from the menu, that playlist will start playing.


Now let's add keyboard shortcuts, so you can make it work from your bluetooth located across the room:
Open System Preferences.
Bring up 'Keyboard' prefs, and the 'Keyboard Shortcuts' pane.
Click on 'Application Shortcuts', and then the little '+' box.
In the resulting popup dialog:
Choose iTunes, and type in the name of your playlist, aka the filname you gave this script, minus the extension.
Choose a keyboard shortcut.
-I like things like command 1, or command shift 7.
Click the 'Add' button.
*)


----------------------------------------------------------------------------------------

set nam to ""
set temlst to {}
---
---
-------------------- Get the name of the current script

-- set nam to name of document 1 -- Works from script editor, not when invoked from iTunes
tell application "Finder" -- So get file name from current path info
   set filpath to path to me
   set nam to name of file filpath as text
   --display dialog (filpath as text) & return & nam
end tell

-------------------- Now strip off the file extension, leaving just the playlist name
set text item delimiters to "."
set temlst to get every text item of nam
try
   set nam to item 1 of temlst as string
end try
set text item delimiters to ""
--display dialog nam


-------------------- Tell iTunes to Play the Playlist
set badplaylistname to false -- flag for error checks
if length of nam is less than 1 then -- paranoid error check
   set badplaylistname to true
end if

if badplaylistname is equal to false then
   set playname to nam
   tell application "iTunes"
       try
           play playlist playname
           set view of browser window 1 to playlist playname -- make sure that a stop and start via mouse click returns to playlist that was invoked from kybd
       on error
           set badplaylistname to true -- likely the playlist doesn't exist
       end try
   end tell
end if
if badplaylistname then
   beep 1
end if
return

Nice thing about using this script is that once you've got the first one in your iTunes folder, you can just duplicate the script file, and change its name to some other playlist you want to make a shortcut for.

I also keep a copy of this script to open the iTunes Script folder in my iTunes script folder.
Makes it easy to add new playlists to the iTunes' Scripting Menu.
Not sure why I wrote it so oddly, but it still works, so I'm not messing with it:

Applescript:

--Open iTunes Script Folder.scpt
set srcfldr to (path to home folder as string) & "Library:iTunes:Scripts"
tell application "Finder"
   activate
end tell

tell application "System Events"
   keystroke "n" using command down
end tell

tell application "Finder"
   activate
   set target of Finder window 1 to folder srcfldr
end tell

Last edited by partron22 (2015-07-28 01:27:16 pm)

Offline

 

#2 2015-07-23 06:17:03 pm

partron22
Member
Registered: 2011-03-18
Posts: 86

Re: Switch iTunes Playlists via Keyboard Shortcuts.

Here's a far more modern version of that second script.
It'll create a Scripts folder inside 'Your home folder/Library/iTunes/Scripts', if you don't already have one:

Applescript:

--Open iTunes Script Folder.scpt
set srcfldr to (path to home folder as string) & "Library:iTunes:Scripts"
tell application "Finder"
   activate
   
   if (folder srcfldr exists) is false then
       make new folder at (path to home folder as string) & "Library:iTunes:" with properties {name:"Scripts"}
   end if
   delay 1
   if (folder srcfldr exists) is false then
       say "beep"
       display dialog "Couldn't make Scripts folder inside iTunes folder."
       return
   end if
   
   open folder srcfldr
end tell

Last edited by partron22 (2015-07-23 06:19:32 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)