Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 4, 2005, 03:58 PM   #1
mduser63
macrumors 68040
 
mduser63's Avatar
 
Join Date: Nov 2004
Location: Salt Lake City, UT
Send a message via AIM to mduser63
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.
mduser63 is offline   0 Reply With Quote
Old May 4, 2005, 10:07 PM   #2
HexMonkey
Demi-God (Moderator)
 
HexMonkey's Avatar
 
Join Date: Feb 2004
Location: New Zealand
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
HexMonkey is offline   1 Reply With Quote
Old May 4, 2005, 11:14 PM   #3
mduser63
Thread Starter
macrumors 68040
 
mduser63's Avatar
 
Join Date: Nov 2004
Location: Salt Lake City, UT
Send a message via AIM to mduser63
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
This is something I had hoped to be able to do with Automator, but couldn't. It was Automator's shortcomings and limitations that motivated me to start learning some AppleScript. The next step is to see about writing some Automator actions with AppleScript.
__________________
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.
mduser63 is offline   1 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC