![]() |
Applescript: Create relative paths
I am trying to get this script to remove the path name to the mp3 file. Here's the entire script, but the trouble is particularly in section 4. Right now, it outputs an m3u file, but has absolute paths. I am working on making a portable playlist script. Here it is. Any help would be greatly appreciated.
Code:
-- 1. THIS SETS THE NAME OF THE PLAYLIST |
This seems to work:
Replace this line: Code:
set theFiles2 to do shell script ¬Code:
set theFiles2 to name of (info for theFiles) |
Hey,
Thanks for the quick replies. The first suggestion only yielded the track name instead of the file name The second suggestion yielded this: Quote:
|
1 Attachment(s)
Maybe I'm not following what you're trying to do. I ran it on my system with the line: set theFiles2 to name of (info for theFiles) and got the results below:
#EXTM3U #EXTINF:536,Lifes Been Good - Joe Walsh Lifes Been Good.mp3 #EXTINF:196,Jimmy Buffett - Changes in Latitudes, Changes in Attitudes - #- Jimmy Buffett - Changes in Latitudes, Changes in Attitudes.mp3 #EXTINF:138,(The Theme From) The Monkees - The Monkees (The Theme From) The Monkees.mp3 #EXTINF:144,(I'm Not Your) Steppin' Stone - The Monkees - The Monkees (I'm Not Your) Steppin' Stone.m4p See attached file as well. By the way, I was able to import the playlist and use it. |
Wow. That is exactly what I am trying to do. The error I got popped up in the Script Editor window. Maybe I didn't modify my code correctly?
|
Be sure not to touch the keyboard while it's running. I've messed up a few runs by trying to tab between applications while it's running.
|
Would you mind posting what your code looks like? I tried it again, but it still gave me that error.
|
Code:
-- 1. THIS SETS THE NAME OF THE PLAYLIST |
Did that work for you?
|
Yes! thanks!!
|
just as an aside, if I can offer some (unsolicited) applescripting tips... :)
you use shell scripting more than is necessary. it doesn't hurt anything, of course, it just makes your code slow and difficult to follow. for instance, where you use: Code:
tell application "Finder"Code:
set DTpath to (path to desktop)Code:
set theFiles to (location of theTrack)Code:
set theFiles2 to (location of theTrack) as text you also have some serious nesting issues - at one point you have applescript telling iTunes to tell the Finder to tell iTunes to display a dialog. I'm sure it works, but if you were doing something more complicated it would probably start giving you errors. Use tell blocks when you need to and end them as quickly as you can; and try to avoid nested tells unless you just can't. For instance, I'd rewrite part of your script like this: Code:
tell application "iTunes" -- get playlist infoCode:
set fileBody to "#EXTM3U" & return & theItems |
Yeah! That's great!
I did try Code:
set DTpath to (path to desktop)So, I just tried to replace those code snippets (one at a time of course) and none of them seemed to work. But I wouldn't mind some insight on my "nesting" problem. That seems easier to figure out. |
which code snipps didn't work? I don't think I was using anything non-standard...
as for nesting, applescript is a programing language designed to extend itself by picking up capabilities from other applications. when you want to tap into an application's special abilities, you use a Tell block to tell the application to do it, and then give the result back to applescript. so if you wanted to know what percent of a playist a particular track represented: Code:
set plName to "cool sounds"code like the following is problematic Code:
tell application "Itunes"Code:
tell application "Itunes"there are things called OSAX (or scripting additions) that add functionality to applescript directly, without calling applications. for instance, the open for access command I used is part of the Standard Additions osax (included by apple in every install). you can get more at places like MacScripter.net, but be a little cautious, because not all of the third party ones are well-written, and they can interfere with each other. |
I tried to implement the code snippets that cwtnospam kindly provided. I replaced them and tested them one at a time. However, none of them seemed to work. Would someone mind compiling a functioning copy of this script for me to study? That's how I've been learning applescript: by studying other peoples scripts. Unfortunately, it sounds like I've picked up most of their bad habits. Thanks.
|
Do you mean that tw provided? Which don't work?
|
I've made some of the tw's suggestions here in the code below. I'd have to spend more time figuring out all the logic, but I'm sure that tw is correct that there is room to simplify and streamline the code.
Code:
-- 1. THIS SETS THE NAME OF THE PLAYLIST |
Somebody didn't understand this:
Code:
on replace_chars(this_text, search_string, replacement_string)A text item delimiter is how AppleScript divides up a string into parts. Since the default value is "", i.e. nada, the text items of a string are simply its characters with nothing between. When you set text item delimiters, you are telling AS to divvy up the string into chunks separated by those delimiters, so the text items are presented in an AppleScript list of the chunks of text as they occur between the delimiters you have set. Similarly, if you reassemble a list of text items by coercing the list to a string, AppleScript will insert its current text item delimiters between the elements of the list as it reassembles a string. The code fragment above works as follows: set AppleScript's text item delimiters to the search_string --> the word or symbol we want to find in the text. set the item_list to every text item of this_text --> a list of strings of text that occur between the instances of search_string set AppleScript's text item delimiters to the replacement_string --> change the delimiters from search_string to replacement_string set this_text to the item_list as string --> coerce the list back to a string, inserting the replacement_string between each item in the item_list. set AppleScript's text item delimiters to "" --> set them back to the default (they're persistent). A better way to do this in a handler is as follows: set tid to applescript's text item delimiters -- preserve the current state set applescript's text item delimiters to search_string set myList to text items of whatever set applescript's text item delimiters to replace_string set myNewText to myList as string set applescript's text item delimiters to tid -- set them back where they were. This is better because it preserves the state in the code that called the handler, in case it was not "". |
ok, normally this is supposed to be a hints forum - :) - but since you're looking for example code, here's a working version, with some tweaks. anything completely incomprehensible, ask.
Code:
on run |
Thanks everyone, I really appreciate the help. It really helps me to see what the code should look like. I hope I can help others with what I learn. Thanks again!
|
So, I've been too busy to work on the code lately, but had a moment in class tonight.
This line: Code:
duplicate theFiles to folderPathQuote:
The point of this script is so that people will be able to create "portable playlists": a folder with all the tracks and the respective m3u file. That way they can place their portable playlist folder in our media server to share. |
Just a guess, but it seems to me that the folder already exists in this case,
and the line under the 'on error': set folderPath to (DTopPath as text) & playlistName & ":" should be: set folderPath to (DTopPath as text) & playlistName & ":" as alias |
| All times are GMT -5. The time now is 05:51 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.