First time Scripter looking for help

What I’m trying to do is write a script to recognize anytime a new file is added to a certain folder…then…I want to activate iTunes and get it to go to the File menu and go to “Add to Library…” (Command+o) then hit choose. The default iTunes folder is already preselected. I think an AppleScript can do this for me, but I have yet to figure out how yet. Any help would be greatly appreciated. Thanks.

Ah, please don’t do that!

Using GUI scripting to select menus when there’s a perfectly acceptable AppleScript event for the action is a bad habit to get into.

Consider that GUI scripting requires that the target application is frontmost, interrupts any user action going on at the time, and is not portable to other languages.

Instead, reading the iTunes dictionary, you’ll find the ‘add’ command:

add: add one or more files to a playlist
	add list of alias  -- the file(s) to add
 		[to location reference] -- the location of the added file(s)
	Result: track  -- reference to added track(s)

So instead of kludging a GUI hack, given ‘theFile’ as the file dropped into the folder just:

tell application "iTunes" 
   add theFile to playlist 1 -- or whichever playlist you want
end tell

Isn’t that neater than GUI scripting (in case you can’t tell, I hate GUI scripting :wink: )

Well first of all I have no idea what GUI scripting is. I’m VERY new to all of this. I found the add command in the iTunes dictionary, but I didn’t understand how to use it. I tried and kept getting errors.

Second of all…the problem is I’m working on a computer with a small hard drive so any time I download music or upload a CD I transfer the files to another computer we have set up on the network that has a much bigger hard drive. Then I just listen to the music via the Rendezvous capability of iTunes 4. So when I transfer the files to the other computer’s iTunes Music Library they don’t automatically show up on the shared computer playlist. I know I can just walk over to the other computer and manually drag the files into iTunes, but the other computer is in another building and well an applescript would just be much nicer. Will this still work? Since the applescript will be on the other computer…how does that computer know what file i just dropped in the folder. You didn’t specify in your example of the code. Are the playlists just numbered starting at the top being 1 and counting downwards? Does the shared music of another computer get included in that counting? Thanks for you help so far.

Maybe one of these folder actions will work when attached to the target folder.

on adding folder items to this_folder after receiving these_items
	tell application "iTunes"
		add these_items to playlist 1 -- or whichever playlist you want 
	end tell
end adding folder items to

– OR –

on adding folder items to this_folder after receiving these_items
	tell application "iTunes"
		add these_items -- add them to the Library 
	end tell
end adding folder items to

Well first of all I have no idea what GUI scripting is.


GUI Scripting is Apple’s recent extension (read: hack) to AppleScript that provides emulation of user actions such as key presses, menu selections, button clicks, etc.

I say ‘hack’ because IMHO it’s a poor substitute for comprehensive AppleScript support in applications. I’d have prefered that Apple spent its time enhancing AppleScript in Cocoa and evangelizing rather than developing AppleScript GUI.

AppleScript’s true power is in its ability to manipulate data within other applications, tying them all together.

If developers get lazy and rely on AppleScript GUI to give users AppleScript control over their apps rather than spending the time to implement AppleScript support properly, AppleScript becomes little more than a glorified macro program and not the scripting language/environment that it deserves to be.

I’ll end the rant now :slight_smile:

While one of the strengths of AppleScript is that applications can define their own commands for manipulating data, it also makes it a little harder to understand since each developer might have their own way of doing something.
Reading the dictionaries is the basic tenet of understanding an application’s AppleScript support. Familiarity makes this easier.

You’d run the folder action on the remote machine and it would notice the file being dropped in the folder, even if dropped from another machine.
The only caveat here is that the remote machine is running Mac OS X since earlier versions of the Mac OS require the folder to be open on the desktop for folder actions to fire. Since you’re using iTunes and rendezvous this clearly isn’t a problem, but it’s something to remember.

Actually, I have no idea, but it doesn’t really matter. You can reference playlists by id, position or name as you see fit:

tell application "iTunes"
  add this_file to playlist 1 -- playlist by number
  add this_file to last playlist -- whatever playlist is last
  add this_file to playlist "Ambient" -- or by name
end tell

Use whichever works best for you.

As far as I’m aware, the playlist list includes all the playlists this copy of iTunes knows about. The location of the specific tracks doesn’t matter.