applescript compare track names- if statement

Discussion in 'Mac Programming' started by odoyle81, Apr 12, 2009.

  1. macrumors newbie

    Joined:
    Mar 26, 2008
    #1
    I'm working on an itunes applescript to delete all podcasts on my ipod shuffle that have been listened to, and then add new ones from a smart playlist. The one part of the script that I need help with is some type of if statement to avoid copying a file from the smart playlist if it is already exists on the shuffle. I'm trying to do this by comparing the names of the tracks, but it doesn't seem to work. I'm new to applescript and I have a feeling I'm not using the right type of variables or I'm not building the statement correctly. Can anyone help me out with this? Thanks!!

    The statement I'm trying to use is:
    if (name of atrack is equal to value of anitem) then

    Here is the code:
    tell application "iTunes"
    repeat with thisSource in sources
    if the name of thisSource = "Paul" then set myIpod to thisSource
    end repeat

    set destinationPlaylist to the first playlist in myIpod
    set sel to every track in destinationPlaylist whose podcast is true and played count is greater than 0
    repeat with atrack in sel
    delete atrack
    end repeat

    set sourcePlaylist to playlist "newpodcasts"
    if the number of tracks in sourcePlaylist is greater than 0 then
    set shufflable of (every track of sourcePlaylist) to true
    if the number of tracks in destinationPlaylist is greater than 0 then
    set shufflefilenames to (name of file tracks in destinationPlaylist whose podcast is true)
    set newsel to (every track in sourcePlaylist)
    repeat with atrack in newsel
    repeat with anitem in shufflefilenames
    if (name of atrack is equal to value of anitem) then
    set donotdup to true
    exit repeat
    else
    set donotdup to false
    end if
    end repeat
    if donotdup is false then
    duplicate atrack to destinationPlaylist
    end if
    end repeat
    else
    duplicate every track in sourcePlaylist to destinationPlaylist
    end if

    end if

    display dialog "all podcasts have been updated and old ones have been deleted"
    end tell
     
  2. macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Location:
    Denver, CO
    #2
    Let me clean it up so people can read it.

    Code:
    tell application "iTunes"
    	repeat with thisSource in sources
    		if the name of thisSource = "Paul" then set myIpod to thisSource
    	end repeat
    
    	set destinationPlaylist to the first playlist in myIpod
    	set sel to every track in destinationPlaylist whose podcast is true and played count is greater than 0
    	repeat with atrack in sel
    		delete atrack
    	end repeat
    
    	set sourcePlaylist to playlist "newpodcasts"
    	if the number of tracks in sourcePlaylist is greater than 0 then
    		set shufflable of (every track of sourcePlaylist) to true
    		if the number of tracks in destinationPlaylist is greater than 0 then
    			set shufflefilenames to (name of file tracks in destinationPlaylist whose podcast is true)
    			set newsel to (every track in sourcePlaylist)
    			repeat with atrack in newsel
    				repeat with anitem in shufflefilenames
    					if (name of atrack is equal to value of anitem) then
    						set donotdup to true
    						exit repeat
    					else
    						set donotdup to false
    					end if
    				end repeat
    				if donotdup is false then
    					duplicate atrack to destinationPlaylist
    				end if
    			end repeat
    		else
    		duplicate every track in sourcePlaylist to destinationPlaylist
    		end if
    
    	end if
    
    	display dialog "all podcasts have been updated and old ones have been deleted"
    end tell 
    
    My suggestion:

    After reading your code, I think you might have an issue at that if statement. What is "atrack" equal to and why do you delete it earlier in your code then try to read it later? Do you set this in a precompiler or what?

    More clarification on your code is needed, mainly comments.
     
  3. thread starter macrumors newbie

    Joined:
    Mar 26, 2008
    #3
    Thanks for the suggestions - I'm new to coding and I also couldn't figure out how to paste the code with the proper indentations. I worked on this for several more hours yesterday, and I got it working and just wanted to share the working code here.

    Code:
    tell application "iTunes"
    	repeat with thisSource in sources
    		if the name of thisSource = "Paul" then set myIpod to thisSource
    	end repeat
    	
    -- this part deletes all the podcasts on the ipod shuffle which have been listened to
    	set destinationPlaylist to the first playlist in myIpod
    	set sel to every track in destinationPlaylist whose podcast is true and played count is greater than 0
    	repeat with atrack in sel
    		delete atrack
    	end repeat
    	
    	-- "newpodcasts" is a smart playlist that only contains podcasts to be added
    	set sourcePlaylist to playlist "newpodcasts"
    -- if statement to only do this if there are new podcasts to add
    	if the number of tracks in sourcePlaylist is greater than 0 then
    		set shufflable of (every track of sourcePlaylist) to true
    -- if statement to only do this if there are already tracks on the ipod shuffle
    		if the number of tracks in destinationPlaylist is greater than 0 then
    			set shufflefilenames to (name of file tracks in destinationPlaylist whose podcast is true)
    			set newsel to (every track in sourcePlaylist)
    -- check each track on the shuffle and see if it has the same name as a podcast about to be uploaded.  If it does, change a boolean variable called "donotdup" to true so this track WON'T be deleted
    			repeat with atrack in newsel
    				if name of atrack is in shufflefilenames then
    					set donotdup to true
    				else
    					set donotdup to false
    				end if
    				if donotdup is false then
    					duplicate atrack to destinationPlaylist
    				end if
    			end repeat
    		else
    			duplicate every track in sourcePlaylist to destinationPlaylist
    		end if
    		
    	end if
    	
    	display dialog "all podcasts have been updated and old ones have been deleted"
    end tell
     

Share This Page