I have a script that iterates through tracks in a playlist. If they are already mp3 it adds them to my iPod (duplicate currentTrack to ipod_lib) and removes them from the playlist - this part works fine. If they are lossless however, I want to convert them to mp3 and add them to my iPod instead of add the lossless files themselves since they are too large. The line duplicate convertedTrack to ipod_lib throws the following error...
error "iTunes got an error: Can’t set library playlist id 270897 of source id 235166 to {file track id 322339 of library playlist id 38702 of source id 68}." number -10006 from library playlist id 270897 of source id 235166
the locateiPods code is pulled from a script on http://dougscripts.com/
Really not sure what I'm doing wrong here...
on locateiPods()
set the volumes_directory to "/Volumes/" as POSIX file as alias
set the volume_names to list folder volumes_directory without invisibles
set mounted_iPods to {}
repeat with i from 1 to the count of volume_names
try
set this_name to item i of volume_names
set this_disk to ("/Volumes/" & this_name & "/") as POSIX file as alias
set these_items to list folder this_disk
if "iPod_Control" is in these_items then
set the end of the mounted_iPods to this_disk
end if
end try
end repeat
-- check for iPod count
if the mounted_iPods is {} then
--
try
display dialog "iPod is not mounted." buttons {"Cancel"} with icon 0 giving up after 15
on error
error number -128
end try
else if the (count of the mounted_iPods) is greater than 1 then
-- choose iPod
set the ipod_names to {}
repeat with i from 1 to the count of the mounted_iPods
set this_iPod to item i of the mounted_iPods
tell application "Finder"
set the end of the ipod_names to the name of this_iPod
end tell
end repeat
tell application "iTunes"
activate
set this_name to (choose from list ipod_names with prompt "Pick the iPod to use:") as string
end tell
if this_name is "false" then error number -128
repeat with i from 1 to the count of the ipod_names
if item i of the ipod_names is this_name then
set this_iPod to item i of the mounted_iPods
exit repeat
end if
end repeat
else
set this_iPod to item 1 of the mounted_iPods
end if
return this_iPod
end locateiPods
tell application "iTunes"
set allTracks to every track in user playlist "TEST"
set the_iPod to my locateiPods() -- this is a path
set the_iPod_name to text 1 thru -2 of (the_iPod as string)
set ipod_src to some source whose name is the_iPod_name
set ipod_lib to library playlist 1 of ipod_src
repeat with i from 1 to count of allTracks
set currentTrack to item i of allTracks
set fileType to kind of currentTrack as string
if fileType is "MPEG audio file" then
duplicate currentTrack to ipod_lib
tell playlist "TEST" to delete contents of currentTrack
end if
if fileType is "Apple Lossless audio file" then
set convertedTrack to convert currentTrack
duplicate convertedTrack to ipod_lib
end if
end repeat
end tell
It seems doing the following makes it work, which suggests that convertedTrack is not of type track???
set convertedTrack to convert currentTrack
repeat with theTrack in convertedTrack
duplicate theTrack to ipod_lib
end repeat
I always used "convert" for list of tracks and then result is list of tracks. by the way it is much faster to convert list of tracks instead of doing loop to convert each track. May be you should do first a select of all tracks whose kind is lossless.
About convert, also keep in mind that convert does the conversion to the format set in your iTunes preferences. Then your scrip will depend of these preferences. I suggest, to be more safe, to save current preference, set the convert mode to format you want MP3, and then reset preferences to what they were before. You can use bellow script:
-- get & save current encoder
tell application "iTunes" to set Encoder_Base to name of current encoder
-- change encoder to MP3
tell application "iTunes" to set current encoder to (get first encoder whose name contains "MP3")
Also, may be you can optimise your iPod subroutine using Source method instead of looking for volumes mounted. iTunes kind of source provides an efficient way:
-- kind of source could be : library, iPod, audio CD, MP3 CD, radio tuner, shared library, iTunes Store
tell application "iTunes"
set myList to every source whose kind is iPod
if myList is {} then
display alert "no ipod connected"
return
else -- I assume if one iPod found, there is only 1 connected !!
set SourceName to name of first item of myList
set SourceID to id of first item of myList
end if
end tell
I hope it helps.
Related
I wrote an Applescript that would monitor a folder for new input. On receiving the new item, it would open iTunes, create a playlist and add the new files into the playlist. However, when I attach the script as a Folder Action via Folder Action Setup and add a new item, nothing happens. I have placed the script inside /Library/Scripts/Folder Action Scripts as well as ~/Library/Scripts/Folder Action Scripts but it still doesn't fire. Here is my code below:
global album_name
global artist_album
on adding folder items to this_folder after receiving added_items
--get the name of the added folder
repeat with x in added_items
copy name of x as string to playlist_name
set AppleScript's text item delimeters to "-"
set delimited_playlist_name to every text item of playlist_name
set artist_name to text item 1 of delimited_playlist_name
set album_name to text item 2 of delimited_playlist_name
set AppleScript's text item delimeters to ""
end repeat
tell "Finder" to activate "iTunes"
tell application "iTunes"
if not (exists playlist album_name) then
--make iTunes playlist
make playlist with properties {name:album_name}
--add the tracks to the iTunes playlist
repeat with song in playlist_name
add song to playlist album_name
set album of song to album_name
set artist of song to artist_name
set comment of song to ""
set composer of song to ""
set grouping of song to ""
set description of song to ""
set long description of song to ""
set (volume adjustment) of song to 100
end repeat
set view of browser window to album_name
end if
end tell
end adding folder items to
I'm running Mac OS 10.8.4
In the future
Test your code in the Applescript Editor, it helps a lot :)
If you dont use Applescript Editor, to get useful messages, use display dialog entries throughout your code to see how far it goes and what is going on.
So, assuming that the problem is with your code, I rewrote it as an simple Applescript and got it to work on my Snow Leopard (10.6.8) system.
It also runs ok from inside Automator as a Folder Action with the code in a Run Applescript Action item. Once saved from inside Automator it did activate when new files were added to the folder and run ok.
PS: Since you didn't provide any details, I had to guesstimate from your code what you were trying to do. It is far from optimal but it will get you going...
HTH
# remove this line for Applscript Action in Automator...
set input to (choose file with multiple selections allowed)
set playlist_name to {}
#display dialog "Delimeter set to -"
set AppleScript's text item delimiters to "-"
repeat with x in input
#display dialog "Item: " & (x as text)
tell application "Finder"
set fl to name of x
display dialog "Filename: " & fl
end tell
# bring back focus to the editor ## TESTING ONLY
activate me
# append item to the list
copy (fl as text) to the end of playlist_name
display dialog "Playlist count: " & length of playlist_name
set artist_name to text item 1 of fl
display dialog "Artist: " & artist_name
set album_name to text item 2 of fl
display dialog "Album: " & album_name
end repeat
#display dialog "Activating iTunes..."
#activate "iTunes"
#display dialog "iTunes activated!"
tell application "iTunes"
if not (exists playlist album_name) then
display dialog "Making new playlist: " & album_name
make playlist with properties {name:album_name}
display dialog "Changing view..."
set view of browser window 1 to playlist album_name
# add the tracks to the iTunes playlist
repeat with song in input
display dialog "Adding: " & song
set newtrack to (add song to playlist album_name)
set album of newtrack to album_name
set artist of newtrack to artist_name
set comment of newtrack to ""
set composer of newtrack to ""
set grouping of newtrack to ""
set description of newtrack to ""
set long description of newtrack to ""
set (volume adjustment) of newtrack to 100
end repeat
else
display dialog "iTunes playlist already exists!"
end if
end tell
I am using iTunes to convert a large .aif file to a much smaller .mp3 file. The filename changes each day because I am adding the date to the name. So, a file named "abcxyz 2-2-2014" gets converted in iTunes. After the conversion, I want to delete it from iTunes. I'm trying to use Applescript to search for the file and delete it. I'm trying this:
on deleteTrack(trackName)
tell application "iTunes"
set theTrack to track named trackName of playlist "Library"
set songFile to location of theTrack
delete theTrack
end tell
tell application "Finder" to delete songFile
end deleteTrack
on run
tell application "iTunes"
set result to (file tracks whose name contains "abcxyz")
repeat with t in result
deleteTrack(name of t as string)
end repeat
end tell
end run
I found the deleteTrack routine which works perfectly if you pass it a string like this:
on run
deleteTrack("abcxyz 2-2-2014")
end run
But that requires that I know the exact name of the track, which I don't. The error that I get is "Can't continue deleteTrack" ..with the deleteTrack(name of t as string) line selected in Applescript Editor.
Thanks for any help.
You should re-read the material about AppleScript's handlers. Because your handler call is inside of a tell application "iTunes" block, AppleScript is looking in iTunes for deleteTrack. Try this instead:
my deleteTrack(name of t as string)
I have a script that add or update files in a folder. What I want to do is for an applescript to add these files to iTunes. But I can't seem to loop through the directory.
tell application "iTunes"
if playlist myPlaylist exists then delete playlist myPlaylist
set newPlaylist to (make new user playlist with properties {name:myPlaylist})
repeat with theFile in myPath as list
add theFile to playlist myPlaylist
end repeat
end tell
For every repeat, the value of theFile is the individual character of myPath, not the actual file in myPath. theFile will be M, a, c, i, n, ... instead of file1.mov, file2.mov, ...
Thank you.
If myPath is a string, as list converts it to an array of characters.
You can tell Finder to get a list of aliases:
tell application "Finder"
set l to items of (POSIX file "/Users/username/Music/folder" as alias) as alias list
end tell
tell application "iTunes"
if playlist "test" exists then
delete tracks of playlist "test"
else
set p to make new user playlist with properties {name:"test"}
end if
add l to p
end tell
This would add the selected files to the currently shown playlist:
tell application "Finder"
set l to selection as alias list
end tell
tell application "iTunes"
add l to view of browser window 1
end tell
I'm trying to make an AppleScript that tells iTunes to play a certain track. Here's my code. I'm baffled, because when I set "theCommand" to "play Shake Up Christmas by artist Train" the script works when I right-click a test e-mail and click "Apply Rules." However, it doesn't work when I tell it to play what the e-mail tells it to play.
using terms from application "Mail"
on perform mail action with messages messageList for rule aRule
tell application "Mail"
repeat with thisMessage in messageList
try
set theCommand to content of thisMessage as string
on error errMsg
display alert errMsg
end try
end repeat
end tell
tell application "iTunes"
if theCommand contains "play" then
if theCommand is equal to "play" then
play
else
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to "play "
set subject to text items of theCommand
set text item delimiters of AppleScript to ""
set subject to "" & subject
set AppleScript's text item delimiters to " by artist "
set delimitedList to every text item of subject
set theTrack to the first item of delimitedList
try
set theArtist to the second item of delimitedList
set artistIsDefined to true
on error
set artistIsDefined to false
end try
set AppleScript's text item delimiters to prevTIDs
if artistIsDefined is true then
try
play (every track in playlist 1 whose artist is theArtist and name is theTrack)
say "Playing " & theTrack
on error errMsg
say errMsg
end try
else
play (every track in playlist 1 whose name is theTrack)
end if
end if
else if theCommand is equal to "pause" then
pause {}
else if theCommand is equal to "stop" then
stop {}
end if
end tell
end perform mail action with messages
end using terms from
The Mac will respond to an e-mail by saying "Playing (track)," but it won't play the track. The fact that the file is on a Time Capsule shouldn't matter, as iTunes can access it. (iTunes would have shown an exclamation mark to the left of the track name if it couldn't.)
My guess is that you need to change the word "every" to "first" in this command...
play (every track in playlist 1 whose artist is theArtist and name is theTrack)
I didn't test my theory, but I think by using the word "every" you get a list of tracks from that command (even if only 1 track is in the list). And iTunes doesn't know how to play a list. iTunes can play a track. So by using "first" you will get the first track found and thus it should work. Alternatively you could get the first item of the list... play (item 1 of (every track...)).
Please excuse my less-than-elegant scripting ability, but the script is working fine when invoked within the script editor itself. When I save it as an application however, the icon doesn't show that it's a droplet and does not work as such. Any help is greatly appreciated!
try
set destinationFolder to "Mercury:F1_PropertyLogos:"
tell application "Finder" to set logoFileName to name of item 1 of (get selection)
end try
set file_name to logoFileName
set file_name to remove_extension(file_name)
on remove_extension(this_name)
if this_name contains "." then
set this_name to ¬
(the reverse of every character of this_name) as string
set x to the offset of "." in this_name
set this_name to (text (x + 1) thru -1 of this_name)
set this_name to (the reverse of every character of this_name) as string
end if
return this_name
end remove_extension
tell application "Finder"
set selected_items to selection
set theFolder to "Mercury:F1_PropertyLogos:"
repeat with x in selected_items
move x to theFolder
end repeat
end tell
tell application "QuarkXPress"
set mypath to "Mercury:F1_Layouts:"
set myfile to file_name
set extension to ".qxp"
set logoFolderPath to "Mercury:F1_PropertyLogos:"
set myLogoFile to file_name
set myLogoExtension to ".psd"
set myLogo to (logoFolderPath & myLogoFile & myLogoExtension)
open file (mypath & myfile & extension)
set selected of picture box "Logo" of spread 1 of document 1 to true
set image 1 of picture box "Logo" of spread 1 of document 1 to file myLogo
set bounds of image 1 of picture box "Logo" of spread 1 of document 1 to proportional fit
end tell
end
To deal with items dropped onto the application, you will need to add an open handler that receives a list of the dropped items (even if there is only one), for example:
on open theDroppedItems
-- whatever
end open
You should probably rearrange your code to place your main statements into a handler (or handlers) that can be called from multiple places, since double-clicking on the application will call the run handler instead.