10.3: Launch iTunes even if it's running elsewhere

Nov 16, '03 01:14:00AM

Contributed by: Safar

I resented the fact that I could not launch an application if another user logged into my computer had left it open. Here is the solution to launch an application another user has left active in Panther, with the example of iTunes.

  1. We need a shell script that will explore processes and kill iTunes.app if it finds one
  2. We need this shell script to have administrator privileges, even for the "Guest" account
  3. We want to wrap around this shell script a nice GUI
Read the rest of the hint for the solution...

[robg adds: The following script will quit iTunes, regardless of where it was launched. Just remember to check first before trashing someone's iTunes; it's possible that iTuness may be busy re-sampling all MP3s when you merrily toss it out of existence!]

  1. The shell script to quit iTunes left open by another user:
    
    #!/bin/sh
    ok=`/bin/ps -auxww | /usr/bin/grep iTunes.app | /usr/bin/grep -vc grep` ; ko=1
    if test $ok = $ko; then
      kill `ps -ax | grep iTunes.app | grep -v grep | awk '{print $1}'`
    fi
    open -a "iTunes"
    
    If you're new to shell scripting, do this:
  2. Now we need to execute the script with administrator privileges. I presume you have named your script itunes_launcher.sh, and that it is at the root of your OS X disk. Copy and paste these lines, one by one, into the Terminal:
    
    % sudo chown root /itunes_launcher.sh
        #(you will be prompted for admin password)
    % sudo chmod +x /itunes_launcher.sh
    % sudo chmod u+s /itunes_launcher.sh
    
    This changes ownership of the script to root, makes it executable, and operates with root privileges when executed.

  3. Now we need a nice GUI around this. Launch Script Editor in Applications -> Applescript, and copy and paste the following:
    
    do shell script "/itunes_launcher.sh"
    quit
    
    Save your AppleScript as an application, leaving all options checkboxes blank, give it the name you want in the place you want. To launch iTunes, double-click on it.

    Bonus: I wanted my AppleScript to have the same icon as iTunes and place it in my dock. Find your iTunes in folder Applications. Click on it. Hit Command-I (to open the information window). Then click on the icon. Hit Command-C (to copy the icon). Now close information window of iTunes and open that of your script. Instead of hitting Command-C, hit Command-V (to paste the icon). You're set!

    Repeat step three for each of your users. This is it. Security concerned? No, iTunes won't be launched with root privileges by the shell script. The command open -a launches an application with its normal privileges.

    Comments (6)


    Mac OS X Hints
    http://hints.macworld.com/article.php?story=20031107063443250