Part 3: Send selected manually

  • You know all that rsync and launchd stuff we just went through? Put it away. Here's a script you can launch manually from iTunes' Scripts menu that will add the tracks selected in the local iTunes to a user's iTunes on a remote machine.

If you need to be more descriminating about which files to copy/add and when then here's a script that can take a selection of tracks in iTunes (that is, tracks you have selected by highlighting them), copy their files to the remote machine and add them to a remote user's iTunes. We will be jettisoning the rsync and launchd routines and instead use scp (secure copy). It uses the same login parameters as ssh and all that stuff is already set up...if you've followed along this far from the beginning.

Ultiimately, what we will need is the path to each selected iTunes track's file. As we did in the earlier script, we will then convert this file path to a location on the remote machine, copy the file to this location, and add it to iTunes. We already have most of the handlers to do this.

However, we will not be using rsync and thus we won't be able to use its abilities to compare and copy files. We'll have to build separate routines for those tasks.

There are some additional quoting and escaping issues to contend with as well.

Here is how the script begins:

-- == allocate globals

global remUserName, remHostIPAddr, remAddr, locMusicLibrary, remMusicLibrary

-- == selection?

tell application "iTunes"
	set sel to selection
	if sel is {} then
		display dialog "No tracks selected." buttons {"Cancel"} default button 1 with icon 0
		return
	end if
end tell

-- == init variables

set remUserName to "cleanuser" -- use your remote username
set remHostIPAddr to "192.168.1.206" -- use your remote IP address
set remAddr to (remUserName & "@" & remHostIPAddr) as text

-- == check if remote iTunes is running

if my are_you_logged_in() is false then
	tell application "iTunes"
		display dialog ("\"" & remUserName & "\" is not logged in and running iTunes.") ¬
			buttons {"Cancel"} default button 1 with icon 0
		return
	end tell
end if

-- == get mx folder loc's via handlers

set locMusicLibrary to my get_local_music_folder()
set remMusicLibrary to my get_remote_music_folder()

-- == process track selection

tell application "iTunes"
	repeat with t in sel
		if (get class of t is file track) then
			try
				set loc to t's location
				if loc is not missing value then
					my process_this_file(POSIX path of (loc as text))
				end if
			on error m
				log "ERROR: " & m
			end try
		end if
	end repeat
end tell

-- == end run

-- == handlers would continue below... == == ==


This portion of the script culminates with calling the process_this_file() handler to copy and add each selected file. This handler will be descibed later.

First, add the variables locMusicLibrary and remMusicLibrary to the global declaration at the top of the script. These two variables will be needed by handlers later and it will be easier to pass them as globals than as parameters.

Get the selected tracks from iTunes. If no tracks are selected, notify the user and exit the script. So far, so good.

After including the username and IP address variables see if the remote user is logged in with the are_you_logged_in() handler and, again via a dialog, abort if he's a no-show.

Set your music folder variables.

Now to the actual track processing. Using a repeat loop, assign the variable t to each track reference in the selection; determine that it is a file track (and not, say, a CD track or URL track) and that it's location is not missing value; if any unanticipated errors occur in the try block skip this track; send the POSIX-style version of the file path to the process_this_file() handler.

Site contents © 2001 - 2014 (that's right: 2001) Doug Adams and weblished by Doug Adams, dougscripts AT mac DOT com. About.
All rights reserved. Legal.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc.