![]() |
| |
![]() |
||
|
![]() |
||||||||||||||||||||||||||||||
|
Taming Napster File Downloads
But when Windows 95 debuted, it supported 255 character file names! Now, I happen to think that 255 characters is much more than the average user needs, and they don't make for very neat file lists. But one area where they DO come in handy is when you are dealing with MP3 files. For those who don't know what MP3 is, it's a compressed sound format that grew out of the MPEG (Motion Picture Enhancement Group) standard for encoding video and audio tracks [Editor's Note: For more information, read this issue's Warehouse.]. MP3 compresses sound by stripping out frequencies that the human ear cannot hear and compressing the rest much the same way that StuffIt compresses document files. For those familiar with MP3, it is not news that Napster, the online peer-to-peer music or MP3 sharing service, now has an official Macintosh client program. Mac users can now participate in the online music revolution with most of the functionality that Windows users have enjoyed. The problem is, most of Napster's users are also Windows users, who have a tendency to name their files with long concatenations of the artist's name, the album name, the number of the song as it appears on the album, and the song title. And to top things off, they tack ".mp3" to the end! As you might guess, this makes for some hideously long file names, and most of my Napster downloaded files end up truncated before they get to a unique name. Just downloading files from the same artist and album will max out the 31 character limit long before you get to the song's title. The result is that you risk getting files whose name consists of just the artist and some of the title, followed by a number (to make it unique) and the obligatory ".mp3" file extension. However, the Mac Napster client software does help you by sticking the entire file name in the file's comment entry (in the Get Info box). So if you set your Finder preferences for your download folder to show comments, you can at least tell the files apart.
AppleScript, a Whip, and a Chair It further occurred to me that when other users were trying to browse my files through Napster, they couldn't tell what the heck I had because the file names were so messed up. At that point, I turned to AppleScript for help. First, I realized that I could set the comment field to reflect the complete name, then write a script to snag the first 27 characters of the comment, then tack on the ".mp3" extension for Windows users. Second, being limited to short file names, I realized that the standard naming conventions of most Windows users for MP3 files had to go. Instead, I restructured the name to delete unnecessary characters like underscores, dashes, etc. and then inverted the order of the elements (from artist, album, track number, song title to track number, song title, artist, album). The reasoning was that Napster allows searching by artist and song title, so the least important information was the album name - if it was lost in the name folks could still find songs by artist and song title. So I wanted to revamp the files' comment info and then take that info and rename the file. The Get Info box before running the script looks like this:
Here's the breakdown on the script. I reused some elements from previous scripts, like the routine to add the Windows file type based on the Macintosh file type, and another piece that strips out special characters (underscores, dashes, etc.).
Comments? First, let's initialize some things to use later. If you want to replace special characters with some other character, set replace_char to the character you want to replace with and use_replace_char to true. Max_length is the maximum file name length (minus the 4 characters for the period and 3 letter file type). The special_character list represents the characters I don't want to appear in the final name. The last 2 lines will be explained later. -- Set variables Next, we tell the Finder to get the list of selected items and rename them to whatever is in the comment field. Because I launch my scripts using the OSA Menu extension, I don't need to do anything except select the files to rename and then launch the script. However, if you prefer to make your script a "droplet" you can do that by surrounding the tell block with an on open handler and saving the file as an application rather than as a compiled script. I won't go into that here, but instead we'll write a simple, compiled script for use with OSA Menu. tell application "Finder" tell
my_as_lib We then finish the script with a couple of handlers. The first one tests the Mac file type and adds the Windows file type. I've stored frequently used handlers I have written in a file called, "My AS Lib." That file contains the handler: on set_pc_type(this_file) This script understands a small handful of Windows types, but you can extend the list to whatever types you may need to use if you are transferring files to/from a Windows machine.
Reusable Code Makes Your Life Easier We preloaded the scripts containing the 2 handlers at the beginning of the script, using the lines: set my_as_lib
to (load script file "Detoo:Bin:My
AS lib") Now we can call the handlers from those scripts using a simple tell block. Note that you don't need to specify the complete path (disk:folder:filename) as I did - if AppleScript can't find the file, it will ask you where it is located the first time the script is run. The last handler was actually written by Sal Soghoian and is contained in his "Add Prefix/Suffix to Files" script. It handles duplicate file names by alerting the user that the name is already taken and allows the user to make changes to the proposed name or to skip the item entirely. Rather than clutter my script with a duplicate of his code, I've referenced it remotely using the of keyword, the same way I called on my own routine in "My AS Lib." The code is ©1998 Sal Soghoian, Apple Computer: on set_item_name(this_item,
new_item_name) Referencing external script handlers is tricky and not for everyone! But if you can master it, it allows you to reuse bits of code without retyping them (or cutting and pasting them) and it allows you upgrade all your scripts at the same time. For example, by adding to the list of Mac/Windows file types in my set_pc_type script, I automatically give every script that uses that routine the ability to recognize more file types! However, if you have difficulty with it, you can always copy the above 2 handlers into your script, then change the line that references those handlers to: my set_item_name(my_item, my_name & my set_pc_type(my_item)) My tells AppleScript to look for the handler contained somewhere within the current script, rather than sending the command to the application mentioned in the surrounding tell block. An alternative to my is to follow the reference with the words of me. That's all for now!
Illustrator Scripting Redux I received some mail regarding the Illustrator scripting column, and thought I'd share it with you, since it's the best explanation I have come across about some applications launching when you open their dictionary in Script Editor or SMILE. Don Rossi wrote:
Good explanation, Don. I hadn't realized that was the reason, but it makes sense. Quark always does the same thing to me. I still wish the AS dictionary was part of AI, though.
More About the AppleScript Foundry Every month in the AppleScript Foundry, I'll be sharing what I know about scripting. Since the object of this column is to get people who are new to scripting up and running, I will take a hands on approach, explaining new terms along the way. However, it is not my goal to talk down to the reader - If you want harder stuff, just write me! You can reach me at kevin@macosjournal.com or you can use the handy web feedback form. Here is a list of places you can go to get more info on AppleScript:
|
|||||||||||||||||||||||||||||||
|