|
|
#1 |
|
Another AppleScript question - Reading a file
I'm trying to figure out how to read a file in using AppleScript. I'd like to read each line into a different list elements so that I can then copy the contents of each list element to the name field of tracks in iTunes. I've got it to read in as a list OK I think, but I don't know how to access individual lines in the list (or file). This is the code I've got so far:
tell application "Finder" set Names to (read (choose file with prompt "Pick text file containing track names")) as list display dialog Names end tell Now what I need to know how to do is reference just one line of "Names" at a time. Any help would be appreciated.
__________________
27" 2.93 GHz Core i7 iMac, 12 GB RAM 2.3 GHz Retina MacBook Pro 12" 1.33 GHz Superdrive PowerBook G4, 1.25 GB RAM iPhone 5, iPad 3, etc. |
|
|
|
0
|
|
|
#2 |
|
Your current code doesn't actually read the file as a list, because it doesn't know how to split the file into list items. You can use "paragraphs of" to convert it to a list of lines.
Then, to read the list, you can get an item at a particular index (eg "item 1 of Names"), and find out how many items there are with "count" (eg "count Names"). Alternatively, you can use a repeat loop to iterate through the list as follows (this example also filters out blank lines): Code:
tell application "Finder" set Names to paragraphs of (read (choose file with prompt "Pick text file containing track names")) repeat with nextLine in Names if length of nextLine is greater than 0 then --Do something with the next name, which is stored in "nextLine" end if end repeat end tell
__________________
MassReplaceIt 2.9.1 |
|
|
|
1
|
|
|
#3 |
|
Thanks a lot for the reply. The people here on MacRumors are great. In case anybody is interested, I've written a little script (with the help of HexMonkey) which takes track names from a text file (one track name per line) and uses it to name the currently selected tracks in iTunes. I'm not expert at programming, and I've never even attempted to start to write an AppleScript before today, so I'm sure it's not perfect, but it works well enough for what I want to do. Here's the code:
Code:
tell application "iTunes"
if selection is not {} then
copy selection to theTracks
copy theTracks's length to theTrackCount
else
display dialog "No tracks selected." with icon caution
return
end if
end tell
tell application "Finder"
set Names to paragraphs of (read (choose file with prompt "Pick text file containing track names"))
if (the length of Names) is not equal to theTrackCount then
display dialog "Number of tracks selected does not match the number of lines in the track info file." with icon caution
return
end if
end tell
repeat with i from 1 to (theTrackCount)
tell application "iTunes"
copy (a reference to item i of theTracks) to theTrack
copy (item i of Names) to the name of theTrack
end tell
end repeat
__________________
27" 2.93 GHz Core i7 iMac, 12 GB RAM 2.3 GHz Retina MacBook Pro 12" 1.33 GHz Superdrive PowerBook G4, 1.25 GB RAM iPhone 5, iPad 3, etc. |
|
|
|
1
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| AppleScript Help - reading a file and sending the text | crobbulan | Mac Programming | 1 | Mar 14, 2010 06:11 AM |
| Applescript Question - Probably Basic | HuggerMugger | Mac Programming | 5 | Nov 9, 2009 03:53 PM |
| Quick AppleScript question - Set variable depending on if [ok] or [cancel] pressed | maclover001 | Mac Programming | 2 | Jan 16, 2009 11:14 PM |
| yet another RAM question - about producer | vante | Buying Tips and Advice | 3 | Mar 8, 2006 10:19 AM |
| Another Newbie Question - Trackballs.. | cabasner | Mac Basics and Help | 13 | Oct 17, 2005 10:56 AM |
All times are GMT -5. The time now is 09:14 PM.







Linear Mode

