Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Help With Basic Finder/iTunes AppleScript

Help With Basic Finder/iTunes AppleScript
Thread Tools
macrophyllum
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Nov 21, 2004, 02:20 PM
 
Hi,

I am trying to write an Applescript that will move a file to iTunes. This is what I have so far, but I get a can't create script command error. Obviously, I am not very good with AppleScript. The "process_item()" I found on net, the other part I am miserably trying to construct. Any help appreciated. Test.mp3 does exist on my hard drive.

on run
set this_item to document file "Mac OS X/Users/myUserName/Desktop/Test.mp3"
process_item(this_item)
end run

-- this sub-routine processes files
on process_item(this_item)
-- NOTE that the variable this_item is a file reference in alias format
-- FILE PROCESSING STATEMENTS GOES HERE
try
tell application "iTunes"
launch
set this_track to add this_item to playlist "Library" of source "Library"
if the playlist_status is "OK" then
duplicate this_track to this_playlist
end if
end tell
set counter to counter + 1
end try
end process_item

thx,
Ian
     
suthercd
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Nov 21, 2004, 03:29 PM
 
Here is a script that will perform the task you describe. This is one way of selecting a file to add without having to hardcode the file and path in your script. There are many others.
[php]set this_file to choose file
tell application "iTunes"
launch
try
add this_file
end try
end tell[/php]
A variable, this_file, contains an Applescript alias to the file you choose when a choose file dialog box is displayed. In the iTunes dictionary- viewable in Script Editor- there is a list of Applescript commands/verbs for iTunes.

Malcom Adams has a great site with lots of iTunes scripts and tips.

Craig
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Nov 21, 2004, 03:35 PM
 
Thanks for the help Craig. I actually want to hard-code the file location. Do you know how I would do that? I will check the mentioned site as well.
     
suthercd
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Nov 21, 2004, 06:42 PM
 
Sure. Applescript uses the ":" delimeter for paths rather than the POSIX "/". It can use POSIX paths, but that is another discussion. In Applescript, once you have the path, you have any info or access to the file. Use Script Editor to help get some help with this. The one line script [php]set x to choose file[/php] will display the path to the file you select when the script is run. Select the Result tab at the bottom of the scipt window to see this.

To hardcode the file, change the first line of the previous script to
Code:
set this_item to "Name of your HD:Users:UserName: Desktop:Test.mp3" as alias.
The exact path will be displayed when you run the one liner suggested above. You can copy and paste from the Result pane of the script window to your scipt you are developing.

You could also use the 'path to' command to accomplish the same.
Code:
set myDesktopPath to path to desktop as text set this_item to (myDesktopPath & "test.mp3" as alias)
C-
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Nov 21, 2004, 11:08 PM
 
Thanks! I got it working perfectly now.

Kind Regards,
Ian
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Mar 2, 2005, 05:15 PM
 
Now that my script is working, I of course, want to improve it ;-).

Does anyone know how I would make iTunes convert the MP3 file to bookmarkable AAC when it imports it?

TIA.
     
suthercd
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Mar 3, 2005, 10:34 AM
 
Take a look at Doug's Applescripts for the mother lode of iTunes scripts.

Craig
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Mar 3, 2005, 10:44 AM
 
Hi,

I have spent a lot of time at Doug's site and found some good examples, but most of them have to do with encoding files of a CD or something. To be quite frank, while I am quite proficient with Cocoa, I am horrible with AppleScript.

Can anyone else post a sample script here? I did get this far, but it doesn't work :-(

property aac_enc : ""
global encoder_backup

tell application "iTunes"
launch
try
set encoder_backup to name of current encoder
set aac_enc to some encoder whose name contains "AAC"
set current encoder to aac_enc
set this_file to "Mac OS X:Users:someUseresktop:Test.mp3" as alias
add this_file to playlist named "Library"
set thisTr to item 1 of playlist "Library"
(convert thisTr)
end try
set current encoder to encoder encoder_backup
end tell
     
suthercd
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Mar 4, 2005, 12:49 PM
 
Give this a try
Code:
tell application "iTunes" Launch set encoder_backup to name of current encoder set acc_enc to some encoder whose name contains "ACC" set current encoder to acc_enc set fileToConvert to (choose file) as list add item 1 of fileToAdd to playlist named "test' set thisTr to track 1 of playlist "test" (convert thisTr) set current encoder to encoder encoder_backup end tell
The iTunes dictionary shows that 'add' requires a list as its parameter. So the line that selects the mp3 variable designates it as a list. thisTr is a reference to a file track ID class, but could be a reference to a file also. Hope this gives you a place to start. Applescript utilizes lists and references a lot and that gives it a lot of power, but takes some getting used to. Sorta kinda pointers and arrays... sorta.

HTH
Craig
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Mar 4, 2005, 12:59 PM
 
Thanks! Got it working now EXCEPT, now two duplicate files are in iTunes, the original MP3 and an AAC file. The AAC file (not surprisingly) is not in the "test" playlist, only in the library. Any thoughts on how to erase the mp3 file and get the aac file into the correct playlist?

Thanks for your help thus far.
~Ian
( Last edited by macrophyllum; Mar 4, 2005 at 02:08 PM. )
     
suthercd
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Mar 4, 2005, 06:30 PM
 
There is a delete command that takes a reference as a param. You have a ref from the line that selects the file. Before going too much further, you'll need to put in some checks to be sure that the script is not going to delete something that you did not intend. I've done this when a script I wrote became familiar and I would execute it without the close attention I paid when I first wrote it... and learned the hard way.

Craig
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Mar 10, 2005, 10:34 AM
 
Hi,

So, I got this far, everything compiles, runs and track is imported, but not converted, not does it play. Anyone see what is wrong:

property aac_enc : ""
global encoder_backup

tell application "iTunes"
launch
try
set p_name to "Test Playlist"
set encoder_backup to name of current encoder
set aac_enc to some encoder whose name contains "AAC"
set current encoder to aac_enc
set this_file to "Mac OS X:Users:<username>esktop:Test.mp3" as alias
set file_list to {this_file}
add item 1 of file_list to playlist named p_name
set thisTr to track 1 of playlist p_name
(convert thisTr)

--set converter back
set current encoder to encoder encoder_backup

set src to first source whose kind is library
set lib to library playlist 1

--our convertedTr appears in the library
set convertedTr to track 1 of lib

set_type(get location of convertedTr)

play convertedTr

end try
end tell

--from Doug's Applescripts
to set_type(loc)
tell application "Finder"
-- set finfo to get info for loc
try
set file type of loc to "M4B "
on error m number n
-- display dialog m -- debugging
end try
end tell
end set_type
     
suthercd
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Mar 10, 2005, 06:51 PM
 
How about adding the one line of script (one line only) 'play thisTr' after 'set encoder to encoder encoder_backup'. Delete the rest of the code.

You already have a reference/alias to the track from the previous steps. iTunes AS dictionary shows that play takes a reference.
Craig
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Mar 10, 2005, 06:54 PM
 
Thanks for your help thus far, I really appreciate it. The play command was bit of a test. I still would like the rest of the code to work because I want the AAC file to be bookmarkable, eventually. I don't understand why the code I have doesn't work.

TIA.
     
suthercd
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Mar 10, 2005, 07:31 PM
 
Phew, passed the test. I'll be looking in the mailbox every day for the decoder ring. Bookmarkable means...?? What app would utilize the bookmark? AFIK iTunes doesn't use that terminology. The concept is there maybe, but.....

C-
( Last edited by suthercd; Mar 10, 2005 at 07:50 PM. )
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Mar 10, 2005, 08:53 PM
 
Nice response, that gave me a nice chuckle. I especially like the part about the decoder ring.

Bookmarkable AAC files are used on the iPod (see here, http://docbug.com/blog/archives/000098.html). They are mostly for audio books, but songs can use it too. The idea is the song/audio book remembers where you stopped it last so that you can easily resume listening to it.

Apparently, all you have to do is change the file type to "M4B " (note the space after B).
     
suthercd
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Mar 12, 2005, 12:09 PM
 
Couple of code changes. In the middle of the script the track is converted to the AAC format by 'convert thisTr'. The dictionary shows that convert returns a list, so we can use that info to keep a reference to the new converted track. That info can be used to get the file location (per Doug's script) and make the file type switch via Finder. Also adding a line that sets a variable for the location (alias) of the converted track file. This could be useful later on when you want to improve the script. Starting with the convert:
Code:
set newTr to (convert thisTr) set current encoder to encoder encoder backup set m4aFileLoc to get location of item 1 of newTr my set_type(m4aFileLoc)
The script needs 'my' in front of the handler set_type call. This is in Doug's script, but was dropped in yours. The set_type handler goes after the above portion.

HTH
C-
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Mar 12, 2005, 04:06 PM
 
Thanks again. This has really helped my learn about AppleScript and now it is working perfectly.
     
   
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 08:49 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,